制作缩略图,主要使用于商品列表小图以及点开后详情图,如淘宝商品鼠标移焦放大 // 原图BufferedImage src = ImageIO.read(new File("d:/a.jpg"));int sw = src.getWidth();int sh = src.getHeight();// 新的目标缩
// 原图
BufferedImage src = ImageIO.read(new File("d:/a.jpg"));
int sw = src.getWidth();
int sh = src.getHeight();
// 新的目标缩略图
//指定尺寸
int w = 100;
int h = 100;
//宽度固定,高度按比例
w = 300;
h = (int)(sh * ((float)w/sw));
BufferedImage dst = new BufferedImage(w, h, 1);
Graphics2D g = (Graphics2D) dst.getGraphics();
g.drawImage(src, 0, 0, w, h, null);
ImageIO.write(dst, "jpg", new File("d:/a_s.jpg"));
//裁剪
public static void crop(String[] args) throws IOException {
// 原图
BufferedImage src = ImageIO.read(new File("d:/o.jpg"));
int sw = src.getWidth();
int sh = src.getHeight();
//裁剪新图片
int w = 150;
int h = 150;
BufferedImage dst = new BufferedImage(w, h, 1);
Graphics2D g = (Graphics2D) dst.getGraphics();
int x = 300;
int y = 180;
int xx = x+w;
int yy = y+h;
g.drawImage(src,0,0,w,h,x,y,xx,yy,null);
ImageIO.write(dst, "jpg", new File("d:/ooo.jpg"));
}
