截取视频中的图片
程序员文章站
2022-06-26 08:41:14
...
#将接收到的 MultipartFile 转为File
/**
* 上传视频,截取图片,并将图片上传,返回图片地址值
*/
@PostMapping("/avg/getImg")
public Result getImg(MultipartFile file) {
File toFile = null;
try {
long size = file.getSize();
if (size > 1024 * 1024 * 10) {
return new Result(false, 9, “视频过大,不能超过10M”);
}
if (file.equals("") || file.getSize() <= 0) {
file = null;
} else {
InputStream ins = null;
ins = file.getInputStream();
toFile = new File(file.getOriginalFilename());
OutputStream os = new FileOutputStream(toFile);
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();
//释放流
ins.close();
}
return JavacvUtil.avGetImg(toFile);
} catch (Exception e) {
return new Result(false, 3, "凉了");
} finally {
//删除临时文件
if (toFile != null) {
File del = new File(toFile.toURI());
//删除临时文件
del.delete();
}
}
}
##截取图片并上传到服务器
/**
* 传过来一个视频file返回一个图片路径
*/
public static Result avGetImg(File toFile) throws IOException {
// todo 截图开始
Random random = new Random();
String val = String.valueOf(random.nextInt(99) + 1);//生成第一个数字,不能为0
//定义截过图存放的路径
String imgUrl = “src/main/resources/static/files/a” + val + “.jpg”;
long start = System.currentTimeMillis();
File targetFile = new File(imgUrl);
@SuppressWarnings("resource")
FFmpegFrameGrabber ff = new FFmpegFrameGrabber(toFile);
ff.start();
int lenght = ff.getLengthInFrames();
int i = 0;
Frame f = null;
while (i < lenght) {
// 过滤前5帧,避免出现全黑的图片,依自己情况而定
f = ff.grabFrame();
if ((i > 48) && (f.image != null)) {
break;
}
i++;
}
// IplImage img = f.image;
int owidth = f.imageWidth;
int oheight = f.imageHeight;
// 对截取的帧进行等比例缩放
int width = 300;
int height = (int) (((double) width / owidth) * oheight);
Java2DFrameConverter converter = new Java2DFrameConverter();
BufferedImage fecthedImage = converter.getBufferedImage(f);
BufferedImage bi = new BufferedImage(width, height,
BufferedImage.TYPE_3BYTE_BGR);
bi.getGraphics().drawImage(
fecthedImage.getScaledInstance(width, height,
Image.SCALE_SMOOTH), 0, 0, null);
ImageIO.write(bi, "jpg", targetFile);
ff.stop();
System.out.println(System.currentTimeMillis() - start);
//截取完成开始上传图片
File img = new File(imgUrl);
//将file转为MultipartFile
FileInputStream fileInput = new FileInputStream(img);
MultipartFile toMultipartFile = new MockMultipartFile("file", img.getName(), "text/plain", IOUtils.toByteArray(fileInput));
toMultipartFile.getInputStream();
String imageName = toMultipartFile.getOriginalFilename();
String extName = imageName.substring(imageName.lastIndexOf(".") + 1);
FastDfsUtil fastDfsUtil = new FastDfsUtil();
FastDfsFile fastDfsFile = null;
try {
fastDfsFile = new FastDfsFile(imageName, toMultipartFile.getBytes(), extName);
String url = fastDfsUtil.uploadImage(fastDfsFile);
return new Result(true, 0, url);
} catch (Exception e) {
e.printStackTrace();
return new Result(false, 2, "截图后上传图片出现问题");
}
}
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacpp</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>opencv-platform</artifactId>
<version>3.4.1-1.4.1</version>
</dependency>
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>ffmpeg-platform</artifactId>
<version>3.4.2-1.4.1</version>
</dependency>
<!--iowtils-->
<dependency>
<groupId>com.jwebmp</groupId>
<artifactId>commons-io</artifactId>
<version>0.63.0.61</version>
</dependency>
<!--File转MultipartFile时使用了 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-mock</artifactId>
<version>2.0.8</version>
</dependency>
上一篇: HTML常用标签