jdk1.6图片压缩

在高版本的jdk中,暂未找到替换方案

class Test{
    /***
     * 图片缩放处理
     * <pre>
     *   Some guidelines:
     *     0.75 high quality
     *     0.5  medium quality
     *     0.25 low quality
     * </pre>
     *
     * @param quality   压缩质量 0.0-1.0 setting of desired quality level.
     * @param is        源文件路径
     * @param os        目标文件路径
     * @param width     预期宽度
     * @param height    预期高度
     * @throws IOException io
     */
    public static void scale(InputStream is, OutputStream os, int width, int height, float quality) throws IOException {
        Image srcFile = ImageIO.read(is);
        BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        tag.getGraphics().drawImage(srcFile, 0, 0, width, height, null);
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
        JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
        jep.setQuality(quality, true);
        encoder.encode(tag, jep);
    }
}

posted on   疯狂的妞妞  阅读(4)  评论(0编辑  收藏  举报

(评论功能已被禁用)
相关博文:
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示