java生成缩略图

package test;

import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

public class PicView {
    public static void main (String argv[]) {
         try {
             File fi = new File("E:/test.jpg"); //大图文件
             File fo = new File("E:/test2.jpg"); //将要转换出的小图文件

             AffineTransform transform = new AffineTransform();
             BufferedImage bis = ImageIO.read(fi);

             int w = bis.getWidth();
             int h = bis.getHeight();
             double scale = (double)w/h;

             int nw = 120;
             int nh = (nw * h) / w;
             if(nh>120) {
                 nh = 120;
                 nw = (nh * w) / h;
             }

             double sx = (double)nw / w;
             double sy = (double)nh / h;

             transform.setToScale(sx,sy);

             AffineTransformOp ato = new AffineTransformOp(transform, null);
             BufferedImage bid = new BufferedImage(nw, nh, BufferedImage.TYPE_3BYTE_BGR);
             ato.filter(bis,bid);
             ImageIO.write(bid, "jpeg", fo);
         } catch(Exception e) {
             e.printStackTrace();
         }
     }


}

你可能感兴趣的:(java生成缩略图)