Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scale a BufferedImage

Following the javadocs, I have tried to scale a BufferedImage without success here is my code:

BufferedImage image = MatrixToImageWriter.getBufferedImage(encoded); Graphics2D grph = image.createGraphics(); grph.scale(2.0, 2.0); grph.dispose(); 

I can't understand why it is not working, any help?

like image 559
Thiago Diniz Avatar asked Nov 18 '10 14:11

Thiago Diniz


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.

What is the difference between BufferedImage and image?

List, the difference between Image and BufferedImage is the same as the difference between List and LinkedList. Image is a generic concept and BufferedImage is the concrete implementation of the generic concept; kind of like BMW is a make of a Car. Show activity on this post. Image is an abstract class.


2 Answers

AffineTransformOp offers the additional flexibility of choosing the interpolation type.

BufferedImage before = getBufferedImage(encoded); int w = before.getWidth(); int h = before.getHeight(); BufferedImage after = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); AffineTransform at = new AffineTransform(); at.scale(2.0, 2.0); AffineTransformOp scaleOp =     new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR); after = scaleOp.filter(before, after); 

The fragment shown illustrates resampling, not cropping; this related answer addresses the issue; some related examples are examined here.

like image 73
trashgod Avatar answered Sep 19 '22 04:09

trashgod


Unfortunately the performance of getScaledInstance() is very poor if not problematic.

The alternative approach is to create a new BufferedImage and and draw a scaled version of the original on the new one.

BufferedImage resized = new BufferedImage(newWidth, newHeight, original.getType()); Graphics2D g = resized.createGraphics(); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,     RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.drawImage(original, 0, 0, newWidth, newHeight, 0, 0, original.getWidth(),     original.getHeight(), null); g.dispose(); 

newWidth,newHeight indicate the new BufferedImage size and have to be properly calculated. In case of factor scaling:

int newWidth = new Double(original.getWidth() * widthFactor).intValue(); int newHeight = new Double(original.getHeight() * heightFactor).intValue(); 

EDIT: Found the article illustrating the performance issue: The Perils of Image.getScaledInstance()

like image 24
charisis Avatar answered Sep 21 '22 04:09

charisis