JAVACV最小依赖

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

本文在创作时,部分参考过行业前辈的代码,如有相似,请联系博主注明出处

创作不易,如果这篇文章能够帮助到你,希望能关注或收藏一下博主,如果文章内容有问题也可留言讨论,我们一起学习,一起进步!!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值