Java 从字节数组转换和显示图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18079754/
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-11 22:15:33  来源:igfitidea点击:

Convert and display image from byte array

javaimageswingjavax.imageio

提问by bajla

I'm making a program, which gets data about an image in byte array from a server. I'm converting this data into 24bit BMP format (whether its jpeg, png, bmp or 8-24-32bpp). First, I'm saving it to my HD, and then I'm loading it into a JLabel's Icon. Works perfectly, though there are some cases in which I get the following exception:

我正在制作一个程序,它从服务器获取有关字节数组中图像的数据。我正在将此数据转换为 24 位 BMP 格式(无论是 jpeg、png、bmp 还是 8-24-32bpp)。首先,我将它保存到我的 HD,然后我将它加载到 JLabel 的图标中。完美运行,尽管在某些情况下我会遇到以下异常:

java.io.EOFException at
javax.imageio.stream.ImageInputStreamImpl.readFully(ImageInputStreamImpl.java:353) at
com.sun.imageio.plugins.bmp.BMPImageReader.read24Bit(BMPImageReader.java:1188) at
com.sun.imageio.plugins.bmp.BMPImageReader.read(BMPImageReader.java:843) at
javax.imageio.ImageIO.read(ImageIO.java:1448) at 
javax.imageio.ImageIO.read(ImageIO.java:1308)

For this line (the second)

对于这条线(第二条)

File imgFile = new File("d:/image.bmp");
BufferedImage image = ImageIO.read(imgFile);

In these cases:

在这些情况下:

  • the image does not load into the JLabel, but it can be found on my HD
  • the conversion is not proper, because something "slips"
  • the picture is like when you use italics in a word document
  • 图像未加载到 JLabel 中,但可以在我的 HD 中找到
  • 转换不正确,因为有些东西“滑倒”
  • 图片就像你在word文档中使用斜体一样

First, i thought maybe the bpp is the problem, then i thought that maybe the pictures are too large, but i have cases it works and cases it doesn't for both suggestions. I'm a little stuck here, and would be glad for ideas.

首先,我认为可能 bpp 是问题所在,然后我认为可能图片太大了,但是我有两种建议都有效的情况和无效的情况。我有点卡在这里,很高兴有想法。

采纳答案by haraldK

  • the picture is like .. when You use italics in a word document
  • 图片就像..当你在word文档中使用斜体时

Think I finally got what this bullet item meant now.. ;-)

想我终于明白这个项目符号的意思了.. ;-)

Speculative answer, but here goes:

推测性的答案,但这里是:

If the image you write looks "skewed", it's probably due to missing padding for each column as the BMP format specifies (or incorrect width field in the BMP header). I assume then, that the images you get EOF exceptions for, is where the width is not a multiple of 4.

如果您编写的图像看起来“倾斜”,则可能是由于 BMP 格式指定的每列缺少填充(或 BMP 标题中的宽度字段不正确)。我假设,您获得 EOF 例外的图像的宽度不是 4 的倍数。

Try to write the BMPs using ImageIO to see if that helps:

尝试使用 ImageIO 编写 BMP 以查看是否有帮助:

private static BufferedImage createRGBImage(byte[] bytes, int width, int height) {
    DataBufferByte buffer = new DataBufferByte(bytes, bytes.length);
    ColorModel cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[]{8, 8, 8}, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
    return new BufferedImage(cm, Raster.createInterleavedRaster(buffer, width, height, width * 3, 3, new int[]{0, 1, 2}, null), false, null);
}

...

...

byte[] bytes = ...; // Your image bytes
OutputStream stream = ...; // Your output

BufferedImage image = createRGBImage(bytes, width, height);

try {
    ImageIO.write(image, "BMP", stream);
}
finally {
    stream.close();
}

回答by Mohsin Shaikh

You can use this code to convert the output image to a byte Array

您可以使用此代码将输出图像转换为字节数组

   Blob b = rs.getBlob(2);
   byte barr[] = new byte[(int)b.length()]; //create empty array
   barr = b.getBytes(1,(int)b.length());

   FileOutputStream fout = new FileOutputStream("D:\sonoo.jpg");
   fout.write(barr);

回答by Azad

Call it by class name, liek ClassName.byteArrayToImage(byte):

按类名调用它,例如ClassName.byteArrayToImage(byte)

public static BufferedImage  byteArrayToImage(byte[] bytes){  
        BufferedImage bufferedImage=null;
        try {
            InputStream inputStream = new ByteArrayInputStream(bytes);
            bufferedImage = ImageIO.read(inputStream);
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
        return bufferedImage;
}