使用JAI将swing组件写入大TIFF图像(Write swing component to large TIFF image using JAI)

编程入门 行业动态 更新时间:2024-10-26 04:24:25
使用JAI将swing组件写入大TIFF图像(Write swing component to large TIFF image using JAI)

我有一个大的swing组件写入TIFF。 组件太大而无法在内存中加载TIFF,因此我需要制作一个大的BufferedImage,它由基于磁盘的WritableRaster(如此处所述)支持或使用JAI。

除了项目的完全混乱之外,JAI似乎是更好的答案。

鉴于此,有人可以概述将我的swing组件写入平铺TIFF而不会耗尽内存的步骤吗?

图像大小可能是10000x700

理想情况下,我会创建某种基于磁盘的映像,并将组件的一部分写入其中,每次写入都会刷新到磁盘。

编辑

我想我可以用ImageWriter做到这一点,但是当我调用时,我得到一个NoSuchElementException:

ImageWriter imageWriter = ImageIO.getImageWritersByFormatName("tif").next();

我的classpath上有jai_code.jar和jai_core.jar罐子,还有什么我需要做的吗?

编辑我可以使用JAI创建一个非常大的TIFF,但JAI不支持TIFF压缩,因此文件是92 MB。

如果我安装JAI-ImageIO,我可以使用ImageWriter创建一个压缩的TIFF,但只能使用Raster或BufferedImage,而我没有足够的内存。

有没有办法做两步法,使用JAI创建大TIFF,然后压缩大TIFF而不将整个内容加载到内存中?

I have a large swing component to write to TIFF. The component is too large to load the TIFF in memory, so I either need to make a big BufferedImage which is backed by a disk-based WritableRaster (as mentioned here) or use JAI.

JAI seems like the better answer, aside from the utter confusion of the project.

Given that, can someone outline steps for writing my swing component to a tiled TIFF without running out of Memory?

Image size will be maybe 10000x700

Ideally I would create some sort of disk-based image, and write parts of the component to it, each write being flushed to disk.

EDIT

I think I could do this with an ImageWriter, however I'm getting a NoSuchElementException when I call:

ImageWriter imageWriter = ImageIO.getImageWritersByFormatName("tif").next();

I have the jai_code.jar and jai_core.jar jars on my classpath, is there something else I need to do?

EDIT I can create a very large TIFF using JAI, but JAI doesn't support TIFF compression, so the file is 92 MB.

If I install JAI-ImageIO, I can create a compressed TIFF Using an ImageWriter, but only from a Raster or BufferedImage, which I don't have enough memory for.

Is there some way to do a two-step approach, use JAI to create the large TIFF, then compress the large TIFF without loading the whole thing into memory?

最满意答案

我不得不加载并存储一个大的tiff(59392x40192px)与JAI。 我的解决方案是:TiledImages。

我使用了TiledImage,因为我需要tile和subimages。 要使用TiledImage效率,您应该使用您喜欢的磁贴大小来构建它。 JAI使用TileCache,因此当不需要时,整个Image都不会在内存中。

要在文件中编写TiledImage,请使用选项“writeTiled”(避免使用OutOfMemory,因为它逐个tile地写入):

public void storeImage(TiledImage img, String filepath) { TIFFEncodeParam tep = new TIFFEncodeParam(); //important to avoid OutOfMemory tep.setTileSize(256, 256); tep.setWriteTiled(true); //fast compression tep.setCompression(TIFFEncodeParam.COMPRESSION_PACKBITS); //write file JAI.create("filestore", img, filepath, "TIFF", tep); }

它适用于高达690mb(压缩)的图像,对于尚未测试的较大图像。

但是,如果你正在使用32位WinXP,你可能无法拥有更多的1280m HeapSpace大小,这仍然是Java VM的限制。

我的TiledImage是使用我的图像源数据中的IndexedColorModel构建的:

//here you create a ColorModel for your Image ColorModel cm = source.createColorModel(); //then create a compatible SampleModel, with the tilesize SampleModel sm = cm.createCompatibleSampleModel(tileWidth,tileHeight); TiledImage image = new TiledImage(0, 0, imageWidth, imageHeight, 0, 0, sm, cm);

I had to load and store a large tiff (59392x40192px) with JAI. My solution is: TiledImages.

I have used a TiledImage because I need tiles and subimages. To use the TiledImage efficient you should construct it with your prefered tile size. JAI uses a TileCache so not the whole Image will be in memory, when it's not needed.

To write the TiledImage in a File use the option "writeTiled" (avoid OutOfMemory because it writes tile by tile):

public void storeImage(TiledImage img, String filepath) { TIFFEncodeParam tep = new TIFFEncodeParam(); //important to avoid OutOfMemory tep.setTileSize(256, 256); tep.setWriteTiled(true); //fast compression tep.setCompression(TIFFEncodeParam.COMPRESSION_PACKBITS); //write file JAI.create("filestore", img, filepath, "TIFF", tep); }

It works fine with images up to 690mb (compressed), for larger images i haven't tested yet.

But if you are working on WinXP 32-bit you may not able to have more as 1280m HeapSpace size, this is still a limit of Java VM.

My TiledImage is build with a IndexedColorModel from my image-source data:

//here you create a ColorModel for your Image ColorModel cm = source.createColorModel(); //then create a compatible SampleModel, with the tilesize SampleModel sm = cm.createCompatibleSampleModel(tileWidth,tileHeight); TiledImage image = new TiledImage(0, 0, imageWidth, imageHeight, 0, 0, sm, cm);

更多推荐

本文发布于:2023-07-29 14:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1316754.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组件   图像   TIFF   JAI   swing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!