图片和视频压缩例子

/**
     * 视频压缩 需要jave jar 包。到网上下载即可  
     * 
     * @param source 需要压缩的视频
     * @param targetPath 压缩的目标路径
     * @return
     */
    public static boolean compressVideo(File source, String targetPath) {
        System.out.println("source:" + source);
        System.out.println("targetPath:" + targetPath);
        try {
            // 编码器
            Encoder encoder = new Encoder();
            // 源视频的信息
            MultimediaInfo mInfo = encoder.getInfo(source);
            AudioInfo audioInfo = mInfo.getAudio();
            VideoInfo videoInfo = mInfo.getVideo();
            // 音频参数设置(一些参数都是使用源视频)
            AudioAttributes audio = new AudioAttributes();
            audio.setBitRate(44100);
            audio.setChannels(audioInfo.getChannels());
            audio.setSamplingRate(audioInfo.getSamplingRate());
            audio.setCodec("libmp3lame");
            audio.setVolume(256);// 256表示原声,小于表示减少音量 大于表示增加音量

            // 视频参数设置(一些参数都是使用源视频)
            VideoAttributes video = new VideoAttributes();
            video.setCodec("mpeg4");
            video.setBitRate(videoInfo.getBitRate());
//            VideoSize size = new VideoSize(720, 960);
//            video.setSize(size);
            video.setFrameRate(30);

            // 视频转码编码设置
            EncodingAttributes attrs = new EncodingAttributes();
            attrs.setFormat("mp4");
            attrs.setAudioAttributes(audio);
            attrs.setVideoAttributes(video);
            System.out.println("audioInfo:" + audioInfo);
            System.out.println("videoInfo:" + videoInfo);
            System.out.println("mInfo" + mInfo);
            File target = new File(targetPath);
            String decoderstr = videoInfo.getDecoder();
            System.out.println("decoderstr:" + decoderstr);
            String[] decoder = encoder.getSupportedDecodingFormats();
            List<String> list = new ArrayList<String>();
            for (int i = 0; i < decoder.length; i++) {
                list.add(decoder[i]);
            }
            // 如果视频解码方式存在就编码。不然就不用
            // 。。查看解码格式类型:http://www.sauronsoftware.it/projects/jave/manual.php#3.1
            if (list.contains(decoderstr)) {
                encoder.encode(source, target, attrs);
                logger.info("压缩完成...");
            } else {
                logger.info("不支持该编码的视频,编码为:{};提示:压缩失败", decoderstr);
            }
        } catch (EncoderException e) {
            logger.info("视频压缩出现异常");
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 图片压缩
     * 
     * @param source
     * @param target
     */
    public static void compressPicture(File source, String targetPath) {
        try {
            System.out.println("文件:" + source + "临时文件路径:" + targetPath);
            System.out.println("begin compress picture。。。。");
            FileOutputStream fos = new FileOutputStream(targetPath);
            BufferedImage bi = ImageIO.read(source);
            int srcWidth = bi.getWidth();
            int srcHeight = bi.getHeight();
            Image image = bi.getScaledInstance(srcWidth, srcHeight, Image.SCALE_SMOOTH);
            BufferedImage tag = new BufferedImage(srcWidth, srcHeight, BufferedImage.TYPE_INT_RGB);
            Graphics g = tag.getGraphics();
            g.setColor(Color.RED);
            g.drawImage(image, 0, 0, null);
            g.dispose();
            ImageIO.write(tag, "jpg", fos);
            System.out.println("end compress picture。。。。");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

转载于:https://www.cnblogs.com/gaolt/p/10556543.html

阅读终点,创作起航,您可以撰写心得或摘录文章要点写篇博文。去创作
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Vue中,可以使用一些库来实现图片视频上传前压缩大小的功能。下面我将分别介绍两种情况的解决方案。 1. 图片上传前压缩大小: 可以使用`vue-image-compressor`库来实现图片上传前的压缩。首先,安装该库: ```bash npm install vue-image-compressor ``` 然后,在需要进行图片上传的组件中,导入并注册该库: ```javascript import VueImageCompressor from 'vue-image-compressor'; export default { components: { VueImageCompressor }, // ... } ``` 接下来,在模板中使用`vue-image-compressor`组件,设置相关属性来进行图片压缩: ```html <template> <div> <vue-image-compressor ref="compressor" :max-width="800" :max-height="600"></vue-image-compressor> <input type="file" @change="compressImage"> </div> </template> <script> export default { methods: { compressImage(event) { const file = event.target.files[0]; this.$refs.compressor.compress(file) .then(compressedImage => { // 压缩后的图片对象 console.log(compressedImage); // 在这里进行图片上传操作 }) .catch(error => { console.error(error); }); } } } </script> ``` 在上述代码中,`max-width`和`max-height`属性用于设置压缩后的图片的最大宽度和高度。通过`this.$refs.compressor.compress(file)`方法来进行图片压缩,返回的`compressedImage`即是压缩后的图片对象,可以在该对象中获取到压缩后的图片数据,然后进行上传操作。 2. 视频上传前压缩大小: 对于视频压缩,可以使用`vue-video-compressor`库。首先,安装该库: ```bash npm install vue-video-compressor ``` 然后,在需要进行视频上传的组件中,导入并注册该库: ```javascript import VueVideoCompressor from 'vue-video-compressor'; export default { components: { VueVideoCompressor }, // ... } ``` 接下来,在模板中使用`vue-video-compressor`组件,设置相关属性来进行视频压缩: ```html <template> <div> <vue-video-compressor ref="compressor" :max-size="10"></vue-video-compressor> <input type="file" @change="compressVideo"> </div> </template> <script> export default { methods: { compressVideo(event) { const file = event.target.files[0]; this.$refs.compressor.compress(file) .then(compressedVideo => { // 压缩后的视频对象 console.log(compressedVideo); // 在这里进行视频上传操作 }) .catch(error => { console.error(error); }); } } } </script> ``` 在上述代码中,`max-size`属性用于设置压缩后的视频文件大小的上限(单位为MB)。通过`this.$refs.compressor.compress(file)`方法来进行视频压缩,返回的`compressedVideo`即是压缩后的视频对象,可以在该对象中获取到压缩后的视频数据,然后进行上传操作。 以上就是在Vue中实现图片视频上传前压缩大小的简单示例,希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值