ImageIO.read无法读取ByteArrayInputStream(图像处理) [英] ImageIO.read can't read ByteArrayInputStream (image processing)

查看:247
本文介绍了ImageIO.read无法读取ByteArrayInputStream(图像处理)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图读取图像文件,将其转换为字节数组,处理各个字节,然后将其转换回图像文件并导出.

I'm trying to read in an image file, convert it into a bytes array, process the individual bytes, and then convert it back into an image file and export it.

我已经尝试过处理它,但是似乎ImageIO.read无法读取ByteInputArrayStream-它返回null.

I've tried working on it, but it seems that ImageIO.read can't read the ByteInputArrayStream - it returns null.

这是我到目前为止已经尝试过的方法(以及引发错误的行)

Here's what I've tried so far (and the line that throws the error)

public static void RGBToGrayManual2(BufferedImage original) {
    byte[] pixels = ((DataBufferByte) original.getRaster().getDataBuffer()).getData();

    /*
     *  Code to process pixels
     */

    ByteArrayInputStream grayscaleByteInputStream = new ByteArrayInputStream(pixels);
    BufferedImage convertedGrayscale = null;


    try {
        // below line throws the error
        convertedGrayscale = ImageIO.read(grayscaleByteInputStream);

        ImageIO.write(convertedGrayscale, "jpg", new File("converted-grayscale-002.jpg"));
    } catch (IOException e) {
        System.err.println("IOException: " + e);
    }   
}

错误消息

线程"main"中的异常java.lang.IllegalArgumentException:图像 ==空!在javax.imageio.ImageTypeSpecifier.createFromRenderedImage(ImageTypeSpecifier.java:925) 在javax.imageio.ImageIO.getWriter(ImageIO.java:1591)在 javax.imageio.ImageIO.write(ImageIO.java:1520)在 project2.ImageProcessing.RGBToGrayManual2(ImageProcessing.java:252) 在project2.ImageProcessing.main(ImageProcessing.java:284)

Exception in thread "main" java.lang.IllegalArgumentException: image == null! at javax.imageio.ImageTypeSpecifier.createFromRenderedImage(ImageTypeSpecifier.java:925) at javax.imageio.ImageIO.getWriter(ImageIO.java:1591) at javax.imageio.ImageIO.write(ImageIO.java:1520) at project2.ImageProcessing.RGBToGrayManual2(ImageProcessing.java:252) at project2.ImageProcessing.main(ImageProcessing.java:284)

我也看过类似的文章-从ImageIO.read返回的空值( new ByteArrayInputStream(bs)); -接受的答案似乎表明这是编码方面的问题.

I've also looked at a similar post - Null returned from ImageIO.read(new ByteArrayInputStream(bs)); - and the accepted answer seems to suggest it's a problem with the encoding.

我看了另一篇文章-哪个Java库提供了base64编码/decoding?-解码bytes数组,但是我认为我做的不正确.

I've looked at this other post - Which Java library provides base64 encoding/decoding? - to decode the bytes array, but I don't think I'm doing it right.

这是我尝试过的:

String encodedPixelsString = DatatypeConverter.printBase64Binary(pixels);
byte[] decodedPixelsString = DatatypeConverter.parseBase64Binary(encodedPixelsString);
ByteArrayInputStream pixelsStreamInputStream = new ByteArrayInputStream(decodedPixelsString);

并作为参数传入解码数组的ByteArrayInputStream

And passed in the decoded array's ByteArrayInputStream as an argument

    convertedGrayscale = ImageIO.read(pixelStreamInputStream);

但是,它产生了完全相同的错误消息.

However it yielded the exact same error message.

我对解决此问题的两个可能方向的想法-但我不确定细节:

  1. 使用ImageIO.read方法找出问题
  2. 尝试以其他方式公开图像文件的字节数组
  1. Find out the problem with ImageIO.read method
  2. Try exposing the bytes array of the image file in a different way

这是我们必须要做的一项任务,但是我以前从未从事过图像处理工作,因此,我对要做的事情有些迷茫.我真的很感激任何指针

This is an assignment we have to do, but I've never worked with image processing before and as such, I'm a bit lost as to what to do. I would really appreciate any pointers

推荐答案

首先,ImageIO.read(...)方法的 not 例外.它应按要求返回null.因此,当您使用null图像调用ImageIO.write(...)时,会发生异常.

First of all, the exception is not from the ImageIO.read(...) method. It returns null, as it should. The exception happens because of that though, when you invoke ImageIO.write(...) with the null image.

现在,ImageIO.read(...)返回null作为输入的原因仅是因为ImageIO文件格式读取和写入图像.您的pixels byte数组不是文件格式,而是原始像素数据(不,这与Base64或其他字符串编码无关).

Now, the reason ImageIO.read(...) returns null for your input is simply because ImageIO reads and writes images from/to file formats. Your pixels byte array is not in a file format, it is raw pixel data (and, no, this has nothing to do with Base64 or other string encodings).

现在,假设您的pixel数组是8位/像素灰度格式(重要的是,如果此假设是错误的,下面的代码将不起作用,但是您没有提供足够的信息供其他人确定,因此您可能需要修改代码以适合您的数据),可以轻松地重新创建BufferedImage:

Now, assuming that your pixel array is 8 bits/pixel gray scale format (important, the below code won't work if this assumption is wrong, but you haven't provided enough information for others to determine this, so you might need to modify the code to fit your data), you can easily re-create the BufferedImage:

byte[] pixels = ((DataBufferByte) original.getRaster().getDataBuffer()).getData();

/*
 *  Code to process pixels (just as before)
 */

// Replace the ImageIO.read invocation with the following code
// Note that *pixels* must be in 8 bits/pixel (grayscale) for this to work,
// it is not cheating! :-)
BufferedImage convertedGrayscale = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
convertedGrayScale.getRaster().setDataElements(0, 0, width, height, pixels);

try {
    ImageIO.write(convertedGrayscale, "jpg", new File("converted-grayscale-002.jpg"));
} 
catch (IOException e) {
    System.err.println("IOException: " + e);
}

这篇关于ImageIO.read无法读取ByteArrayInputStream(图像处理)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆