Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing image in Java

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.

like image 399
Suhail Gupta Avatar asked May 05 '11 09:05

Suhail Gupta


People also ask

Can you resize an image in Java?

Core Java offers the following options for resizing images: Resize using java. awt. Graphics2D.

How do you resize graphics in Java?

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

What is resize in Java?

Java resizes an image, draw a new image. This example is more flexible (can customized background, color filtering, or RenderingHints) but needs extra coding to draw a newly resized image. BufferedImage originalImage = ImageIO. read(input); BufferedImage newResizedImage = new BufferedImage(width, height, BufferedImage.


4 Answers

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 the ReplicateScaleFilter class.

See the Javadoc for more info.

like image 104
Alba Mendez Avatar answered Oct 14 '22 17:10

Alba Mendez


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 
like image 20
Thomas Avatar answered Oct 14 '22 15:10

Thomas


Try this:

ImageIcon icon = new ImageIcon(UrlToPngFile);
Image scaleImage = icon.getImage().getScaledInstance(28, 28,Image.SCALE_DEFAULT);
like image 22
Harry Joy Avatar answered Oct 14 '22 15:10

Harry Joy


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;
        }
    }
like image 44
Radadiya Nikunj Avatar answered Oct 14 '22 17:10

Radadiya Nikunj