JAVACV依赖包精简
因为最近做了一个人脸对比的项目,需要将上传的视频截取特定的帧,将截取下来的图片再进行保存,这么一个操作,发现网上大多都是这种操作
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.4.4</version>
</dependency>
打包测试项目的时候,从原来的几十M变成了500多M,后来找了很多方法,操作如下,再次打包后不到100M了,原因是直接引用 javacv-platform 会将其其他所有平台的兼容jar都拉下来(包括linux版本的,ios, Windows,Android等)。如下图:
<!-- 截取视频中的图片 -->
<!-- 精简前 -->
<!-- <dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.4.4</version>
</dependency> -->
<!-- 精简后 注意:javacv和javacpp为必不可缺-->
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<version>1.5.2</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacpp</artifactId>
<version>1.5.2</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>opencv</artifactId>
<version>4.1.2-1.5.2</version>
<!-- 本机和Linux不同环境配置不一样 -->
<!-- <classifier>windows-x86_64</classifier>-->
<classifier>linux-x86_64</classifier>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>openblas</artifactId>
<version>0.3.7-1.5.2</version>
<!-- <classifier>windows-x86_64</classifier>-->
<classifier>linux-x86_64</classifier>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>ffmpeg</artifactId>
<version>4.2.1-1.5.2</version>
<!-- <classifier>windows-x86_64</classifier>-->
<classifier>linux-x86_64</classifier>
</dependency>
注意再本机测试时是Windows系统,classifier用windows-x86_64,项目部署到Linux上时修改为linux-x86_64
以下附录java截取图片代码:
/** 此方法如有雷同纯属意外(及时联系我注明出处)
* 截取视频指定帧保存为指定格式的图片(图片保存在视频同文件夹下)
* @param vid_file 视频地址
* @param imgSuffix 图片格式
* @param indexFrame 第几帧
*/
public static void CropImg(String vid_file, String imgSuffix, Integer indexFrame) throws Exception {
if (indexFrame == null) {
indexFrame = 100;
}
Integer suffixIndex = vid_file.lastIndexOf(".");
File targetFile = new File((suffixIndex != -1 ? vid_file.substring(0, suffixIndex) : vid_file) + imgSuffix);
try (FFmpegFrameGrabber ff = new FFmpegFrameGrabber(vid_file);
OutputStream outputStream = new FileOutputStream(targetFile)) {
ff.start();
ff.setFrameNumber(indexFrame);
Frame f = ff.grabImage();
Java2DFrameConverter converter = new Java2DFrameConverter();
BufferedImage fecthedImage = converter.getBufferedImage(f);
ImageIO.write(fecthedImage, "jpg", outputStream);
}
}
public static void main(String[] args) {
try {
CropImg("C:\\Users\\ASUS\\Downloads\\test1.mp4", ".jpg", 100);
} catch (Exception e) {
e.printStackTrace();
}
}
本文在创作时,部分参考过行业前辈的代码,如有相似,请联系博主注明出处
创作不易,如果这篇文章能够帮助到你,希望能关注或收藏一下博主,如果文章内容有问题也可留言讨论,我们一起学习,一起进步!!