How to create a big image file from many tiles in java?

邮差的信 提交于 2019-12-18 05:55:37

问题


My program produces 10 x 10 tiles images of 3000x3000 pixel, one by one (currently saved to 100 files named image_x_y.jpg)

I want to assemble these 100 images into one big image, without loading everything in memory. My goal is to create one big image file, of 30'000 * 30'000 pixels.

I'm looking for a way to do this without using JAI (which cannot be installed from public maven repositories, I don't understand why)

Is there a way to do this with pure java2D ? Or does another library exist, able to handle this ?

My original idea was to create a very big buffered image, from a DataBuffer backed to a file on the disk. But i'm not sure that this is possible. Did anybody ever do this ?


回答1:


I want to assemble these 100 images into one big image, without loading everything in memory. My goal is to create one big image file, of 30'000 * 30'000 pixels.

I believe there is a class in JAI that does this. Whatever problems you are having with integrating JAI into your project I would persevere with that rather than roll your own version. There is nothing like this in Java2D.

My original idea was to create a very big buffered image, from a DataBuffer backed to a file on the disk. But i'm not sure that this is possible. Did anybody ever do this ?

Yes I have written an incomplete implementation of this. It consists of

  • A DataBuffer that is backed by a ByteBuffer instead of an array (if the buffer is direct it can be mapped to a file.)
  • A WritableRaster similar to the standard rasters but using my implementation of DataBuffer (the standard rasters in the JDK cheat by holding a reference to the backing array. There is no array in the case of a direct ByteBuffer so unfortunately you must re-implement most Raster methods.)

I do not recommend extending SampleModel because your class will not work with the JDK rasters (various methods in Java2D including the Raster factory methods switch on the type of the SampleModel assuming it is one of the standard ones. Bad design IMHO but not much you can do about it except follow the same pattern.)




回答2:


I don't know if it is possible without loading everything into memory. You can dump all your images to an uncompressed bmp, and then use some external tool to convert it to jpg.




回答3:


If you have trouble using a resource from a public maven repository you might want to use Nexus, a maven proxy, and manually add the JAI jar there (and add that to your list of repositories).

The advantage of chosing this solution is that you would have JAI, and would have a standard way to use non-maven resources (all the javax libraries) in a maven way.

Don't fiddle with this around yourself, the imaging matter is complex due to all the compression involved and dealing with BMPs on disk is, given your image sizes (about 100 * 30MB = 3GB) probably not optimal nor fast.



来源:https://stackoverflow.com/questions/3412805/how-to-create-a-big-image-file-from-many-tiles-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!