Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JavaFX Image from a byte array
import javafx.scene.image.Image;
import javax.imageio.ImageIO;
import java.awt.image.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
public Image getJavaFXImage(byte[] rawPixels, int width, int height) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
ImageIO.write((RenderedImage) createBufferedImage(rawPixels, width, height), "png", out);
out.flush();
} catch (IOException ex) {
Logger.getLogger(ImageUtils.class.getName()).log(Level.SEVERE, null, ex);
}
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
return new javafx.scene.image.Image(in);
}
private BufferedImage createBufferedImage(byte[] pixels, int width, int height) {
SampleModel sm = getIndexSampleModel(width, height);
DataBuffer db = new DataBufferByte(pixels, width*height, 0);
WritableRaster raster = Raster.createWritableRaster(sm, db, null);
IndexColorModel cm = getDefaultColorModel();
BufferedImage image = new BufferedImage(cm, raster, false, null);
return java.awt.image;
}
private SampleModel getIndexSampleModel(int width, int height) {
IndexColorModel icm = getDefaultColorModel();
WritableRaster wr = icm.createCompatibleWritableRaster(1, 1);
SampleModel sampleModel = wr.getSampleModel();
sampleModel = sampleModel.createCompatibleSampleModel(width, height);
return sampleModel;
}
private IndexColorModel getDefaultColorModel() {
byte[] r = new byte[256];
byte[] g = new byte[256];
byte[] b = new byte[256];
for(int i=0; i<256; i++) {
r[i]=(byte)i;
g[i]=(byte)i;
b[i]=(byte)i;
}
IndexColorModel defaultColorModel = new IndexColorModel(8, 256, r, g, b);
return defaultColorModel;
}
public void findMinAndMax(short[] pixels, int width, int height) {
int size = width*height;
int value;
min = 65535;
max = 0;
for (int i=0; i<size; i++) {
value = pixels[i]&0xffff;
if (value<min)
min = value;
if (value>max)
max = value;
}
}
@jamesthompson

This comment has been minimized.

Show comment
Hide comment
@jamesthompson

jamesthompson Aug 13, 2012

How to create a JavaFX Image from a byte array. A gist showing the general method.

Owner

jamesthompson commented Aug 13, 2012

How to create a JavaFX Image from a byte array. A gist showing the general method.

@castmart

This comment has been minimized.

Show comment
Hide comment
@castmart

castmart Oct 30, 2012

Is it possible to identify the image's width and height directly from the file?

castmart commented Oct 30, 2012

Is it possible to identify the image's width and height directly from the file?

@ricemery

This comment has been minimized.

Show comment
Hide comment
@ricemery

ricemery Dec 3, 2012

Thanks. This was helpful... One comment, the IndexColorModel is getting built twice. Probably not a huge deal, but easy to optimize away.
Thanks again.

ricemery commented Dec 3, 2012

Thanks. This was helpful... One comment, the IndexColorModel is getting built twice. Probably not a huge deal, but easy to optimize away.
Thanks again.

@taylorthurlow

This comment has been minimized.

Show comment
Hide comment
@taylorthurlow

taylorthurlow Dec 12, 2013

Line 25 has return java.awt.image' which I haven't seen before... I assume we just needreturn image` in it's place?

taylorthurlow commented Dec 12, 2013

Line 25 has return java.awt.image' which I haven't seen before... I assume we just needreturn image` in it's place?

@gogomarine

This comment has been minimized.

Show comment
Hide comment
@gogomarine

gogomarine commented Jan 30, 2015

Nice tool.

@saudraisn

This comment has been minimized.

Show comment
Hide comment
@saudraisn

saudraisn Mar 19, 2018

+1 for @taylorthurlow comment! Very helpful, thanks!

saudraisn commented Mar 19, 2018

+1 for @taylorthurlow comment! Very helpful, thanks!

@PrincewillIroka

This comment has been minimized.

Show comment
Hide comment
@PrincewillIroka

PrincewillIroka Jul 10, 2018

This resource was very useful to me. Thanks.

PrincewillIroka commented Jul 10, 2018

This resource was very useful to me. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment