问题描述
我正在尝试读取图像文件,将其转换为字节数组,处理各个字节,然后将其转换回图像文件并导出.
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: image== 空!在 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)
我也看过类似的帖子 - 从 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? - 解码字节数组,但我认为我做得不对.
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.
我对解决此问题的两个可能方向的想法 - 但我不确定细节:
My thoughts on two possible directions to solve this problem - but I'm not sure about the details:
- 用
ImageIO.read
方法找出问题 - 尝试以不同的方式公开图像文件的字节数组
这是我们必须完成的任务,但我之前从未从事过图像处理工作,因此,我有点不知所措.我真的很感激任何指点
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(...)
方法的不是例外.它应该返回 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
从/to 文件格式.您的 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(图像处理)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!