Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bufferedimage resize

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 it work and show the image as a 200*200 file would be great

private void profPic(){     String path = factory.getString("bottle");     BufferedImage img = ImageIO.read(new File(path)); }   public static BufferedImage resize(BufferedImage img, int newW, int newH) {       int w = img.getWidth();       int h = img.getHeight();       BufferedImage dimg = new BufferedImage(newW, newH, img.getType());       Graphics2D g = dimg.createGraphics();       g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,     RenderingHints.VALUE_INTERPOLATION_BILINEAR);       g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);       g.dispose();       return dimg;   }   
like image 412
user1060187 Avatar asked Feb 23 '12 16:02

user1060187


People also ask

How do I resize an image in a swing?

Image , resizing it doesn't require any additional libraries. Just do: Image newImage = yourImage. getScaledInstance(newWidth, newHeight, Image.

How do you scale an image in Java?

The simplest way to scale an image in Java is to use the AffineTransformOp class. You can load an image into Java as a BufferedImage and then apply the scaling operation to generate a new BufferedImage. You can use Java's ImageIO or a third-party image library such as JDeli to load and save the image.


2 Answers

Updated answer

I cannot recall why my original answer worked but having tested it in a separate environment, I agree, the original accepted answer doesn't work (why I said it did I cannot remember either). This, on the other hand, did work:

public static BufferedImage resize(BufferedImage img, int newW, int newH) {      Image tmp = img.getScaledInstance(newW, newH, Image.SCALE_SMOOTH);     BufferedImage dimg = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB);      Graphics2D g2d = dimg.createGraphics();     g2d.drawImage(tmp, 0, 0, null);     g2d.dispose();      return dimg; }   
like image 142
Ocracoke Avatar answered Sep 17 '22 18:09

Ocracoke


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.

like image 42
coobird Avatar answered Sep 19 '22 18:09

coobird