视频通过api获取图片

原创
2019/11/15 16:23
阅读数 137
package com.htdadao.common.utils;

import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.Buffer;
import java.util.Date;
import java.util.UUID;

public class FileToImgUtil {

    private String url;

    public static String cutPhotoFromVedio(String videoUrl){
        FFmpegFrameGrabber ff = null;
        String videoPath = videoUrl;
        String imgPath = "D:/alarmPicture"+new Date().toString() + UUID.randomUUID()+".jpg";
        File targetFile = new File(imgPath);
        try{
            ff = new FFmpegFrameGrabber(videoPath);
            ff.start();
            int lenght = ff.getLengthInFrames();
            int i = 0;
            Frame f = null;
            f = ff.grabFrame();
            int owidth = f.imageWidth;
            int oheight = f.imageHeight;
            //截取的帧进行等比例缩放
            int width = 800;
            int height = (int)(((double)width/owidth)*oheight);
            Buffer[] img = f.image;
            BufferedImage bufferedImage = new Java2DFrameConverter().convert(f);
            ImageIO.write(bufferedImage, "jpg", targetFile);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return imgPath;
    }
private String grapPicture(String camera1) throws IOException {
   String imgDetail;
   String imgPath1 = FileToImgUtil.cutPhotoFromVedio(camera1);
   InputStream in = new FileInputStream(imgPath1);
   byte[] imgBytes = new byte[in.available()];
   in.read(imgBytes);
   BASE64Encoder encoder = new BASE64Encoder();
   imgDetail = encoder.encode(Objects.requireNonNull(imgBytes));
   return imgDetail;
}
}
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部