JavaFX. Displaying an image from byte[]

别来无恙 提交于 2019-12-24 23:53:55

问题


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

来源:https://stackoverflow.com/questions/53381018/javafx-displaying-an-image-from-byte

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!