0

I'm using the approach I found in this answer to create zoom in/out functionality for an image labeling application. However, despite dealing with relatively high resolution images (order of 1500x1500 pixels), the scaling method for zoom leads to pixelation. I checked the image in Windows Photo Viewer and I could definitely zoom in without much pixelation until much more than the 200% zoom I have in my code.

If I resize only compared to the original image (i.e. not cascading each resize call), this works just fine (this is what's in the code snippet). But in order to do this, I lose any drawing functionality. Basically, I would like to be able to do the following:

  1. Load an image
  2. Draw lines/circles/shapes/etc on it
  3. Zoom in (maintaining any drawing on the image)
  4. Draw more
  5. Zoom in/out at will for whatever reasons I need

In summary, the resizing-for-zoom works. It's preserving any additional drawing at each stage of the zoom that I can't seem to get.

Here's a code snippet of my resize function (heavily influenced by that answer cited before):

private void resizeImage()
{
    int newImgW = (int)(this.zoom * this.imgW);
    int newImgH = (int)(this.zoom * this.imgH);
    BufferedImage resized = new BufferedImage(newImgW, newImgH, originalImage.getType());
    graphics2D = resized.createGraphics();
    graphics2D.drawImage(this.originalImage, 0, 0, newImgW, newImgH, null);
    this.image = resized;
    repaint();
}
Community
  • 1
  • 1
marcman
  • 3,233
  • 4
  • 36
  • 71
  • 1
    Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer Jul 01 '15 at 03:46
  • 1
    One step zooming is never a good idea, you need to step down the scaling for better results, [for example](http://stackoverflow.com/questions/14115950/quality-of-image-after-resize-very-low-java/14116752#14116752) – MadProgrammer Jul 01 '15 at 03:47
  • 3
    Also what is the image you are resizing? If it is not the original image, but one that you've already resized to zoom out, then zooming in on that will give you a pixelated image. You have to resize from the ORIGINAL image as per the code you linked too. – Dijkgraaf Jul 01 '15 at 03:48
  • @MadProgrammer thank you for the step down scaling link. I'll check that out. I can't share the code though, because although I'm writing it and it's hacky and for my use primary, I'm bound by institutional restrictions. – marcman Jul 01 '15 at 03:59
  • However @Dijkgraaf's pointed out my obvious mistake which has greatly improved the performance. Thank you both! – marcman Jul 01 '15 at 03:59
  • See also this [Q&A](http://stackoverflow.com/q/4216123/230513). – trashgod Jul 01 '15 at 10:32
  • @MadProgrammer: So I'm drawing on the image (remember this is for labeling purposes). Given that I have to resize based on the original image, how do I resize without losing the labels? Is there someway to have all the drawing be done on a mask? That way I can just resize the mask? – marcman Jul 01 '15 at 17:45
  • Draw the label to the resized image only – MadProgrammer Jul 01 '15 at 20:47
  • Well that's the easiest solution. My concern is the scenario where you label at one zoom level, then zoom in to see finer details and label at that zoom level and zoom back out...and so on – marcman Jul 01 '15 at 21:22

0 Answers0