public static void main(String[] args) throws Exception {
//缩略图
BufferedImage src = ImageIO.read(new File("image2.jpg"));
int w = src.getWidth();
int h = src.getHeight();
//生成指定宽高得缩略图
//int w1 = (int)(w * 0.1f);
//int h1 = (int)(h * 0.1f);
int w1 = 750;
int h1 = (int)(h*w1/w);
//建立一个w1宽,h1高,真彩格式的图片
BufferedImage bf = new BufferedImage(w1, h1, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bf.createGraphics();
//再bf图片上画src,从坐标0,0,开始画,bf图片的宽高为w1,h1
g.drawImage(src,0,0,w1,h1,null);
g.dispose();
//生成图片
//ImageIO.write(bf,"jpg",new File("image3.jpg"));
//也可生成指定动态名字的图片
ImageIO.write(bf,"jpg",new File(String.format("image2_%d_%d.jpg",w1,h1)));
}
ImageIO将图片变成缩略图
最新推荐文章于 2023-11-21 18:05:51 发布