[英]Convert byte array (byte[]) to Image in Java
I have a byte[]
that I want to convert to an Image and display the image in a label. 我有一个
byte[]
,我想转换为图像并在标签中显示图像。 The byte[] is of a jpeg 2000 format. byte []是jpeg 2000格式。 I have tried the code below but it returns null:
我已经尝试了下面的代码,但它返回null:
InputStream in = new ByteArrayInputStream(bytearray);
BufferedImage image = ImageIO.read(in);
The image value comes back as null
. 图像值返回
null
。
I want to be able to display the image in a label like below: 我希望能够在如下标签中显示图像:
jLabel.setIcon(new ImageIcon(image));
Thanks 谢谢
To convert an array of bytes, ie byte[]
into an Image
, use getImage()
. 要将字节数组(即
byte[]
转换为Image
,请使用getImage()
。 Probably the easiest way to do this is to instantiate an ImageIcon
using the ImageIcon(byte[])
constructor, and then call getImage()
. 可能最简单的方法是使用
ImageIcon(byte[])
构造函数实例化ImageIcon
,然后调用getImage()
。 This is illustrated in the method below, particularly the last line: 这在下面的方法中说明,特别是最后一行:
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();
}
I think this can by used for png
, gif
, bmp
, and jpg
images. 我认为这可以用于
png
, gif
, bmp
和jpg
图像。 Also the byte array does not have to be hard-coded, as in this example. 字节数组也不必硬编码,如本例所示。
If you want an ImageIcon
instead of an Image
, don't call getImage()
: 如果你想要一个
ImageIcon
而不是一个Image
,不要调用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);
}
Then you can call jlabel.setIcon(createIconImage());
然后你可以调用
jlabel.setIcon(createIconImage());
. 。
Use Java Advanced Imaging to process JPEG2000 images. 使用Java Advanced Imaging处理JPEG2000图像。
Similar question: read jpeg2000 files in java 类似的问题: 在java中读取jpeg2000文件
ServletOutputStream out = response.getOutputStream();
out.write(user.getBytes());
The above is how its worked for me in the past where user has a profile picture simply stored in a byte array. 上面是它在过去的工作方式,用户只需将字符串图片存储在字节数组中。 The servlet realizes this and outputs the image.
servlet意识到这一点并输出图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.