Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert byte to image

Tags:

java

image

why bImageFromConvert is getting null even o has data ?

BufferedImage img = null;
byte[] s;
ArrayList<Byte> f;
InputStream in;
BufferedImage bImageFromConvert;

public void print(ArrayList<Byte> lst) throws IOException {
    byte[] o = new byte[lst.size()];
    for (int i = 0; i < lst.size(); i++) {
        o[i] = lst.get(i);
    }
    in = new ByteArrayInputStream(o);
    bImageFromConvert = ImageIO.read(in);
like image 598
Basil Battikhi Avatar asked Jul 30 '26 09:07

Basil Battikhi


1 Answers

okay, based on the discussion in the comments:

You have a byte-stream which represents RGB per pixel.

ImageIO.read() does, according to its Javadoc, "Return a BufferedImage as the result of decoding a supplied InputStream with an ImageReader chosen automatically from among those currently registered.". So, ImageIO.read() typically does not expect RGB pixel bytes, but an encoding as JPG or PNG. As it cannot recognize the byte-stream as a valid image encoding, it returns null.

As possible solution on how to get an image from the RGB pixel bytes is then given on SO: How to convert array of bytes into Image in Java SE

like image 198
cello Avatar answered Aug 01 '26 22:08

cello



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!