Bufferedimage resize

后端 未结 7 1065
深忆病人
深忆病人 2020-11-29 03:50

I am trying to resized a bufferedimage. I am able to store it and show up on a jframe no problems but I can\'t seem to resize it. Any tips on how I can change this to make i

7条回答
  •  既然无缘
    2020-11-29 04:24

    If all that is required is to resize a BufferedImage in the resize method, then the Thumbnailator library can do that fairly easily:

    public static BufferedImage resize(BufferedImage img, int newW, int newH) {
      return Thumbnails.of(img).size(newW, newH).asBufferedImage();
    }
    

    The above code will resize the img to fit the dimensions of newW and newH while maintaining the aspect ratio of the original image.

    If maintaining the aspect ratio is not required and resizing to exactly the given dimensions is required, then the forceSize method can be used in place of the size method:

    public static BufferedImage resize(BufferedImage img, int newW, int newH) {
      return Thumbnails.of(img).forceSize(newW, newH).asBufferedImage();
    }
    

    Using the Image.getScaledInstance method will not guarantee that the aspect ratio of the original image will be maintained for the resized image, and furthermore, it is in general very slow.

    Thumbnailator uses a technique to progressively resize the image which can be several times faster than Image.getScaledInstance while achieving an image quality which generally is comparable.

    Disclaimer: I am the maintainer of this library.

提交回复
热议问题