Resizing an image in swing
Asked Answered
R

4

5

I have snippet of code that I am using for the purpose of resizing an image to a curtain size (I want to change the resolution to something like 200 dpi). Basically the reason I need it is because I want to display the image that the user have picked (somewhat large) and then if the user approves I want to display the same image in a different place but using a smaller resolution. Unfortunately, if I give it a large image nothing appears on the screen. Also, if I change

imageLabel.setIcon(newIcon); 

to

imageLabel.setIcon(icon); 

I get the image to display but not in the correct resolution that's how I know that I have a problem inside this snipper of code and not somewhere else.

Image img = icon.getImage();

BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
boolean myBool = g.drawImage(img, 0, 0, 100, 100, null);
System.out.println(myBool);
ImageIcon newIcon = new ImageIcon(bi);
imageLabel.setIcon(newIcon);
submitText.setText(currentImagePath);
imageThirdPanel.add(imageLabel);
Ramble answered 27/11, 2011 at 6:33 Comment(4)
1) Did you have a question? 2) Note that everything up to ImageIcon newIcon = new ImageIcon(bi); is AWT, not Swing. 3) It is 'Swing', not 'swing' 4) For better help sooner, post an SSCCE.Shopping
Okay. I'm not displaying the whole code because it's too long. Also, I'm not sure why you say it's Swing and not swing (that's what I got from the auto complete when I entered the tag - I don't see how that is my fault). I think it's SSCCE. Like I said the rest of the code works I'm sure there is a problem in this snippet of code. Probably when I draw the Image, but I'm not sure what.Ramble
"I think it's SSCCE." You think wrong. Please read the document.Shopping
Why don't you use Image.getScaledInstance()?Sustenance
P
6

Here is my solution:

    private BufferedImage resizeImage(BufferedImage originalImage, int width, int height, int type) throws IOException {  
        BufferedImage resizedImage = new BufferedImage(width, height, type);  
        Graphics2D g = resizedImage.createGraphics();  
        g.drawImage(originalImage, 0, 0, width, height, null);  
        g.dispose();  
        return resizedImage;  
    }  
Plessor answered 27/11, 2011 at 8:32 Comment(0)
T
9

You don't really have to care about the details of scaling images. The Image class has already a method getScaledInstance(int width, int height, int hints) designed for this purpose. Java documentation says:

Creates a scaled version of this image. A new Image object is returned which will render the image at the specified width and height by default. The new Image object may be loaded asynchronously even if the original source image has already been loaded completely. If either the width or height is a negative number then a value is substituted to maintain the aspect ratio of the original image dimensions.

And you can use it like this:

// Scale Down the original image fast
Image scaledImage = imageToScale.getScaledInstance(newWidth, newHighth, Image.SCALE_FAST);
// Repaint this component
repaint();

Check this for a complete example.

Turanian answered 27/11, 2011 at 8:39 Comment(1)
If a proportional scale is needed pass -1 as argument for the measure that should follow the other. For example, if you want to scale based on width only you would call something like anImage.getScaledInstance(150, -1, java.awt.Image.SCALE_FAST)Argali
P
6

Here is my solution:

    private BufferedImage resizeImage(BufferedImage originalImage, int width, int height, int type) throws IOException {  
        BufferedImage resizedImage = new BufferedImage(width, height, type);  
        Graphics2D g = resizedImage.createGraphics();  
        g.drawImage(originalImage, 0, 0, width, height, null);  
        g.dispose();  
        return resizedImage;  
    }  
Plessor answered 27/11, 2011 at 8:32 Comment(0)
E
1

Try this CODE to resize image :

public static Image scaleImage(Image original, int newWidth, int newHeight) {
    //do nothing if new and old resolutions are same
    if (original.getWidth() == newWidth && original.getHeight() == newHeight) {
        return original;
    }

    int[] rawInput = new int[original.getHeight() * original.getWidth()];
    original.getRGB(rawInput, 0, original.getWidth(), 0, 0, original.getWidth(), original.getHeight());
    int[] rawOutput = new int[newWidth * newHeight];
    // YD compensates for the x loop by subtracting the width back out
    int YD = (original.getHeight() / newHeight) * original.getWidth() - original.getWidth();
    int YR = original.getHeight() % newHeight;
    int XD = original.getWidth() / newWidth;
    int XR = original.getWidth() % newWidth;
    int outOffset = 0;
    int inOffset = 0;
    for (int y = newHeight, YE = 0; y > 0; y--) {
        for (int x = newWidth, XE = 0; x > 0; x--) {
            rawOutput[outOffset++] = rawInput[inOffset];
            inOffset += XD;
            XE += XR;
            if (XE >= newWidth) {
                XE -= newWidth;
                inOffset++;
            }
        }
        inOffset += YD;
        YE += YR;
        if (YE >= newHeight) {
            YE -= newHeight;
            inOffset += original.getWidth();
        }
    }
    return Image.createRGBImage(rawOutput, newWidth, newHeight, false);
}
Eschalot answered 27/11, 2011 at 7:17 Comment(0)
E
0

Another example is given here :

2D-Graphics/LoadImageandscaleit.htm">http://www.java2s.com/Tutorial/Java/0261_2D-Graphics/LoadImageandscaleit.htm

http://www.java2s.com/Code/JavaAPI/java.awt/ImagegetScaledInstanceintwidthintheightinthints.htm

Eschalot answered 27/11, 2011 at 10:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.