码迷,mamicode.com
首页 > 编程语言 > 详细

java 图片压缩

时间:2019-03-10 20:33:29      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:ace   ima   tst   Fix   red   nal   stream   block   finally   

        BufferedImage image_to_save = null;
        try {
            image_to_save = ImageIO.read(new File("d:\\123.jpg"));
        } catch (IOException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }

        FileOutputStream fos = null;
        ImageWriter imageWriter = null;
        ImageOutputStream ios = null;

        try {
            fos = new FileOutputStream("d:\\124.jpg");

            imageWriter = ImageIO.getImageWritersBySuffix("jpg").next();
            ios = ImageIO.createImageOutputStream(fos);
            imageWriter.setOutput(ios);

            JPEGImageWriteParam jpegParams = (JPEGImageWriteParam) imageWriter.getDefaultWriteParam();
            jpegParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            jpegParams.setCompressionQuality(0.3f);
            imageWriter.write(null, new IIOImage(image_to_save, null, null), jpegParams);

        } catch (Exception ex) {

        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                }
            }
            if (ios != null) {
                try {
                    ios.close();
                } catch (IOException e) {
                }
            }
            if (imageWriter != null) {
                imageWriter.dispose();
            }
        }        

 

java 图片压缩

标签:ace   ima   tst   Fix   red   nal   stream   block   finally   

原文地址:https://www.cnblogs.com/merseyalma/p/10506701.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!