Convert byte to image

why does bImageFromConvert get null 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);

      

+3


source to share


1 answer


ok, based on the discussion in the comments:

You have a byte stream that represents RGB per pixel.



ImageIO.read()

does, according to its Javadoc, "Returns a BufferedImage as a result of decoding a supplied InputStream with an ImageReader that is automatically selected from among those currently registered." So ImageIO.read()

normally does not expect RGB pixel bytes, but encoded as JPG or PNG. Since it cannot recognize the byte stream as a valid image encoding, it returns null.

As a possible solution on how to get an image from RGB pixel bytes then points to SO: How to convert a byte array to Image in Java SE

+3


source







All Articles