Java 图片、视频生成缩略图

在后台将图片、视频、生成缩略图

前言
对于视频上传在后台生成缩略图。

1 下载FFmpeg

1.1 参考地址:http://www.ffmpeg.org/download.html在这里插入图片描述

1.2 下载此文件(.zip文件),进行解压;设定无汉字的目录(java中用的到)
在这里插入图片描述
1.3 在java中为此文件设定路径----(ffmpeg_path)
在这里插入图片描述

2 后台代码

public static boolean processImg(String veido_path, String ffmpeg_path, String thumb_path) {
        File file = new File(veido_path);
        if (!file.exists()) {
            System.err.println("路径[" + veido_path + "]对应的视频文件不存在!");
            return false;
        }
        List<String> commands = new java.util.ArrayList<String>();
        commands.add(ffmpeg_path);  //ffmpeg.exe存放路径
        commands.add("-i");
        commands.add(veido_path);  //视频路径--如:(F:\FileRecv\test.mp4)
        commands.add("-y");
        commands.add("-f");
        commands.add("image2");
        commands.add("-t");
        commands.add("0.001");
        commands.add("-s");
        commands.add("320x240");
        commands.add(thumb_path+ ".jpg");  //缩略图路径--如:(F:\FileRecv\test.jpg)
        try {
			/*多线程*/
            ProcessBuilder builder = new ProcessBuilder();
            builder.command(commands);
            builder.start();
            return true;
			/*单线程*/
			ProcessBuilder builder = new ProcessBuilder();
            builder.command(commands);
            Process process = builder.start();
            process.waitFor();
            int result = process.exitValue();
            System.out.println("result:" + result);
            if (result != 0) {
                return false;
            }else{
                return true;
            }
			
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    public static boolean sltimg (String veido_path, String thumb_path){
            try {
                File fi = new File(veido_path); //大图文件
                File fo = new File(thumb_path+ ".jpg"); //将要转换出的小图文件
                int nw = 100;
                AffineTransform transform = new AffineTransform();
                BufferedImage bis = ImageIO.read(fi); //读取图片
                int w = bis.getWidth();
                int h = bis.getHeight();
                //double scale = (double)w/h;
                int nh = (nw*h)/w ;
                double sx = (double)nw/w;
                double sy = (double)nh/h;
                transform.setToScale(sx,sy); //setToScale(double sx, double sy) 将此变换设置为缩放变换。
                AffineTransformOp ato = new AffineTransformOp(transform,null);
                BufferedImage bid = new BufferedImage(nw,nh,BufferedImage.TYPE_3BYTE_BGR);
                ato.filter(bis,bid);
                ImageIO.write(bid,"jpeg",fo);
                return true;
            } catch(Exception e) {
                e.printStackTrace();
                return false;
            }
    }

作者:liuqing
来源:CSDN
原文:https://blog.csdn.net/qq_39711356/article/details/90644568
版权声明:本文为博主原创文章,转载请附上博文链接!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值