调整图片大小,保持纵横比

77

我有一张需要调整大小的图片:

if((width != null) || (height != null))
{
    try{
        // Scale image on disk
        BufferedImage originalImage = ImageIO.read(file);
        int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB
                                                : originalImage.getType();

        BufferedImage resizedImageJpg = resizeImage(originalImage, type, 200, 200);
        ImageIO.write(resizedImageJpg, "jpg", file); 

       } catch(IOException e) {
           System.out.println(e.getMessage());
       }
}

这是我调整图像大小的方式:

private static BufferedImage resizeImage(BufferedImage originalImage, int type,
                                         Integer imgWidth, Integer imgHeight)
{
    var resizedImage = new BufferedImage(imgWidth, imgHeight, type);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(originalImage, 0, 0, imgWidth, imgHeight, null);
    g.dispose();

    return resizedImage;
}

现在的问题是,我还需要保持纵横比。也就是说,我需要新的200/200图像包含新的缩放图像。类似于这样: enter image description here

我尝试了一些方法,但它们并没有如预期那样奏效。 非常感谢您的帮助。

11个回答

117

下面开始:

Dimension imgSize = new Dimension(500, 100);
Dimension boundary = new Dimension(200, 200);

根据边界返回新尺寸的函数:

public static Dimension getScaledDimension(Dimension imgSize, Dimension boundary) {

    int original_width = imgSize.width;
    int original_height = imgSize.height;
    int bound_width = boundary.width;
    int bound_height = boundary.height;
    int new_width = original_width;
    int new_height = original_height;

    // first check if we need to scale width
    if (original_width > bound_width) {
        //scale width to fit
        new_width = bound_width;
        //scale height to maintain aspect ratio
        new_height = (new_width * original_height) / original_width;
    }

    // then check if we need to scale even with the new height
    if (new_height > bound_height) {
        //scale height to fit instead
        new_height = bound_height;
        //scale width to maintain aspect ratio
        new_width = (new_height * original_width) / original_height;
    }

    return new Dimension(new_width, new_height);
}

如果有人需要图像大小调整代码,这里有一个不错的解决方案

如果您对上面的解决方案不确定,还有其他方法可以实现相同的结果。


1
我在Android中使用了相同的逻辑来调整位图大小 :) - James andresakis
2
@Ozzy的答案是正确的,除非不需要缩放(或更具体地说,当原始宽度小于边界时)。将new_widthnew_height初始化为original_widthoriginal_height可以解决这个问题。 - Ryan Morlok
我知道这是针对Java的,但我将其转换为JS,以防其他人需要。 function getDimensions(originalWidth,originalHeight,newWidth,newHeight){ var dimensions = {}; dimensions.width = originalWidth; dimensions.height = originalHeight; if(originalWidth > newWidth){ dimensions.width = newWidth; dimensions.height = (dimensions.width * originalHeight)/originalWidth; } if(dimensions.height > newHeight){ dimensions.height = newHeight; dimensions.width = (dimensions.height * originalWidth) / originalHeight; } return dimensions;} - MorningDew
2
如果目标尺寸更大,这个方法就不起作用了。所以,对于这个方法,应该将'scale'改为'shrink'。 - yılmaz
2
这个解决方案对我有效,直到我需要将图像调整大小到超过图像本身的边界。也许这个链接更有意义:https://hastebin.com/devegemari.cpp - Cardinal System
显示剩余4条评论

32

这里翻译:

Dimension getScaledDimension(Dimension imageSize, Dimension boundary) {

    double widthRatio = boundary.getWidth() / imageSize.getWidth();
    double heightRatio = boundary.getHeight() / imageSize.getHeight();
    double ratio = Math.min(widthRatio, heightRatio);

    return new Dimension((int) (imageSize.width  * ratio),
                         (int) (imageSize.height * ratio));
}
您还可以使用imgscalr来在保持长宽比的同时调整图像大小:
BufferedImage resizeMe = ImageIO.read(new File("orig.jpg"));
Dimension newMaxSize = new Dimension(255, 255);
BufferedImage resizedImg = Scalr.resize(resizeMe, Method.QUALITY,
                                        newMaxSize.width, newMaxSize.height);

1
这应该是被接受的答案,代码更小,也适用于较小的图像!谢谢! - Simon Ludwig
我将Scalr.java文件放入我的项目中,这个例子完美地运行了——没有任何问题。 - Sean Anderson

5

2

加载图片:

BufferedImage bufferedImage = ImageIO.read(file);   

调整大小:

private BufferedImage resizeAndCrop(BufferedImage bufferedImage, Integer width, Integer height) {

    Mode mode = (double) width / (double) height >= (double) bufferedImage.getWidth() / (double) bufferedImage.getHeight() ? Scalr.Mode.FIT_TO_WIDTH
            : Scalr.Mode.FIT_TO_HEIGHT;

    bufferedImage = Scalr.resize(bufferedImage, Scalr.Method.ULTRA_QUALITY, mode, width, height);

    int x = 0;
    int y = 0;

    if (mode == Scalr.Mode.FIT_TO_WIDTH) {
        y = (bufferedImage.getHeight() - height) / 2;
    } else if (mode == Scalr.Mode.FIT_TO_HEIGHT) {
        x = (bufferedImage.getWidth() - width) / 2;
    }

    bufferedImage = Scalr.crop(bufferedImage, x, y, width, height);

    return bufferedImage;
}

使用Scalr库:
<dependency>
    <groupId>org.imgscalr</groupId>
    <artifactId>imgscalr-lib</artifactId>
    <version>4.2</version>
</dependency>

2
这是我编写的一小段代码,它可以调整图像大小以适应容器并保持图像的原始宽高比。它接受容器的宽度、高度和图像作为参数。您可以根据自己的需求进行修改。这很简单,对我的应用程序非常有效。
private Image scaleimage(int wid, int hei, BufferedImage img){
    Image im = img;
    double scale;
    double imw = img.getWidth();
    double imh = img.getHeight();
    if (wid > imw && hei > imh){
        im = img;
    }else if(wid/imw < hei/imh){
        scale = wid/imw;
        im = img.getScaledInstance((int) (scale*imw), (int) (scale*imh), Image.SCALE_SMOOTH);
    }else if (wid/imw > hei/imh){
        scale = hei/imh;
        im = img.getScaledInstance((int) (scale*imw), (int) (scale*imh), Image.SCALE_SMOOTH);
    }else if (wid/imw == hei/imh){
        scale = wid/imw;
        im = img.getScaledInstance((int) (scale*imw), (int) (scale*imh), Image.SCALE_SMOOTH);
    } 
    return im;
}

1
尝试这个。
float rateX =  (float)jpDisplayImagen.getWidth()/(float)img.getWidth();
float rateY = (float)jpDisplayImagen.getHeight()/(float)img.getHeight();
if (rateX>rateY){
    int W=(int)(img.getWidth()*rateY);
    int H=(int)(img.getHeight()*rateY);
    jpDisplayImagen.getGraphics().drawImage(img, 0, 0,W,H, null);
}
else{
    int W=(int)(img.getWidth()*rateX);
    int H=(int)(img.getHeight()*rateX);
    jpDisplayImagen.getGraphics().drawImage(img, 0, 0,W,H, null);
}

1
这是我的解决方案:
/*
Change dimension of Image
*/
public static Image resizeImage(Image image, int scaledWidth, int scaledHeight, boolean preserveRatio) {  

    if (preserveRatio) { 
        double imageHeight = image.getHeight();
        double imageWidth = image.getWidth();

        if (imageHeight/scaledHeight > imageWidth/scaledWidth) {
            scaledWidth = (int) (scaledHeight * imageWidth / imageHeight);
        } else {
            scaledHeight = (int) (scaledWidth * imageHeight / imageWidth);
        }        
    }                   
    BufferedImage inputBufImage = SwingFXUtils.fromFXImage(image, null);     
    // creates output image
    BufferedImage outputBufImage = new BufferedImage(scaledWidth, scaledHeight, inputBufImage.getType());       
    // scales the input image to the output image
    Graphics2D g2d = outputBufImage.createGraphics();
    g2d.drawImage(inputBufImage, 0, 0, scaledWidth, scaledHeight, null);
    g2d.dispose();      
    return SwingFXUtils.toFXImage(outputBufImage, null);
}

1
public class ImageTransformation {

public static final String PNG = "png";

public static byte[] resize(FileItem fileItem, int width, int height) {

    try {
        ResampleOp resampleOp = new ResampleOp(width, height);
        BufferedImage scaledImage = resampleOp.filter(ImageIO.read(fileItem.getInputStream()), null);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(scaledImage, PNG, baos);

        return baos.toByteArray();
    } catch (Exception ex) {
        throw new MapsException("An error occured during image resizing.", ex);
    }

}

public static byte[] resizeAdjustMax(FileItem fileItem, int maxWidth, int maxHeight) {

    try {
        BufferedInputStream bis = new BufferedInputStream(fileItem.getInputStream());  
        BufferedImage bufimg = ImageIO.read(bis);   

        //check size of image 
        int img_width = bufimg.getWidth();
        int img_height = bufimg.getHeight();
        if(img_width > maxWidth || img_height > maxHeight) {
            float factx = (float) img_width / maxWidth;
            float facty = (float) img_height / maxHeight;
            float fact = (factx>facty) ? factx : facty;
            img_width = (int) ((int) img_width / fact);
            img_height = (int) ((int) img_height / fact);
        }

        return resize(fileItem,img_width, img_height);

    } catch (Exception ex) {
        throw new MapsException("An error occured during image resizing.", ex);
    }

}

}


0

我发现所选答案在升级方面存在问题,因此我又制作了另一个版本(我已经测试过):

public static Point scaleFit(Point src, Point bounds) {
  int newWidth = src.x;
  int newHeight = src.y;
  double boundsAspectRatio = bounds.y / (double) bounds.x;
  double srcAspectRatio = src.y / (double) src.x;

  // first check if we need to scale width
  if (boundsAspectRatio < srcAspectRatio) {
    // scale width to fit
    newWidth = bounds.x;
    //scale height to maintain aspect ratio
    newHeight = (newWidth * src.y) / src.x;
  } else {
    //scale height to fit instead
    newHeight = bounds.y;
    //scale width to maintain aspect ratio
    newWidth = (newHeight * src.x) / src.y;
  }

  return new Point(newWidth, newHeight);
}

使用Android术语编写 :-)

关于测试:

@Test public void scaleFit() throws Exception {
  final Point displaySize = new Point(1080, 1920);
  assertEquals(displaySize, Util.scaleFit(displaySize, displaySize));
  assertEquals(displaySize, Util.scaleFit(new Point(displaySize.x / 2, displaySize.y / 2), displaySize));
  assertEquals(displaySize, Util.scaleFit(new Point(displaySize.x * 2, displaySize.y * 2), displaySize));
  assertEquals(new Point(displaySize.x, displaySize.y * 2), Util.scaleFit(new Point(displaySize.x / 2, displaySize.y), displaySize));
  assertEquals(new Point(displaySize.x * 2, displaySize.y), Util.scaleFit(new Point(displaySize.x, displaySize.y / 2), displaySize));
  assertEquals(new Point(displaySize.x, displaySize.y * 3 / 2), Util.scaleFit(new Point(displaySize.x / 3, displaySize.y / 2), displaySize));
}

0
所有其他答案都展示了如何根据新图像的宽度或反之亦然来计算新图像的高度,并且如何使用Java Image API调整图像大小。对于那些寻求简单解决方案的人,我推荐使用任何能够在一行代码中完成此操作的Java图像处理框架。
下面的示例使用Marvin Framework
// 300 is the new width. The height is calculated to maintain aspect.
scale(image.clone(), image, 300);

必要的导入:

import static marvin.MarvinPluginCollection.*

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接