Search code examples
javaarraysbytebufferedimageimage-conversion

Converting byte array to bufferedimage results in null


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?


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.