使用 Java 中的 ImageIO 编写 TIFF 的平铺输出 [英] Write tiled output of TIFF, using ImageIO in Java

查看:43
本文介绍了使用 Java 中的 ImageIO 编写 TIFF 的平铺输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大量的帧需要放在一起形成一个更大的图像(如马赛克).已知图像所需的位置.

What I have is a a large number of frames that need to be placed together in a larger image (like a mosaic). The required positions of the images are known.

图像数量非常多,因此将它们全部加载到内存中充其量是不切实际的.

There are a very large number of images so loading them all into memory is impractical at best.

基于这里的其他一些答案,我能够覆盖 RenderedImage 中的方法(特别是 getData(rect))以加载适当的数据并返回它.

Based on some other answers here I was able to override the methods in RenderedImage (specifically getData(rect)) to load in the appropriate data and return it.

这工作得很好,但是图像编写器总是调用 getData 并请求数据行.在我看来,我应该能够更改 ImageWriterParam 来调用单个图块,但是当我尝试 write 函数时仍然调用 getData 中的一行.

This works just fine, however the image writer is always calling getData and requesting rows of data. It seems to me I should be able to change the ImageWriterParam to call for individual tiles instead, but when I tried the write function still calls for a single line from getData.

我如何强制它使用图块并调用 getTile.

How can I force this to use tiles and call getTile instead.

BufferedOutputStream bos=null;
try {
   bos = new BufferedOutputStream(new FileOutputStream(new File("test2.tiff")));

   ImageWriter writer =(ImageWriter) ImageIO.getImageWritersBySuffix("tif").next();

   ImageOutputStream ios=null

   ios = ImageIO.createImageOutputStream(bos); 

   writer.setOutput(ios);
   ImageWriteParam param = writer.getDefaultWriteParam();
   param.setTilingMode(ImageWriteParam.MODE_DEFAULT);

   RenderedImage mosaic = new MosaicImage(imageFiles[]);

   writer.write(null,new IIOImage(mosaic,null,null),param);

    } catch (FileNotFoundException ex) {

    }

注意我可以使用 param.setTilingMode(ImageWriteParam.MODE_EXPLICIT);setTiling(w,h,xoff,yoff);

Note I can use param.setTilingMode(ImageWriteParam.MODE_EXPLICIT); and setTiling(w,h,xoff,yoff);

然而,在使用这个时,writer.write 仍然在我的图像中调用了 getData(rect),并且非常烦人的是没有调用 writer.write 指定大小的矩形代码>w,h.它调用一个大小不同的矩形,随机数量不同(可能来自某物)

However when using this, writer.write still calls getData(rect) in my image, and very annoyingly does not call for a rectangle of the size specified by w,h. It calls a rectangle of a size that is different by some random amount(probably comes from something)

例如,如果我使用 setTiling(100,100,0,0);

人们会期望,即使它不从图像中调用 getTile,传递给 getData 的 Rectangle 应该是 (0,0,100,100) 但它传递的 Rectangle (0,0,96,96) 不是 的倍数图像宽度或其他任何我能想到的.

one would expect that even if it does not call getTile from the image, the Rectangle passed to getData should be (0,0,100,100) but instead it passes Rectangle (0,0,96,96) which is not a multiple of the image width or anything else i can think of.

感谢您的帮助

推荐答案

如果您在 TileWidth 和 TileLength 字段下阅读 TIFF 6.0 规范,您会找到文本

If you read up on the TIFF 6.0 spec under the TileWidth and TileLength fields, you will find the text

TileWidth 必须是 16 的倍数.这个限制改进了在某些图形环境中提高性能并增强兼容性使用 JPEG 等压缩方案.

TileWidth must be a multiple of 16. This restriction improves performance in some graphics environments and enhances compatibility with compression schemes such as JPEG.

和 TileLength 类似.100x100 不能被 16 整除,但 96x96 可以,我敢打赌这是 TIFF 编码器尽力满足您的要求.

And similar for TileLength. 100x100 isn't divisible by 16, but 96x96 is, which I bet is the TIFF encoder trying its best to fullfill your request.

这篇关于使用 Java 中的 ImageIO 编写 TIFF 的平铺输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆