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; }
}