将 ImageOutputStream 转换为 byte[]
一直尝试使用 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);
谢谢 , 维奈
Have been trying to convert ImageOutputStream to byte[] for a while using JAI. Any inputs are appreciated. Thanks.
Sorry here is the code snippet, I am working on. I had to post it earlier. The problem I am facing is that, I am able to get ByteArrayOutputStream from ImageOutputStream. But it always gives me zero bytes. But if I use a FileOutputStream instead of a ByteArrayOuputStream, I can write into a file which has non zero bytes. :
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);
Thanks ,
Vinay
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我很确定这是因为你在操作结束时没有调用
ImageOutputStream.flush()
- 如果创建的 IOS 是MemoryCacheOutputStream
,它似乎需要对某些 ImageIO 实现的显式刷新。I'm pretty sure it's because you're not calling
ImageOutputStream.flush()
at the end of the operation - if the IOS created is aMemoryCacheOutputStream
, it seems to require an explicit flush on some ImageIO implementations.我得到了答案,这与纪尧姆的建议非常接近。只是不需要在中间写入临时文件。以下是将 ImageOutputStream 转换为 byte[] 的修改后的代码:
I got the answer for this, which is very close to what Guillaume suggested. Except that no need to write to a temp file in between. The following is the modified code to convert ImageOutputStream to byte[] :
使用 ByteArrayOutputStream(而不是 FileOutputStream)创建 ImageOutputStream
图像处理完成后,图像字节将在“ios”对象中可用,但偏移量将位于“ios”对象的末尾。因此,我们需要将偏移量定位到“ios”对象的开头,然后读取字节以写入新的 ByteArrayOutputStream 对象。最后从新对象返回 toByteArray()。就像下面这样:
Create ImageOutputStream with ByteArrayOutputStream (instead of FileOutputStream)
After image processing is done, image bytes would be available in the "ios" object, but the offset would be positioned at the end of the "ios" object. So we need to position the offset to the beginning of the "ios" object and then read the bytes to write to new ByteArrayOutputStream object. Finally return the toByteArray() from the new object. Like below:
那么,您将无法从输出流中读取。
您应该尝试从图像对象获取
InputStream
并从那里读取必要的数据。但是,如果您提供有关您实际想要实现的目标的更多详细信息,我们可以更有效地帮助您。 :)
例如,如果您只想以字节为单位显示图像的大小,还有其他几种方法。
Well, you won't be able to read from an output stream.
You should rather try to get an
InputStream
from your image object and read the necessary data from there.However, if you gave more details about what you are actually trying to achieve we could help you more efficiently. :)
For instance if you are only willing to display the size of the images in bytes, there are several other ways.