Skip to content

Instantly share code, notes, and snippets.

@irbull
Last active July 1, 2020 11:49
Show Gist options
  • Select an option

  • Save irbull/9d27185c94f7990570e1882d64140749 to your computer and use it in GitHub Desktop.

Select an option

Save irbull/9d27185c94f7990570e1882d64140749 to your computer and use it in GitHub Desktop.

Revisions

  1. irbull revised this gist Jul 27, 2016. 1 changed file with 25 additions and 25 deletions.
    50 changes: 25 additions & 25 deletions jimp example with J2V8.java
    Original file line number Diff line number Diff line change
    @@ -23,21 +23,21 @@ public class Exercise1 {
    public static void main(String[] args) throws IOException {
    final NodeJS nodeJS = NodeJS.createNodeJS();
    nodeJS.getRuntime().registerJavaMethod((V8Object receiver, V8Array parameters) -> {
    V8TypedArray typedArray = (V8TypedArray) parameters.get(0);
    ByteBuffer buffer = typedArray.getByteBuffer();
    try {
    for(int i = 0; i < buffer.limit(); i+=4) {
    int gray = createGrayscale(buffer, i);
    buffer.put(i, (byte) gray);
    buffer.put(i+1, (byte) gray);
    buffer.put(i+2, (byte) gray);
    };
    V8TypedArray typedArray = (V8TypedArray) parameters.get(0);
    ByteBuffer buffer = typedArray.getByteBuffer();
    try {
    for(int i = 0; i < buffer.limit(); i+=4) {
    int gray = createGrayscale(buffer, i);
    buffer.put(i, (byte) gray);
    buffer.put(i+1, (byte) gray);
    buffer.put(i+2, (byte) gray);
    };

    } finally {
    typedArray.release();
    }
    return null;
    }, "process_in_java");
    } finally {
    typedArray.release();
    }
    return null;
    }, "process_in_java");

    File script = createTemporaryScriptFile(JIMP_SCRIPT, "jimpscript.js");
    nodeJS.exec(script);
    @@ -70,15 +70,15 @@ public static void executeJSFunction(V8Object object, String name, Object... par
    }
    }

    private static File createTemporaryScriptFile(final String script, final String name) throws IOException {
    File tempFile = File.createTempFile(name, ".js");
    PrintWriter writer = new PrintWriter(tempFile, "UTF-8");
    try {
    writer.print(script);
    } finally {
    writer.close();
    }
    return tempFile;
    }

    private static File createTemporaryScriptFile(final String script, final String name) throws IOException {
    File tempFile = File.createTempFile(name, ".js");
    PrintWriter writer = new PrintWriter(tempFile, "UTF-8");
    try {
    writer.print(script);
    } finally {
    writer.close();
    }
    return tempFile;
    }
    }
  2. irbull revised this gist Jul 26, 2016. 1 changed file with 11 additions and 17 deletions.
    28 changes: 11 additions & 17 deletions jimp example with J2V8.java
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,6 @@
    import java.io.PrintWriter;
    import java.nio.ByteBuffer;

    import com.eclipsesource.v8.JavaCallback;
    import com.eclipsesource.v8.NodeJS;
    import com.eclipsesource.v8.Releasable;
    import com.eclipsesource.v8.V8Array;
    @@ -23,9 +22,7 @@ public class Exercise1 {

    public static void main(String[] args) throws IOException {
    final NodeJS nodeJS = NodeJS.createNodeJS();
    nodeJS.getRuntime().registerJavaMethod(new JavaCallback() {

    public Object invoke(V8Object receiver, V8Array parameters) {
    nodeJS.getRuntime().registerJavaMethod((V8Object receiver, V8Array parameters) -> {
    V8TypedArray typedArray = (V8TypedArray) parameters.get(0);
    ByteBuffer buffer = typedArray.getByteBuffer();
    try {
    @@ -37,22 +34,11 @@ public Object invoke(V8Object receiver, V8Array parameters) {
    };

    } finally {
    typedArray.release();

    typedArray.release();
    }
    return null;
    }
    }, "process_in_java");

    private int createGrayscale(ByteBuffer buffer, int i) {
    int red = 0xFF & buffer.get(i);
    int green = 0xFF & buffer.get(i+1);
    int blue = 0xFF & buffer.get(i+2);
    int gray = (int) (red + green + blue ) / 3;
    return gray;
    }
    }, "process_in_java");


    File script = createTemporaryScriptFile(JIMP_SCRIPT, "jimpscript.js");
    nodeJS.exec(script);

    @@ -61,6 +47,14 @@ private int createGrayscale(ByteBuffer buffer, int i) {
    }
    nodeJS.release();
    }

    private static int createGrayscale(ByteBuffer buffer, int i) {
    int red = 0xFF & buffer.get(i);
    int green = 0xFF & buffer.get(i+1);
    int blue = 0xFF & buffer.get(i+2);
    int gray = (int) (red + green + blue ) / 3;
    return gray;
    }

    public static void executeJSFunction(V8Object object, String name) {
    Object result = object.executeFunction(name, null);
  3. irbull revised this gist Jul 26, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion jimp example with J2V8.java
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@
    public class Exercise1 {

    private static String JIMP_SCRIPT = "var Jimp = require('/Users/irbull/node_modules/jimp');\n"
    + " Jimp.read('/Users/irbull/Downloads/IMG_20160706_175824.jpg', function (err, image) {\n"
    + " Jimp.read('/Users/irbull/Downloads/IMG_20160706_175824.jpg', (err, image) => {\n"
    + " if (err) throw err;\n"
    + " process_in_java(image.bitmap.data);\n"
    + " image.write('/Users/irbull/Downloads/output.jpg');\n"
  4. irbull renamed this gist Jul 25, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. irbull created this gist Jul 25, 2016.
    90 changes: 90 additions & 0 deletions jimp example with J2V8
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,90 @@
    package com.eclipsesource.j2v8.tutorial;

    import java.io.File;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.nio.ByteBuffer;

    import com.eclipsesource.v8.JavaCallback;
    import com.eclipsesource.v8.NodeJS;
    import com.eclipsesource.v8.Releasable;
    import com.eclipsesource.v8.V8Array;
    import com.eclipsesource.v8.V8Object;
    import com.eclipsesource.v8.V8TypedArray;

    public class Exercise1 {

    private static String JIMP_SCRIPT = "var Jimp = require('/Users/irbull/node_modules/jimp');\n"
    + " Jimp.read('/Users/irbull/Downloads/IMG_20160706_175824.jpg', function (err, image) {\n"
    + " if (err) throw err;\n"
    + " process_in_java(image.bitmap.data);\n"
    + " image.write('/Users/irbull/Downloads/output.jpg');\n"
    + " });\n";

    public static void main(String[] args) throws IOException {
    final NodeJS nodeJS = NodeJS.createNodeJS();
    nodeJS.getRuntime().registerJavaMethod(new JavaCallback() {

    public Object invoke(V8Object receiver, V8Array parameters) {
    V8TypedArray typedArray = (V8TypedArray) parameters.get(0);
    ByteBuffer buffer = typedArray.getByteBuffer();
    try {
    for(int i = 0; i < buffer.limit(); i+=4) {
    int gray = createGrayscale(buffer, i);
    buffer.put(i, (byte) gray);
    buffer.put(i+1, (byte) gray);
    buffer.put(i+2, (byte) gray);
    };

    } finally {
    typedArray.release();

    }
    return null;
    }

    private int createGrayscale(ByteBuffer buffer, int i) {
    int red = 0xFF & buffer.get(i);
    int green = 0xFF & buffer.get(i+1);
    int blue = 0xFF & buffer.get(i+2);
    int gray = (int) (red + green + blue ) / 3;
    return gray;
    }
    }, "process_in_java");


    File script = createTemporaryScriptFile(JIMP_SCRIPT, "jimpscript.js");
    nodeJS.exec(script);

    while(nodeJS.isRunning()) {
    nodeJS.handleMessage();
    }
    nodeJS.release();
    }

    public static void executeJSFunction(V8Object object, String name) {
    Object result = object.executeFunction(name, null);
    if (result instanceof Releasable) {
    ((Releasable) result).release();
    }
    }

    public static void executeJSFunction(V8Object object, String name, Object... params) {
    Object result = object.executeJSFunction(name, params);
    if (result instanceof Releasable) {
    ((Releasable) result).release();
    }
    }

    private static File createTemporaryScriptFile(final String script, final String name) throws IOException {
    File tempFile = File.createTempFile(name, ".js");
    PrintWriter writer = new PrintWriter(tempFile, "UTF-8");
    try {
    writer.print(script);
    } finally {
    writer.close();
    }
    return tempFile;
    }

    }