Basil Battikhi
Basil Battikhi

Reputation: 2668

convert byte to 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);

Upvotes: 3

Views: 401

Answers (1)

cello
cello

Reputation: 5486

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

Upvotes: 3

Related Questions