Skip to content

Instantly share code, notes, and snippets.

@HanSolo
Created January 1, 2015 13:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HanSolo/d0017d0729e5f7a73c31 to your computer and use it in GitHub Desktop.
Save HanSolo/d0017d0729e5f7a73c31 to your computer and use it in GitHub Desktop.
JavaFX create snapshot and save as xbm file
// create the xbm image from a given node
createSnapshotAndWriteXbm(pane, "epaperImage");
// display the image on the e-paper display
try {
Process p1 = new ProcessBuilder("/bin/bash", "-c", "/home/pi/gratis/PlatformWithOS/driver-common/xbm2bin < /home/pi/epaper/yotaclock.xbm > /dev/epd/display").start();
p1.waitFor(1, TimeUnit.SECONDS);
Process p2 = new ProcessBuilder("/bin/bash", "-c", "echo U > /dev/epd/command").start();
p2.waitFor(1, TimeUnit.SECONDS);
} catch (InterruptedException | IOException e) {
// do something meaningful here
}
// methods
private void createSnapshotAndWriteXbm(final Node NODE, final String FILE_NAME) {
final WritableImage SNAPSHOT = NODE.snapshot(new SnapshotParameters(), null);
StringBuilder sb = new StringBuilder();
try {
final int WIDTH = (int) SNAPSHOT.getWidth();
final int HEIGHT = (int) SNAPSHOT.getHeight();
final PixelReader PIXEL_READER = SNAPSHOT.getPixelReader();
final OutputStream OUTPUT_STREAM = new FileOutputStream(String.join("", "/PATH/TO/IMAGE/", FILE_NAME, ".xbm").toString());
sb.setLength(0);
OUTPUT_STREAM.write(sb.append("#define ").append(FILE_NAME).append("_width ").append(WIDTH).append("\n").toString().getBytes("US-ASCII"));
sb.setLength(0);
OUTPUT_STREAM.write(sb.append("#define ").append(FILE_NAME).append("_height ").append(HEIGHT).append("\n").toString().getBytes("US-ASCII"));
sb.setLength(0);
OUTPUT_STREAM.write(sb.append("static unsigned char ").append(FILE_NAME).append("_bits[] = {").toString().getBytes("US-ASCII"));
int bitcache = 0;
int bits_in_cache = 0;
String separator = "\n ";
int written = 0;
for (int y = 0 ; y < HEIGHT ; y++) {
for (int x = 0 ; x < WIDTH ; x++) {
final int argb = PIXEL_READER.getArgb(x, y);
final int red = 0xff & (argb >> 16);
final int green = 0xff & (argb >> 8);
final int blue = 0xff & (argb >> 0);
int sample = (red + green + blue) / 3;
if (sample > 127) {
sample = 0;
} else {
sample = 1;
}
bitcache |= (sample << bits_in_cache);
++bits_in_cache;
if (8 == bits_in_cache) {
OUTPUT_STREAM.write(separator.getBytes("US-ASCII"));
separator = ",";
if (12 == written) {
OUTPUT_STREAM.write("\n ".getBytes("US-ASCII"));
written = 0;
}
OUTPUT_STREAM.write(toHexString(bitcache).getBytes("US-ASCII"));
bitcache = 0;
bits_in_cache = 0;
++written;
}
}
if (0 != bits_in_cache) {
OUTPUT_STREAM.write(separator.getBytes("US-ASCII"));
separator = ",";
if (12 == written) {
OUTPUT_STREAM.write("\n ".getBytes("US-ASCII"));
written = 0;
}
OUTPUT_STREAM.write(toHexString(bitcache).getBytes("US-ASCII"));
bitcache = 0;
bits_in_cache = 0;
++written;
}
}
sb.setLength(0);
OUTPUT_STREAM.write("\n};\n".getBytes("US-ASCII"));
OUTPUT_STREAM.close();
} catch (IOException exception) {
// do something meaningful here
}
}
private String toHexString(final int VALUE) {
final String HEX_STRING = Integer.toHexString(0xff & VALUE);
return 2 == HEX_STRING.length() ? ("0x" + HEX_STRING) : ("0x0" + HEX_STRING);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment