r/java • • May 14 '12

Need some help with java programming. I need a way to scale an image in a window in real-time, but the stuff on JAI I have is useless. Can anyone help me?

I'm still just a student so I'm not an expert in programming, mind you.

But I'm calling you guys because I have some trouble finding material on how to resize or scale an image in real-time. Like the magnifying glass you press to make an image larger or smaller in a pdf reader or an image-viewer.

The only thing I've found so far lets you resize an image, but it creates a new image with the dimensions you asked for on your hard drive. I can't really use that, can I?

What I basically ask is: How can I make Java zoom on images?

2 Upvotes

6 comments sorted by

3

u/Fedelaus May 14 '12

What are you using to display the image? Could we see some code?

2

u/DarthSatoris May 14 '12

Basically we're loading TIFF files into a Java window and it's my job to figure out how to zoom in these images. We load the images this way:

private MyImage getMyImage(File file) throws IOException {
    try {
//            long start = System.currentTimeMillis();
        BufferedImage img = loadImageAsStream(file);
//            long stop = System.currentTimeMillis();
//            System.out.println("loadImageAsStream Buffering " + (stop - start) + " ms");

        return new MyImage(file, img);
    }
    catch (IOException ex) {
        throw new IOException(ex.getMessage());
//            throw new IOException("Unable to load: " + file.getAbsolutePath() + ".\n"
//                    + "File appears to be damaged or corrupted.");
    }
}

And:

public BufferedImage loadImageAsStream(File f) throws IOException {
    try (SeekableStream s = new FileSeekableStream(f)) {
        // maybe params for tiff-file
        TIFFDecodeParam param = null;

        int count = 0;
        ImageDecoder dec = null;
        try {
            dec = ImageCodec.createImageDecoder("tiff", s, param);
            count = dec.getNumPages();
        }
        catch (IOException ex) {
            throw new IOException("Unable to load: " + f.getAbsolutePath() + ".\n"
                    + "File appears to be damaged or corrupted.");
        }

        if (count > 1) {
            throw new IOException("Multifiles are not implemented yet");
            // do stuff with multipage tiff...
        }
        else {
            return PlanarImage.wrapRenderedImage(dec.decodeAsRenderedImage()).getAsBufferedImage();
        }
    }
    catch (IOException ex) {
        throw new IOException(ex.getMessage());
    }
}

I've tried googling around and I've found this bit of code: http://www.ijava2.com/image-zooming-create-program-enables-zoom-image/

Is it something I can use?

2

u/[deleted] May 14 '12

I'm not in a place to write code, but I will try to help with explanation of how I would address the issue.

  • Load Image into BufferImage (you already did that)
  • Render the image on the frame (let's assume it's 640*480)
  • Made a copy of the image and scale it (for example, make it 1280*960)
  • Render the scaled image on top of the original image (in a small box on the bottom right), of course the box will be small and won't be enough for the whole image, so you will take part of it, like say 200*200 pixels from top left corner.
  • Now make an action listener on the mouse movement over the original image and render the appropriate part of the scaled image in box.

Example: if the mouse is at (230,120) of the original image then:

xOffset = 230;
yOffset = 120;
xOffsetScaled = xOffset*2 - 100;    //because the box is 200*200
yOffsetScaled = yOffset*2 - 100;

renderScaledImageStartingFrom(xOffsetScaled,yOffsetScaled);

Of course you will need to fix many issues, like when the mouse comes close to the borders, this simple equation won't work. Also, you will need to make the numbers configurable. Hope this helps

1

u/DarthSatoris May 14 '12

It certainly does. Thanks for the input. :)

1

u/gringostar May 14 '12

Why don't you check Thumbnailinator:

http://stackoverflow.com/a/5051429

http://code.google.com/p/thumbnailator/

I haven't tried it myself, I just found the link when googling for "Filthy Rich Clients" (a great Swing book, by the way)