我正在使用com.sun.media.imageioimpl.plugins.tiff.TIFFPackBitsCompressor嘗試編碼我使用PackBits的tiff字節數組。我對這個班不熟悉,並沒有找到很多關於如何使用它的例子。但是,在遵循javadoc時,每次嘗試對數據進行編碼時都會收到NPE。就我所知,我的值都不爲空。我試着這些測試多個值,在這一點上,但下面是我最近的迭代:TIFFPackBitsCompressor - 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);
沒有人有任何見解,以發生了什麼?此外,如果可用,有沒有人知道一個更好的方法來編碼我的TIFF文件在Java程序中使用PackBits?
讓我知道是否有什麼可以讓我的問題更清晰。
謝謝!
堆棧跟蹤在哪裏?無論如何,當你在'ImageWriteParam'中指定「PackBits」作爲壓縮類型時,我認爲你不應該自己使用壓縮器,而是使用JAI ImageIO TIFF插件('TIFFImageWriter')。你也可以在param中傳遞一個壓縮器實例,如果你先將它轉換爲'TIFFImageWriteParam',但這對插件不知道的自定義壓縮更有用。 – haraldK