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 2024-06-13 21:30  疯狂的妞妞  阅读(4)  评论(0编辑  收藏  举报

导航