一段时间以来一直在尝试使用 JAI 将 ImageOutputStream 转换为 byte[]。任何输入表示赞赏。谢谢。
抱歉,这是代码片段,我正在处理。我不得不提前发布它。我面临的问题是,我能够从 ImageOutputStream 中获取 ByteArrayOutputStream。但它总是给我零字节。但是如果我使用 FileOutputStream 而不是 ByteArrayOuputStream,我可以写入一个非零字节的文件。:
File file = new File("C:/TIFFImages/tiff-image.tiff");
FileInputStream in = new FileInputStream(file);
long filelength = file.length();
byte[] bytes = new byte[(int)filelength];
int offset = 0;
int numRead = 0;
while (offset < bytes.length && (numRead=in.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}
if (offset < bytes.length) {
throw new IOException("Could not completely read file "+file.getName());
}
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
RenderedImage src = JAI.create("stream", SeekableStream.wrapInputStream(bais, true));
RenderedOp renderedOp = MedianFilterDescriptor.create(src, MedianFilterDescriptor.MEDIAN_MASK_SQUARE , 1, null);
BufferedImage image = renderedOp.getAsBufferedImage();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
//Instead of baos if I pass a FileOutputStream to the above function. It writes non zero
//bytes to the output file
TIFFImageWriterSpi tiffspi = new TIFFImageWriterSpi();
ImageWriter writer = tiffspi.createWriterInstance();
RenderedImage renderedImage = PlanarImage.wrapRenderedImage(src);
writer.setOutput(ios);
writer.write(image);
writer.write(null,new IIOImage(image, null, null), param);
System.out.println("After tiff ImageIO operations" + baos.toByteArray().length);
谢谢,维奈