Skip to content

Instantly share code, notes, and snippets.

@jamesthompson
Created August 13, 2012 21:03
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jamesthompson/3344090 to your computer and use it in GitHub Desktop.
Save jamesthompson/3344090 to your computer and use it in GitHub Desktop.
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;
}
}
@castmart
Copy link

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

@ricemery
Copy link

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
Copy link

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

@gogomarine
Copy link

Nice tool.

@saudraisn
Copy link

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

@PrincewillIroka
Copy link

This resource was very useful to me. Thanks.

@helospark
Copy link

helospark commented Sep 9, 2018

If performance is important (ex. to display animation), I suggest to use:

javafx.embed.swing.SwingFXUtils.toFXImage(bufferedImage, null)

On my machine it is about 6 times faster than rendering image to a png then parsing that with JavaFX.

Update:
An even faster solution that avoids the intermediary BufferedImage is:

ByteBuffer bgraPixels = convertToBgra(rawPixels); // convert your byte[] array into a ByteBuffer in bgra format.
Image img = new WritableImage(new PixelBuffer<>(width, height, bgraImage, WritablePixelFormat.getByteBgraPreInstance()));

@Precival
Copy link

Good job! Thanks :D

@ouaibsky
Copy link

ouaibsky commented Jan 6, 2023

convertToBgra

Hi,
Where did you get method: convertToBgra(rawPixels) ?

Thx
Chris

@helospark
Copy link

helospark commented Jan 6, 2023

@ouaibsky I have created it like this. I'm using ByteBuffer input here instead of byte[], but that's basically just a wrapper around the array. I'm doing RGBA->BGRA conversion, depending on you input format, you may just have to wrap the byte array.

Also in some cases further optimization can by done by moving longs instead of bytes.

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