cscott / intent-addon

Plugin for Firefox/Android which exposes a web api for sending Android Intents.
7 stars 3 forks source link

How to make array of int? #6

Open Noitidart opened 9 years ago

Noitidart commented 9 years ago

I am not able to figure out how to make an array?

I keep getting a bunch of errors, like new is not a function of intArray. I also tried intArray(4*width*height) and also intArray(4*width*height) but cant get it to work :(

var bmp = Bitmap.createBitmap(width, height, Bitmap_Config.ARGB_8888);
console.info('bmp:', bmp);

var intArray = JNI.jint.array;
var rgba = intArray(4 * width * height);
console.info('rgba:', rgba.toString());
// bmp.getPixels(rgba, 0, width * 4, 0, 0, width, height);

I was trying to follow your example here: https://github.com/cscott/intent-addon/blob/338deef9fb5dbca9bdb62d85ad8020c49cf6bb97/lib/main.js#L96-L120

            var StringArray = JNI.classes.java.lang.String.array;
            var sa = StringArray.new(value.length);
            sa.setElements(0, value);
Noitidart commented 9 years ago

I got it to make an array I think:

var rgba = JNI.jint.array(4 * width * height)();
console.info('rgba:', rgba.toString());

However this is where it crashes:

bmp.getPixels(rgba, 0, width * 4, 0, 0, width, height);
console.info('rgba:', rgba.toString());

My full code is here: https://gist.github.com/Noitidart/e988e060e1e4942e38ce

Final step baby!

Noitidart commented 9 years ago

I'm thinking maybe the getPixels function is throwing one of the errors from the documentation: http://developer.android.com/reference/android/graphics/Bitmap.html#getPixels%28int[],%20int,%20int,%20int,%20int,%20int,%20int%29

Either IllegalArgumentException or ArrayIndexOutOfBoundsException does JNI.jsm crash on exception thrown? Or does it log it to console?

Noitidart commented 9 years ago

I might be creating the array wrong, as when you do hte var sa = StringArray.new it returns:

http://mxr.mozilla.org/mozilla-central/source/mobile/android/modules/JNI.jsm#1047 which is return wrap(j[constructor].call(j, jenv, length), classSig); im not sure if var rgba = JNI.jint.array(#) is same as that. May you please review my array creation and feed to getPixels

Noitidart commented 9 years ago

I started the JNI documention, im still crashing on this getPixels code if you could please help me with that, that would be awesome.

JNI documentation: https://developer.mozilla.org/en-US/docs/Mozilla/js-ctypes/Examples/Using_JNI_from_js-ctypes

Copy paste code where getPixel is problem: https://gist.github.com/Noitidart/cb6e7c3a102784d04262/276832e9707470ce7a30b7239d06a2928422e9c4#file-_ff-addon-snippet-jniscreenshot-js-L144

I tried this code with the *4 too but still crashing :(

Noitidart commented 9 years ago

Hi there Dr I know you are probably busy but I am really stuck on the getPixels line of this code, I was wondering if you could please help me out.

I released version 1 of my addon without Android support, it works on desktop Linux, Mac, and Windows check it out please I think you'll like it and maybe be more into helping me bring it to Android :) https://addons.mozilla.org/en-US/firefox/addon/nativeshot/

Will just need few minutes of your time please, nothing too much.

Noitidart commented 9 years ago

Hi there @cscott is this correct way to make an array of int? Its not causing crash anymore:

JNI.LoadClass(my_jenv, '[' + SIG.int);
var IntArray = JNI.classes.int.array;
var rgba = IntArray.new(width * height);
// var rgba = JNI.jint.array(width * height)(); // bad way, which was causing crash

console.info('rgba:', rgba['js#obj'].toString());
bmp.getPixels(rgba, 0, width, 0, 0, width, height);
console.info('rgba:', rgba['js#obj'].toString());