如何从java中的许多tile创建一个大图像文件?

时间:2021-03-28 01:22:47

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

我的程序生成10 x 10个图像,每个像素为3000x3000像素(目前保存到100个名为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.

我想将这100个图像组合成一个大图像,而不是将所有内容都加载到内存中。我的目标是创建一个30'000 * 30'000像素的大图像文件。

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)

我正在寻找一种方法来做到这一点,而不使用JAI(不能从公共maven存储库安装,我不明白为什么)

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

有没有办法用纯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 ?

我最初的想法是创建一个非常大的缓冲图像,从支持DataBuffer到磁盘上的文件。但我不确定这是可能的。有没有人这样做过?

4 个解决方案

#1


6  

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.

我想将这100个图像组合成一个大图像,而不是将所有内容都加载到内存中。我的目标是创建一个30'000 * 30'000像素的大图像文件。

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.

我相信JAI中有一个类可以做到这一点。无论您将JAI集成到项目中遇到什么问题,我都会坚持不懈而不是推出自己的版本。在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 ?

我最初的想法是创建一个非常大的缓冲图像,从支持DataBuffer到磁盘上的文件。但我不确定这是可能的。有没有人这样做过?

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.)
  • 由ByteBuffer而不是数组支持的DataBuffer(如果缓冲区是直接的,则可以映射到文件。)

  • 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.)
  • 类似于标准栅格的WritableRaster,但是使用我的DataBuffer实现(JDK作弊中的标准栅格,通过保持对后备数组的引用。在直接ByteBuffer的情况下没有数组,所以不幸的是你必须重新实现大多数Raster方法。)

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.)

我不建议扩展SampleModel,因为你的类不适用于JDK栅格(Java2D中的各种方法,包括Raster工厂方法切换SampleModel的类型,假设它是标准的一个。糟糕的设计恕我直言,但你不能太多除了遵循相同的模式,这样做。)

#2


1  

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.

我不知道是否可以将所有东西都加载到内存中。您可以将所有图像转储到未压缩的bmp,​​然后使用一些外部工具将其转换为jpg。

#3


1  

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).

如果您在使用公共maven存储库中的资源时遇到问题,可能需要使用Nexus,maven代理,并在那里手动添加JAI jar(并将其添加到您的存储库列表中)。

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.

选择这个解决方案的好处是你可以使用JAI,并且可以采用标准方式以maven方式使用非maven资源(所有javax库)。

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.

不要在你自己周围摆弄这个问题,由于所涉及的所有压缩并且在磁盘上处理BMP,成像问题很复杂,因为你的图像尺寸(大约100 * 30MB = 3GB)可能不是最佳的也不是快速的。

#4


1  

The solution is BigBufferedImage. It can load and store much larger images than the memory limit. The trick is that its buffer is replaced with a file based implementation of DataBuffer. It stores the decoded raw image data in files. No RAM is used. This can prevent OutOfMemoryException.

解决方案是BigBufferedImage。它可以加载和存储比内存限制更大的图像。诀窍是它的缓冲区被基于文件的DataBuffer实现替换。它将解码的原始图像数据存储在文件中。没有使用RAM。这可以防止OutOfMemoryException。

For more information about big image handling read this article.

有关大图像处理的更多信息,请阅读本文。

#1


6  

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.

我想将这100个图像组合成一个大图像,而不是将所有内容都加载到内存中。我的目标是创建一个30'000 * 30'000像素的大图像文件。

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.

我相信JAI中有一个类可以做到这一点。无论您将JAI集成到项目中遇到什么问题,我都会坚持不懈而不是推出自己的版本。在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 ?

我最初的想法是创建一个非常大的缓冲图像,从支持DataBuffer到磁盘上的文件。但我不确定这是可能的。有没有人这样做过?

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.)
  • 由ByteBuffer而不是数组支持的DataBuffer(如果缓冲区是直接的,则可以映射到文件。)

  • 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.)
  • 类似于标准栅格的WritableRaster,但是使用我的DataBuffer实现(JDK作弊中的标准栅格,通过保持对后备数组的引用。在直接ByteBuffer的情况下没有数组,所以不幸的是你必须重新实现大多数Raster方法。)

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.)

我不建议扩展SampleModel,因为你的类不适用于JDK栅格(Java2D中的各种方法,包括Raster工厂方法切换SampleModel的类型,假设它是标准的一个。糟糕的设计恕我直言,但你不能太多除了遵循相同的模式,这样做。)

#2


1  

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.

我不知道是否可以将所有东西都加载到内存中。您可以将所有图像转储到未压缩的bmp,​​然后使用一些外部工具将其转换为jpg。

#3


1  

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).

如果您在使用公共maven存储库中的资源时遇到问题,可能需要使用Nexus,maven代理,并在那里手动添加JAI jar(并将其添加到您的存储库列表中)。

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.

选择这个解决方案的好处是你可以使用JAI,并且可以采用标准方式以maven方式使用非maven资源(所有javax库)。

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.

不要在你自己周围摆弄这个问题,由于所涉及的所有压缩并且在磁盘上处理BMP,成像问题很复杂,因为你的图像尺寸(大约100 * 30MB = 3GB)可能不是最佳的也不是快速的。

#4


1  

The solution is BigBufferedImage. It can load and store much larger images than the memory limit. The trick is that its buffer is replaced with a file based implementation of DataBuffer. It stores the decoded raw image data in files. No RAM is used. This can prevent OutOfMemoryException.

解决方案是BigBufferedImage。它可以加载和存储比内存限制更大的图像。诀窍是它的缓冲区被基于文件的DataBuffer实现替换。它将解码的原始图像数据存储在文件中。没有使用RAM。这可以防止OutOfMemoryException。

For more information about big image handling read this article.

有关大图像处理的更多信息,请阅读本文。