博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
图片按质量压缩
阅读量:5731 次
发布时间:2019-06-18

本文共 2423 字,大约阅读时间需要 8 分钟。

 

图片按质量压缩

 

/***     * author : dai xianjun     * 按图片质量,压缩图片,直到小于制定的存储空间     * @param originalFilePath     * @param resizedFilePath     * @param quality     * @return     * @throws IOException     */    public static boolean resizeByQuality(String originalFilePath, String resizedFilePath, float quality) throws IOException {        if (quality > 1) {            throw new IllegalArgumentException("Quality has to be between 0 and 1");        }        File originalFile = new File(originalFilePath);        if (!originalFile.exists()){            DkdLogger.error("resize",false,"originalFile is not exist,originalFilePath =: " + originalFilePath);            return false;        }        ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());        Image image = ii.getImage();        Image resizedImage = null;        resizedImage = image.getScaledInstance(image.getWidth(null), image.getHeight(null), Image.SCALE_SMOOTH);        // This code ensures that all the pixels in the image are loaded.        Image temp = new ImageIcon(resizedImage).getImage();        // Create the buffered image.        BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null),                temp.getHeight(null), BufferedImage.TYPE_INT_RGB);        // Copy image to buffered image.        Graphics g = bufferedImage.createGraphics();        // Clear background and paint the image.        g.setColor(Color.white);        g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));        g.drawImage(temp, 0, 0, null);        g.dispose();        // Soften.        float softenFactor = 0.05f;        float[] softenArray = { 0, softenFactor, 0, softenFactor, 1 - (softenFactor * 4), softenFactor, 0, softenFactor, 0 };        Kernel kernel = new Kernel(3, 3, softenArray);        ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);        bufferedImage = cOp.filter(bufferedImage, null);        // Write the jpeg to a file.        FileOutputStream out = new FileOutputStream(new File(resizedFilePath));        // Encodes image as a JPEG data stream        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);        JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufferedImage);        param.setQuality(quality, true);        encoder.setJPEGEncodeParam(param);        encoder.encode(bufferedImage);        return true;    }

 

转载于:https://www.cnblogs.com/daixianjun/p/resize-by-quality.html

你可能感兴趣的文章
【转】Python 可视化神器-Plotly Express
查看>>
计算机网络原理笔记-停止等待协议
查看>>
topcoder srm 662 div1
查看>>
Java基础之静态变量
查看>>
更换好的yum源
查看>>
NET牛人应该知道些什么?
查看>>
[Asp.Net web api]基于自定义Filter的安全认证
查看>>
洛谷P3763 [TJOI2017]DNA(后缀自动机)
查看>>
确定当前记录和下一条记录之间相差的天数
查看>>
NYOJ32:组合数(DFS入门)
查看>>
使用Callable和Future接口创建线程
查看>>
BZOJ 2568 比特集合
查看>>
sql语句返回主键SCOPE_IDENTITY()
查看>>
MongoDB培训
查看>>
机器学习开源项目精选TOP30
查看>>
python基础===对字符串进行左右中对齐
查看>>
一起谈.NET技术,ASP.NET缓存全解析6:数据库缓存依赖
查看>>
代码分析系列 内存执行过程
查看>>
iOS开发-邮件发送
查看>>
/etc/resolv.conf文件详解
查看>>