Last active
July 1, 2020 11:49
-
-
Save irbull/9d27185c94f7990570e1882d64140749 to your computer and use it in GitHub Desktop.
Revisions
-
irbull revised this gist
Jul 27, 2016 . 1 changed file with 25 additions and 25 deletions.There are no files selected for viewing
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 charactersOriginal 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); }; } 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; } } -
irbull revised this gist
Jul 26, 2016 . 1 changed file with 11 additions and 17 deletions.There are no files selected for viewing
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 charactersOriginal 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.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((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(); } return null; }, "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); -
irbull revised this gist
Jul 26, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal 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', (err, image) => {\n" + " if (err) throw err;\n" + " process_in_java(image.bitmap.data);\n" + " image.write('/Users/irbull/Downloads/output.jpg');\n" -
irbull renamed this gist
Jul 25, 2016 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
irbull created this gist
Jul 25, 2016 .There are no files selected for viewing
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 charactersOriginal 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; } }