java huge BufferedImage in JScrollPane

2019-07-18 09:38发布

I need to fit a huge image (BufferedImage to access colors etc) into a JScrollPane derived class. Nothing very hard until there.

The image is a JPG scan of an A3 sample of material, its size is 13030x20840 pixels, 24bits, and weights 12MB on disk -> about 800MB in RAM.

I embedded the BufferedImage into a Jpanel, which lies as the Scrollpane's view.

When I try to scroll/drag my image, it takes seconds to respond, so it is not very handy.

I need your help in order to know what I should do to render the scrollpane's scrolling and the image dragging as smooth and reactive as possible, but I easily figure out that with such an image it could be impossible.

I tried many options, such as double buffering, etc. but I'm quite new to Swing, then it is greatly possible I missed some simple solution.

If there is a need to change the BufferedImage by something else, or whatever, I'm receptive to any solution.

2条回答
戒情不戒烟
2楼-- · 2019-07-18 09:57

So, we have a large 13,030 x 20,840 pixel image.

if we break this image into 256 x 256 pixel tiles, we get a tile set that's 51 tiles across and 82 tiles down. The last tiles in the rows and columns will be partial images, since 256 doesn't go evenly into 13,030 and 20,840.

Let's assume our display window is 400 X 400 pixels. Let's also assume were starting in the upper left hand corner of the large image.

We take and make a 3 x 3 tile buffered image. This would be 768 x 768 pixels, which allows enough overlap for smooth scrolling. We take the first 3 tiles from the first row, the first 3 tiles from the second row, and the first 3 tiles from the third row to make our 3 x 3 tile buffered image.

Now, when we set up the horizontal and vertical scroll on the JScrollPane, we have to set the maximum value on the horizontal scroll to 13,030 and the maximum value on the vertical scroll to 20,840. This is so the user is kinetically aware that he is scrolling a large image.

Ok, we display the 3 x 3 tile buffered image. The user scrolls to the right to see more images. The user has scrolled 256 pixels.

The application now has to build a new 3 x 3 tile buffered image. We take the 2nd through 4th tiles from the first row, second row, and third row. We display this new buffered image. To the user, it looks like one huge image. To the application, a series of small 3 x 3 tile buffered images are displayed.

mKorbel has already created some code which builds and displays these smaller buffered images on the fly.

To deal with zooming, you can resize the large image outside of the Java application and create more than one tile set. This makes the Java application code more complicated, but much faster than trying to resize the smaller buffered images in the Java application.

查看更多
Ridiculous、
3楼-- · 2019-07-18 10:06

I found what I needed there :

very large image manipulation and tiling

A nice book, the sample shown covers that.

The Java JAI lib has a lot of features to handle these kind of problems, and is supported by Oracle, which means it is (theoretically) stable and sustainable.

Thanks to @BryanD !

查看更多
登录 后发表回答