Forked from jamesthompson/JavaFXImageConversion.java
Created
October 16, 2019 02:15
-
-
Save CellycoMobiles/bb804aa1cc7514451901f1d8290f5cf8 to your computer and use it in GitHub Desktop.
JavaFX Image from a byte array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment