在Java中将字节数组(字节[])转换为图像

在Java中将字节数组(字节[])转换为图像,java,image,byte,jpeg2000,Java,Image,Byte,Jpeg2000,我有一个字节[],我想将其转换为图像并在标签中显示图像。 字节[]为jpeg 2000格式。 我尝试了下面的代码,但返回null: InputStream in = new ByteArrayInputStream(bytearray); BufferedImage image = ImageIO.read(in); 图像值返回为null 我希望能够以如下所示的标签显示图像: jLabel.setIcon(new ImageIcon(image)); 谢谢 以上是我过去的工作方式,用户只需将

我有一个
字节[]
,我想将其转换为图像并在标签中显示图像。 字节[]为jpeg 2000格式。 我尝试了下面的代码,但返回null:

InputStream in = new ByteArrayInputStream(bytearray);
BufferedImage image = ImageIO.read(in);
图像值返回为
null

我希望能够以如下所示的标签显示图像:

jLabel.setIcon(new ImageIcon(image));
谢谢


以上是我过去的工作方式,用户只需将配置文件图片存储在字节数组中。servlet实现这一点并输出图像。

要将字节数组,即
字节[]
转换为
图像
,请使用
getImage()
。可能最简单的方法是使用
ImageIcon(byte[])
构造函数实例化
ImageIcon
,然后调用
getImage()
。下面的方法对此进行了说明,特别是最后一行:

public Image createImage(){
   //ccurve.png
   byte[] b = {-119, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82,
      0, 0, 0, 15, 0, 0, 0, 15, 8, 6, 0, 0, 0, 59, -42, -107,
      74, 0, 0, 0, 64, 73, 68, 65, 84, 120, -38, 99, 96, -64, 14, -2,
      99, -63, 68, 1, 100, -59, -1, -79, -120, 17, -44, -8, 31, -121, 28, 81,
      26, -1, -29, 113, 13, 78, -51, 100, -125, -1, -108, 24, 64, 86, -24, -30,
      11, 101, -6, -37, 76, -106, -97, 25, 104, 17, 96, -76, 77, 97, 20, -89,
      109, -110, 114, 21, 0, -82, -127, 56, -56, 56, 76, -17, -42, 0, 0, 0,
      0, 73, 69, 78, 68, -82, 66, 96, -126};
   return new ImageIcon(b).getImage();
}
我认为这可以用于
png
gif
bmp
jpg
图像。此外,字节数组不必像本例中那样硬编码

如果希望使用
ImageIcon
而不是
Image
,请不要调用
getImage()


然后可以调用
jlabel.setIcon(createIconImage())

我猜可能的重复是您的输入数据不正确。请显示您是如何获得数据的。请看这里:@Jon Skeet,我从web服务获取字节[]。我将用户ID和指纹发送到存储图像的web服务,因此如果它们正确,我将返回字节[]。当然可以,但通常是浏览器而不是Java将
字节[]
返回到图像,因此我无法将您的答案视为相关。@用户请将您的注释从此答案移到问题上。这样Jon就会知道了(AFAIU)。
public Image createImage(){
   //ccurve.png
   byte[] b = {-119, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82,
      0, 0, 0, 15, 0, 0, 0, 15, 8, 6, 0, 0, 0, 59, -42, -107,
      74, 0, 0, 0, 64, 73, 68, 65, 84, 120, -38, 99, 96, -64, 14, -2,
      99, -63, 68, 1, 100, -59, -1, -79, -120, 17, -44, -8, 31, -121, 28, 81,
      26, -1, -29, 113, 13, 78, -51, 100, -125, -1, -108, 24, 64, 86, -24, -30,
      11, 101, -6, -37, 76, -106, -97, 25, 104, 17, 96, -76, 77, 97, 20, -89,
      109, -110, 114, 21, 0, -82, -127, 56, -56, 56, 76, -17, -42, 0, 0, 0,
      0, 73, 69, 78, 68, -82, 66, 96, -126};
   return new ImageIcon(b).getImage();
}
public ImageIcon createImageIcon(){
   //ccurve.png
   byte[] b = {-119, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82,
      0, 0, 0, 15, 0, 0, 0, 15, 8, 6, 0, 0, 0, 59, -42, -107,
      74, 0, 0, 0, 64, 73, 68, 65, 84, 120, -38, 99, 96, -64, 14, -2,
      99, -63, 68, 1, 100, -59, -1, -79, -120, 17, -44, -8, 31, -121, 28, 81,
      26, -1, -29, 113, 13, 78, -51, 100, -125, -1, -108, 24, 64, 86, -24, -30,
      11, 101, -6, -37, 76, -106, -97, 25, 104, 17, 96, -76, 77, 97, 20, -89,
      109, -110, 114, 21, 0, -82, -127, 56, -56, 56, 76, -17, -42, 0, 0, 0,
      0, 73, 69, 78, 68, -82, 66, 96, -126};
   return new ImageIcon(b);
}