将图像大小调整为固定大小

时间:2011-04-29 21:53:29

标签: java image-processing image-resizing

我需要一个逻辑来重新调整图像的大小,使其始终为320X240或更小,并且应该保持宽高比。

2 个答案:

答案 0 :(得分:0)

1) determine which side of the image is the larger one
2) if its width, calc: faktor = 320/width, else faktor = 240/height;
3) downscale the image by size.x *faktor, size.y *faktor.

示例:

鉴于:图片有1024x640

1) its wider than high.
2) faktor = 320 / 1024 -> 0.3125
3) 1024 * 0.3125 = 320 (huh!)     
   640 * 0.3125 = 200.

答案 1 :(得分:0)

查看ImgScalr,java库。具有多种转换质量和速度设置,并可自动为您缩放。

BufferedImage sourceImage = ImageIO.read(new File(sourceFile));
BufferedImage destImage = Scalr.resize(sourceImage, Scalr.Method.ULTRA_QUALITY, 320, 240, Scalr.OP_ANTIALIAS);