Zoom in and out of images in Java

43,267

Solution 1

You can easily achieve this by using scale transforms on the original image. Assuming your the current image width newImageWidth, and the current image height newImageHeight, and the current zoom level zoomLevel, you can do the following:

int newImageWidth = imageWidth * zoomLevel;
int newImageHeight = imageHeight * zoomLevel;
BufferedImage resizedImage = new BufferedImage(newImageWidth , newImageHeight, imageType);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, newImageWidth , newImageHeight , null);
g.dispose();

Now, replace the original image, originalImage, in your display area by resizedImage.

Solution 2

You can also use it as follows :

public class ImageLabel extends JLabel{
    Image image;
    int width, height;

    public void paint(Graphics g) {
        int x, y;
        //this is to center the image
        x = (this.getWidth() - width) < 0 ? 0 : (this.getWidth() - width);
        y = (this.getHeight() - width) < 0 ? 0 : (this.getHeight() - width);

        g.drawImage(image, x, y, width, height, null);
    }

    public void setDimensions(int width, int height) {
        this.height = height;
        this.width = width;

        image = image.getScaledInstance(width, height, Image.SCALE_FAST);
        Container parent = this.getParent();
        if (parent != null) {
            parent.repaint();
        }
        this.repaint();
    }
}

Then you can put it to your frame and with the method that zooms with a zooming factor, for which I used percent values.

public void zoomImage(int zoomLevel ){
    int newWidth, newHeight, oldWidth, oldHeight;
    ImagePreview ip = (ImagePreview) jLabel1;
    oldWidth = ip.getImage().getWidth(null);
    oldHeight = ip.getImage().getHeight(null);

    newWidth = oldWidth * zoomLevel/100;
    newHeight = oldHeight * zoomLevel/100;

    ip.setDimensions(newHeight, newWidth);
}
Share:
43,267
Andy
Author by

Andy

A teenager with a passion for all things computers! I have cerebral palsy so computers have been my best, and I've got the most out of them. I've taught myself AutoIt, Java, and a bit of most web languages, including PHP and HTML, as now own about six domains and I'm in the process of starting my own business. I don't confess to know everything about anything and I know I have A LOT to learn but I do what I can and try to help out where I can. I think I'm the biggest 'Guru' of the family!

Updated on October 18, 2020

Comments

  • Andy
    Andy over 3 years

    I think the question is quite self-explanatory. I want to implement a simple zoom function using a JSlider like in Windows Live Photo Gallery for instance.

    I've had a quick look online but all the code I've tried to use appears to have errors when I copy it into Eclipse. I don't really want to use a third-party library either as the application may be sold under a company name. Plus, I'm beginning to realise that there may be some safety precautions required in order to prevent errors, but I do not know what these will be.

    So, if anybody can provide me with some Java code to zoom in and out of images it would be greatly appreciated.

    P.S. I plan to use the Image as an ImageIcon inside of a JLabel which will be added to a JScrollPane.

  • Andy
    Andy about 12 years
    Thanks GETah, I already had a method to do this in my code, it just didn't click with me that it could serve for this perpose as well, but as I've just tested - it obviously can!
  • Andy
    Andy about 12 years
    Ah wait, this method seems to be affecting the transparency of the image (the transparent bits are black), do you have any idea why and more over how to solve the issue?
  • GETah
    GETah about 12 years
    @Andy It seems that using AffineTransform should preserve the image transparency... check this out stackoverflow.com/a/2176977/782719