Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zoom in and out of images in Java

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.

like image 305
Andy Avatar asked Feb 18 '12 14:02

Andy


People also ask

How do you zoom in and zoom out in Java?

In Java, there is having a class “Robot”. Robot class having the “keyPress” and “ keyRelease ” methods. Using these methods we handle the zoom in and the zoom out. keyPress() method send an event to press the control, add or subtract key, and keyRelease() method send an event to release the pressed key.

How do I zoom out in Java?

View > Appearance > Zoom In (Ctrl+=) - increase the Zoom level. View > Appearance > Zoom Out (Ctrl+-) - decrease the Zoom level.

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.

How do you resize graphics in Java?

You can resize an image in Java using the getScaledInstance() function, available in the Java Image class. We'll use the BufferedImage class that extends the basic Image class. It stores images as an array of pixels.


2 Answers

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.

like image 80
GETah Avatar answered Sep 28 '22 19:09

GETah


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);
}
like image 26
Amanuel Nega Avatar answered Sep 28 '22 19:09

Amanuel Nega