I have a PNG image and I want to resize it. How can I do that? Though I have gone through this I can't understand the snippet.
Asked
Active
Viewed 1.8e+01k times
41
-
1What exactly do you not understand of that snippet? – Heisenbug May 05 '11 at 10:02
-
1Do you want to generate a resized version of the original png, or just paint a resized version somewhere in your UI? – dcn May 05 '11 at 10:03
7 Answers
90
If you have an java.awt.Image
, resizing it doesn't require any additional libraries. Just do:
Image newImage = yourImage.getScaledInstance(newWidth, newHeight, Image.SCALE_DEFAULT);
Obviously, replace newWidth
and newHeight
with the dimensions of the specified image.
Notice the last parameter: it tells the runtime the algorithm you want to use for resizing.
There are algorithms that produce a very precise result, however these take a large time to complete.
You can use any of the following algorithms:
Image.SCALE_DEFAULT
: Use the default image-scaling algorithm.Image.SCALE_FAST
: Choose an image-scaling algorithm that gives higher priority to scaling speed than smoothness of the scaled image.Image.SCALE_SMOOTH
: Choose an image-scaling algorithm that gives higher priority to image smoothness than scaling speed.Image.SCALE_AREA_AVERAGING
: Use the Area Averaging image scaling algorithm.Image.SCALE_REPLICATE
: Use the image scaling algorithm embodied in theReplicateScaleFilter
class.
See the Javadoc for more info.

Matthias Braun
- 32,039
- 22
- 142
- 171

Alba Mendez
- 4,432
- 1
- 39
- 55
-
4The doc link refers to older version use this instead: http://download.oracle.com/javase/6/docs/api/java/awt/Image.html – Harry Joy May 05 '11 at 10:57
23
We're doing this to create thumbnails of images:
BufferedImage tThumbImage = new BufferedImage( tThumbWidth, tThumbHeight, BufferedImage.TYPE_INT_RGB );
Graphics2D tGraphics2D = tThumbImage.createGraphics(); //create a graphics object to paint to
tGraphics2D.setBackground( Color.WHITE );
tGraphics2D.setPaint( Color.WHITE );
tGraphics2D.fillRect( 0, 0, tThumbWidth, tThumbHeight );
tGraphics2D.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR );
tGraphics2D.drawImage( tOriginalImage, 0, 0, tThumbWidth, tThumbHeight, null ); //draw the image scaled
ImageIO.write( tThumbImage, "JPG", tThumbnailTarget ); //write the image to a file

Thomas
- 87,414
- 12
- 119
- 157
10
Try this:
ImageIcon icon = new ImageIcon(UrlToPngFile);
Image scaleImage = icon.getImage().getScaledInstance(28, 28,Image.SCALE_DEFAULT);

Harry Joy
- 58,650
- 30
- 162
- 207
-
2IIRC, this way (getSclaedInstance()) is not advised due to its lack of performance (I think I read that, a long time ago, in "Filthy Rich Clients"). – jfpoilpret May 05 '11 at 10:22
-
1There's no need to use an `ImageIcon`, you can directly do `ImageIO.read(...)`. – Alba Mendez May 07 '11 at 14:59
-
1@AndrewThompson Your link seems to be out-of-date. The new location is: https://community.oracle.com/docs/DOC-983611 – Brian Matthews Dec 30 '17 at 23:13
-
1Thanks @BrianMatthews. Unfortunately I'm unable to edit the original comment so I'll update it here: For more info. see [The Perils of Image.getScaledInstance()](https://community.oracle.com/docs/DOC-983611). – Andrew Thompson Dec 31 '17 at 00:37
4
Resize image with high quality:
private static InputStream resizeImage(InputStream uploadedInputStream, String fileName, int width, int height) {
try {
BufferedImage image = ImageIO.read(uploadedInputStream);
Image originalImage= image.getScaledInstance(width, height, Image.SCALE_DEFAULT);
int type = ((image.getType() == 0) ? BufferedImage.TYPE_INT_ARGB : image.getType());
BufferedImage resizedImage = new BufferedImage(width, height, type);
Graphics2D g2d = resizedImage.createGraphics();
g2d.drawImage(originalImage, 0, 0, width, height, null);
g2d.dispose();
g2d.setComposite(AlphaComposite.Src);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(resizedImage, fileName.split("\\.")[1], byteArrayOutputStream);
return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
} catch (IOException e) {
// Something is going wrong while resizing image
return uploadedInputStream;
}
}

Radadiya Nikunj
- 988
- 11
- 10
-
3why are you setting rendering hints after drawImage() and dispose()? – ykaganovich Jun 07 '19 at 17:57
1
Simple way in Java
public void resize(String inputImagePath,
String outputImagePath, int scaledWidth, int scaledHeight)
throws IOException {
// reads input image
File inputFile = new File(inputImagePath);
BufferedImage inputImage = ImageIO.read(inputFile);
// creates output image
BufferedImage outputImage = new BufferedImage(scaledWidth,
scaledHeight, inputImage.getType());
// scales the input image to the output image
Graphics2D g2d = outputImage.createGraphics();
g2d.drawImage(inputImage, 0, 0, scaledWidth, scaledHeight, null);
g2d.dispose();
// extracts extension of output file
String formatName = outputImagePath.substring(outputImagePath
.lastIndexOf(".") + 1);
// writes to output file
ImageIO.write(outputImage, formatName, new File(outputImagePath));
}

Pritam Banerjee
- 17,953
- 10
- 93
- 108

Shahid Hussain Abbasi
- 2,508
- 16
- 10
1
int newHeight = 150;
int newWidth = 150;
holder.iv_arrow.requestLayout();
holder.iv_arrow.getLayoutParams().height = newHeight;
holder.iv_arrow.getLayoutParams().width = newWidth;
holder.iv_arrow.setScaleType(ImageView.ScaleType.FIT_XY);
holder.iv_arrow.setImageResource(R.drawable.video_menu);

MEGHA DOBARIYA
- 1,622
- 9
- 7
0
Design jLabel first:
JLabel label1 = new JLabel("");
label1.setHorizontalAlignment(SwingConstants.CENTER);
label1.setBounds(628, 28, 169, 125);
frame1.getContentPane().add(label1); //frame1 = "Jframe name"
Then you can code below code:
ImageIcon imageIcon1 = new ImageIcon(new ImageIcon("add location url").getImage().getScaledInstance(100, 100, Image.SCALE_DEFAULT)); //100, 100 add your own size
label1.setIcon(imageIcon1);

Ruwan Pathirana
- 41
- 4