【发布时间】:2017-03-24 20:39:00
【问题描述】:
我正在使用 com.sun.media.imageioimpl.plugins.tiff.TIFFPackBitsCompressor 来尝试对使用 PackBits 的 tiff 字节数组进行编码。我对这个类不熟悉,也没有找到很多关于如何使用它的例子。但是,在遵循 javadoc 时,每次尝试对数据进行编码时,我都会得到一个 NPE。据我所知,我的值都不是空值。在这一点上,我已经用多个值尝试了这些测试,但下面是我最近的迭代:
TIFFPackBitsCompressor pack = new TIFFPackBitsCompressor();
//bImageFromConvert is a 16-bit BufferedImage with all desired data.
short[] bufferHolder = ((DataBufferUShort) bImageFromConvert.getRaster().getDataBuffer()).getData();
//Since bImageFromConvert is 16-bits, the short array isn't the right length.
//The below conversion handles tihs issue
byte[] byteBuffer = convertShortToByte(bufferHolder);
//I'm not entirely sure what this int[] in the parameters should be.
//For now, it is a test int[] array containing all 1s
int[] testint = new int[byteBuffer.length];
Arrays.fill(testint, 1);
//0 offset. dimWidth = 1760, dimHeight = 2140. Not sure what that last param is supposed to be in layman's terms.
//npe thrown at this line.
int testOut = pack.encode(byteBuffer, 0, dimWidth, dimHeight, testint, 1);
有人对正在发生的事情有任何见解吗?另外,如果有的话,有没有人知道在 java 程序中使用 PackBits 对我的 TIFF 文件进行编码的更好方法?
如果有什么可以让我的问题更清楚,请告诉我。
谢谢!
【问题讨论】:
-
堆栈跟踪在哪里?无论如何,我认为您不应该自己使用压缩器,而是当您在
ImageWriteParam中将“PackBits”指定为压缩类型时,JAI ImageIO TIFF 插件(TIFFImageWriter)会使用它。你也可以在参数中传递一个压缩器实例,如果你先将它转换为TIFFImageWriteParam,但这对于插件不知道的自定义压缩更有用。
标签: java tiff javax.imageio jai