将ImageOutputStream转换为byte []

时间:2010-08-17 23:48:50

标签: java image jai

尝试使用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);

谢谢,  维奈

4 个答案:

答案 0 :(得分:3)

使用ByteArrayOutputStream(而不是FileOutputStream)创建ImageOutputStream

ByteArrayOutputStream bos = new ByteArrayOutputStream(255);
ImageOutputStream ios = ImageIO.createImageOutputStream(bos);
// use "ios" for image processing

图像处理完成后,图像字节将在“ios”对象中可用,但偏移量将位于“ios”对象的末尾。因此,我们需要将偏移量定位到“ios”对象的开头,然后读取字节以写入新的ByteArrayOutputStream对象。最后从新对象返回toByteArray()。如下所示:

        public byte[] getBytes(ImageOutputStream ios) {
            ByteArrayOutputStream bos = new ByteArrayOutputStream(255);
            byte imageByte;
            long counter = 0; 
            try {
                    System.out.println("getStreamPosition()[BEFORE]=" + ios.getStreamPosition());
                    ios.seek(0);
                    System.out.println("getStreamPosition()[AFTER]=" + ios.getStreamPosition());
            } catch (IOException e1) {                       
                    e1.printStackTrace();
            }

            while (true) {
                    try {
                            bos.write(ios.readByte());
                            counter++;
                    } catch (EOFException e) {
                            System.out.println("End of Image Stream");
                            break;
                    } catch (IOException e) {
                            System.out.println("Error processing the Image Stream");
                            break;
                    }
            }
            System.out.println("Total bytes read=" + counter);
            byte[] retValue = bos.toByteArray();
            return retValue;
    }

答案 1 :(得分:3)

我很确定这是因为你没有在操作结束时调用ImageOutputStream.flush() - 如果创建的IOS是MemoryCacheOutputStream,它似乎需要在某些ImageIO实现上显式刷新

答案 2 :(得分:2)

我得到了答案,这与纪尧姆建议的非常接近。除了不需要写入之间的临时文件。以下是将ImageOutputStream转换为byte []的修改代码:

ByteArrayOutputStream baos = new ByteArrayOutputStream(37628);
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);

//Create a ByteArrayInputStream to read that ByteArrayOutputStream and read it from ImageIO

ByteArrayInputStream bai = new ByteArrayInputStream(baos.toByteArray());
RenderedImage out = ImageIO.read(bai);
int size = bos.toByteArray().length;

System.out.println(""+ size);

return bos.toByteArray

答案 3 :(得分:0)

好吧,您将无法从输出流中读取

您应该尝试从图片对象中获取InputStream并从那里读取必要的数据。

但是,如果您提供了有关您实际尝试实现的更多详细信息,我们可以更有效地帮助您。 :)

例如,如果您只愿意以字节为单位显示图像的大小,还有其他几种方法。