0

This is now solved! The solution is in the code below

My initial question below

I need to display an Image from pixels received as an array of bytes-one byte per pixel- into a ImageView.

The image was originally in png format.

private WritableImage convertByteArrayToImage(byte[] pixels, int width,int height) {

    int imageType= Integer.valueOf(expTime.getText());

    int[] ints = new int[pixels.length];
    for (int i = 0; i < pixels.length; i++) {
        ints[i] = (int) pixels[i] & 0xff;
    }

    BufferedImage bImg
            = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);        


    WritableRaster raster = (WritableRaster) bImg.getData();
     raster.setPixels(0, 0, width, height, ints);
    bImg.setData(raster);


    return SwingFXUtils.toFXImage(bImg, null);
}

Thanks

dummyhead
  • 81
  • 2
  • 9
  • 1
    How did the data get into the byte[] array to begin with? And what is the format of the data *in the byte array*? Hint, it is not png, because JavaFX understands png and if it were png, then `Image img = new Image(new ByteArrayInputStream(data));` would not throw an exception. – jewelsea Nov 19 '18 at 19:08
  • Can you post the full stack trace of the `ImageStorageException`? – Slaw Nov 19 '18 at 19:44
  • Please provide a [mcve] that demonstrates the problem. – kleopatra Nov 20 '18 at 09:25
  • Thank you all for your comments..I have edited the initial question with more details. Hope now it is OK – dummyhead Nov 20 '18 at 10:50
  • Solved!! The solution is based on my code-2- (edited). It was a matter of changing the way the BufferedImage was created. Seen here: [link] https://stackoverflow.com/questions/10061029/how-to-make-bmp-image-from-pixel-byte-array-in-java – dummyhead Nov 20 '18 at 17:16

0 Answers0