图片压缩不失真

来源:互联网 发布:2017淘宝扣分清零规则 编辑:程序博客网 时间:2024/04/28 19:45
public static void reduceImg(String imgsrc, String imgdist, int widthdist,   
        int heightdist) {   
    try {   
        File srcfile = new File(imgsrc);   
        if (!srcfile.exists()) {   
            return;   
        }   
        Image src = javax.imageio.ImageIO.read(srcfile);   
  
        BufferedImage tag= new BufferedImage((int) widthdist, (int) heightdist,   
                BufferedImage.TYPE_INT_RGB);   
  
        tag.getGraphics().drawImage(src.getScaledInstance(widthdist, heightdist,  Image.SCALE_SMOOTH), 0, 0,  null);   
///         tag.getGraphics().drawImage(src.getScaledInstance(widthdist, heightdist,  Image.SCALE_AREA_AVERAGING), 0, 0,  null);   
           
        FileOutputStream out = new FileOutputStream(imgdist);   
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);   
        encoder.encode(tag);   
        out.close();   
  
    } catch (IOException ex) {   
        ex.printStackTrace();   
    }   
}  
0 0
原创粉丝点击