Java 如何将字节数组转换为缓冲图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20013980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert byte array to buffered image
提问by baekacaek
I have a server-side java code that gets a byte array from the client. In order to do some image processing, I need to convert the byte array into a BufferedImage
. I have a code that's supposed to do that here:
我有一个从客户端获取字节数组的服务器端 java 代码。为了进行一些图像处理,我需要将字节数组转换为BufferedImage
. 我有一个应该在这里做的代码:
public void processImage(byte[] data) {
ByteArrayInputStream stream = new ByteArrayInputStream(data);
BufferedImage bufferedImage;
bufferedImage = ImageIO.read(stream);
// bufferedImage is null
//...
}
But this doesn't work; bufferedImage is null. According to the ImageIO documentation:
但这不起作用;bufferedImage 为空。根据 ImageIO 文档:
If no registered
ImageReader
claims to be able to read the resulting stream,null
is returned.
如果没有注册的
ImageReader
声明能够读取结果流,null
则返回。
How do I tell the ImageReader
what image type it is. For instance, if I know the image to be JPEG (which it is, in my case), what am I supposed to do?
我怎么知道ImageReader
它是什么图像类型。例如,如果我知道图像是 JPEG(在我的情况下是这样),我应该怎么做?
EDIT:Thanks for the suggestion that the file is most likely not in JPEG format. This is the client-side code I have that sends the data as String over to the server:
编辑:感谢您建议该文件很可能不是 JPEG 格式。这是我将数据作为字符串发送到服务器的客户端代码:
import org.json.JSONObject;
// Client-side code that sends image to server as String
public void sendImage() {
FileInputStream inputStream = new FileInputStream(new File("myImage.jpg"));
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
byte[] b = new byte[1024];
while ((bytesRead = inputStream.read(b)) != -1) {
byteStream.write(b,0,bytesRead);
}
byte[] byteArray = byteStream.toByteArray();
JSONObject jsonObject = new JSONObject();
jsonObject.put("data",new String(byteArray));
// ... more code here that sends jsonObject in HTTP post body
}
And this is the server-side code that calls the processImage() function:
这是调用 processImage() 函数的服务器端代码:
// Server-side code that calls processImage() function
public void handleRequest(String jsonData) {
JSONObject jsonObject = new JSONObject(jsonData);
processImage(jsonObject.getString("data").getBytes());
}
采纳答案by Stephen C
The most likely explanation is that the byte array doesn'tcontain a JPEG image. (For instance, if you've just attempted to download it, you may have an HTML document giving an error diagnostic.) If that's the case, you'll need to find what is causing this and fix it.
最可能的解释是字节数组不包含 JPEG 图像。(例如,如果您刚刚尝试下载它,您可能有一个给出错误诊断的 HTML 文档。)如果是这种情况,您需要找出导致此问题的原因并修复它。
However, if you "know" that the byte array contains an image with a given format, you could do something like this:
但是,如果您“知道”字节数组包含具有给定格式的图像,则可以执行以下操作:
- Use
ImageIO.getImageReadersByFormatName
orImageIO.getImageReadersByMIMEType
to get anIterator<ImageReader>
. - Pull the first
ImageReader
from theIterator
. - Create an
MemoryCacheImageInputStream
wrapping aByteArrayInputStream
for the types. - Use
ImageReader.setInput
to connect the reader to theImageInputStream
. - Use
ImageReader.read
to get theBufferedImage
.
- 使用
ImageIO.getImageReadersByFormatName
或ImageIO.getImageReadersByMIMEType
获取Iterator<ImageReader>
. - 拉第一
ImageReader
从Iterator
。 - 为类型创建一个
MemoryCacheImageInputStream
包装 aByteArrayInputStream
。 - 使用
ImageReader.setInput
的读卡器连接到ImageInputStream
。 - 使用
ImageReader.read
得到BufferedImage
。