Question

I am trying to convert a byte array of length 128 to a 32x32 bitmap stored in a BufferedImage. I am using the following code:

private BufferedImage fSP;

public Pattern( byte[] aBitData ) {
  if ( aBitData == null ) {
    throw new IllegalArgumentException( "Please provide a non-null byte array of length 128: " + aBitData );
  }
  else if ( aBitData.length != 128 ) {
    throw new IllegalArgumentException( "Please provide a non-null byte array of length 128: " + aBitData.length );
  }
  InputStream in = new ByteArrayInputStream( aBitData );
  try {
    fSP = ImageIO.read( in );
  } catch( IOException e ) {
    e.printStackTrace();
  }
}

But every time fSP is set to null for some reason. I don't understand why this happens. Could anyone help me out?

Was it helpful?

Solution

I suspect that the data provided in your bit array does not correspond to any supported file format, i.e. can be read by any of the implemented ImageReaders.

OTHER TIPS

From the JavaDoc:

Returns a BufferedImage as the result of decoding a supplied InputStream with an ImageReader chosen automatically from among those currently registered. The InputStream is wrapped in an ImageInputStream. If no registered ImageReader claims to be able to read the resulting stream, null is returned.

Looks as if the byte array's contents can't be decoded as a known image format.

there could be another reason of getting null, if you modified aBitData such way that doesn't represent any image, suppose - if you modify first byte, which is image header byte, not image data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top