package cn.com.ty.lift.ud.utils;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import org.apache.commons.codec.binary.Base64;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.springframework.web.multipart.MultipartFile;

import sun.misc.BASE64Encoder;

public class ImgUtils {
    /**
     * 获取指定视频的帧并保存为图片至指定目录
     * 
     * @param file
     *            源视频文件
     * @throws Exception
     */
    public static String fetchPic(File file) throws Exception {
        FFmpegFrameGrabber ff = new FFmpegFrameGrabber(file);
        return aa(ff);
    }

    public static String fetchPic(InputStream input) throws Exception {
        FFmpegFrameGrabber ff = new FFmpegFrameGrabber(input);
        return aa(ff);
    }

    public static String aa(FFmpegFrameGrabber ff) throws Exception {
        ff.start();
        int lenght = ff.getLengthInFrames();
        int i = 0;
        int[] rgb = new int[3];
        Frame frame = null;
        while (i < lenght) {
            // 过滤前50帧，避免出现全黑的图片，依自己情况而定
            frame = ff.grabFrame();
            // 判断图片黑色区域是否过高
            if ((i > 50) && (frame.image != null)) {
                Java2DFrameConverter converter = new Java2DFrameConverter();
                BufferedImage bif = converter.getBufferedImage(frame);
                int width = bif.getWidth();
                int height = bif.getHeight();
                int minx = bif.getMinX();
                int miny = bif.getMinY();
                int sumPixel = (width - minx) * (height - miny); // 总像素
                int blackPixel = 0; // 黑色像素
                for (int m = minx; m < width; m++) {
                    for (int j = miny; j < height; j++) {
                        // 得到指定像素（i,j)上的RGB值，
                        int pixel = bif.getRGB(m, j);
                        // 分别进行位操作得到 r g b上的值
                        rgb[0] = (pixel & 0xff0000) >> 16;
                        rgb[1] = (pixel & 0xff00) >> 8;
                        rgb[2] = (pixel & 0xff);
                        if (rgb[0] < 99 && rgb[0] >= 0 && rgb[1] < 99 && rgb[1] >= 0 && rgb[2] < 99 && rgb[2] >= 0) {
                            blackPixel++;
                        }
                    }
                }
                if (blackPixel / (sumPixel / 100) < 90) {
                    break;
                }
            }
            i++;
        }

        Java2DFrameConverter converter = new Java2DFrameConverter();
        BufferedImage srcBi = converter.getBufferedImage(frame);
        int owidth = srcBi.getWidth();
        int oheight = srcBi.getHeight();
        // 对截取的帧进行等比例缩放
        int width = 800;
        int height = (int)(((double)width / owidth) * oheight);
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
        bi.getGraphics().drawImage(srcBi.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(bi, "jpg", baos);

        byte[] bytes = baos.toByteArray();// 转换成字节
        BASE64Encoder encoder = new BASE64Encoder();
        String png_base64 = encoder.encodeBuffer(bytes).trim();// 转换成base64串
        png_base64 = png_base64.replaceAll("\n", "").replaceAll("\r", "");// 删除// \r\n
        return png_base64;
    }

    /*public void coverFile(MultipartFile file)throws IOException{
        //MultipartFile file = ((MultipartHttpServletRequest) request).getFile("file");
        File f = null;
        if(file.equals("")||file.getSize()<=0){
            file = null;
        }else{
            InputStream ins = file.getInputStream();
            f=new File(file.getOriginalFilename());
            inputStreamToFile(ins, f);
        }
    
        File del = new File(f.toURI());
        del.delete();
    }*/

    public static void inputStreamToFile(InputStream ins, File file) {
        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
