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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:20:08  来源:igfitidea点击:

How to convert byte array to buffered image

javabufferedimagejavax.imageiobytearrayinputstream

提问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 ImageReaderclaims to be able to read the resulting stream, nullis returned.

如果没有注册的ImageReader声明能够读取结果流,null则返回。

How do I tell the ImageReaderwhat 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:

但是,如果您“知道”字节数组包含具有给定格式的图像,则可以执行以下操作:

  1. Use ImageIO.getImageReadersByFormatNameor ImageIO.getImageReadersByMIMETypeto get an Iterator<ImageReader>.
  2. Pull the first ImageReaderfrom the Iterator.
  3. Create an MemoryCacheImageInputStreamwrapping a ByteArrayInputStreamfor the types.
  4. Use ImageReader.setInputto connect the reader to the ImageInputStream.
  5. Use ImageReader.readto get the BufferedImage.
  1. 使用ImageIO.getImageReadersByFormatNameImageIO.getImageReadersByMIMEType获取Iterator<ImageReader>.
  2. 拉第一ImageReaderIterator
  3. 为类型创建一个MemoryCacheImageInputStream包装 a ByteArrayInputStream
  4. 使用ImageReader.setInput的读卡器连接到ImageInputStream
  5. 使用ImageReader.read得到BufferedImage