问题
I want to implement invert effect in image processing. I decode color channels in java side the I pass a 2D array to C side, I invert (255-value) then I return a processed 2D array.
Here is my C code:
#include <jni.h>
#include<stddef.h>
#include <stdio.h>
#include<com_example_invert_MainActivity.h>
JNIEXPORT jobjectArray JNICALL Java_com_example_invert_MainActivity_inv
(JNIEnv *env, jobject obj, jobjectArray arr, jint w, jint h)
{
double a[w][h][3];
int i,j,k;
double x = 0;
///////////////////READING THE INPUT ARRAY////////////////////////
jsize dim1 = (*env)->GetArrayLength(env, arr);
for (i=0; i<dim1; i++){
jdoubleArray *line1 = (*env)->GetObjectArrayElement(env, arr, i);
int dim2 = (*env)->GetArrayLength(env, line1);
jdouble *pos1 = (*env)->GetDoubleArrayElements(env, line1, 0);
for (j=0; j<dim2; j++){
jdoubleArray *line2 = (*env)->GetObjectArrayElement(env, line1, j);
int dim3 = (*env)->GetArrayLength(env, line2);
jdouble *pos2 = (*env)->GetDoubleArrayElements(env, line2, 0);
for (k=0; k<dim3; k++){
a[i][j][k]= pos2[k];
}
(*env)->ReleaseDoubleArrayElements(env, arr, pos2, 0);
(*env)->ReleaseDoubleArrayElements(env, arr, line2, 0);
}
(*env)->ReleaseDoubleArrayElements(env, arr, pos1, 0);
(*env)->ReleaseDoubleArrayElements(env, arr, line1, 0);
}
/////////////////PROCESSING...///////////////////
for( i = 0; i<w; i++){
for( j = 0; j<h; j++){
for( k = 0; k<3; k++){
a[i][j][k] = 255-a[i][j][k];
}
}
}
//////////////RETURNING THE ARRAY////////////////////////////
jclass doubleArrayArrayClass = (*env)->FindClass(env,"[[D");
jclass doubleArrayClass = (*env)->FindClass(env,"[D");
jobjectArray ret = (*env)->NewObjectArray(env,w, doubleArrayArrayClass, NULL);
for( i = 0; i<w; i++){
jobjectArray dim2 = (*env)->NewObjectArray(env, w, doubleArrayClass, NULL);
for( j = 0; j<h; j++) {
jdoubleArray dim1 = (*env)->NewDoubleArray(env,h);
jdouble tmp[256];
for( k = 0; k<3; k++){
tmp[k] = a[i][j][k];
}
(*env)->SetDoubleArrayRegion(env,dim1 , 0, 3, tmp);
(*env)->SetObjectArrayElement(env, dim2, j, dim1);
(*env)->DeleteLocalRef(env, dim1);
}
(*env)->SetObjectArrayElement(env,ret, i, dim2);
(*env)->DeleteLocalRef(env,dim2);
}
return ret;
}
And here is the java code:
public class MainActivity extends ActionBarActivity {
ImageView imageView2;
double[][][] imgArray;
int w,h;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView2 = (ImageView) findViewById(R.id.imageView1);
imageView2.setDrawingCacheEnabled(true);
BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView2.getDrawable();
final Bitmap bitmap = bitmapDrawable.getBitmap();
Button button = (Button) findViewById(R.id.button1);
w = bitmap.getWidth();
h = bitmap.getHeight();
imgArray = new double[w][h][3];
for(int i = 0 ; i<w; i++){
for(int j =0; j<h; j++){
imgArray[i][j][0] = Color.red(bitmap.getPixel(i, j));
imgArray[i][j][1] = Color.green(bitmap.getPixel(i, j));
imgArray[i][j][2] = Color.blue(bitmap.getPixel(i, j));
}
}
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
imgArray = inv(imgArray, w, h);
Bitmap newBitmap = Bitmap.createBitmap(w,h,bitmap.getConfig());
for(int i = 0 ; i<w; i++){
for(int j =0; j<h; j++){
newBitmap.setPixel(i, j, Color.rgb((int)(imgArray[i][j][0]), (int)(imgArray[i][j][1]), (int)(imgArray[i][j][2])));
}
}
imageView2.setImageBitmap(newBitmap);
}
});
}
static{
System.loadLibrary("inv");
}
// internal, private
public native double[][][] inv(double[][][] inputArr, int w, int h);
...
}
The application crashes with logcat error: W/dalvikvm(5009): JNI: unpinPrimitiveArray(0x424eaea0) failed to find entry (valid=1)
回答1:
ReleaseDoubleArrayElements(A, B, 0) should be called only corresponding to B=GetDoubleArrayElements(env, A, 0). In your code, the arguments on Release… are wrong, (arr instead of line1, line2, etc.). They should never be called to match GetObjectArrayElement().
As an optimization notice, access to 3-dimensional array is much slower both in Java and JNI than access to 1-dimensional array of same size. Therefore I would strongly recommend to create in Java imgArray = new double[w*h*3] and work with it.
PS same applies to the output array.
PPS using SetDoubleArrayRegion(), you introduce an extra memcopy; better, use double* cArray = GetDoubleArrayElements(env, jArray, 0), put the values directly into cArray, and release it to Java with ReleaseDoubleArrayElements(env, jArray, cArray, 0). This 0 means that the changes to cArray will be seen in jArray on the Java side.
回答2:
I'm no JNI whizz, so here's a workaround: allocate the output array in Java. It goes something like this:
// the public API - a helper wrapper for the native `inv`:
public final int[][] invert( int[][] inputArr, int w, int h ) {
int [][] outputArr = new int[h][w]; // allocate the array in Java
inv( inputArr, outputArr, w, h );
return outputArr;
}
// internal, private
private native void inv(int[][] inputArr, int[][] outputArr , int w, int h);
And the C method signature would become:
JNIEXPORT jvoid JNICALL Java_com_example_invert_MainActivity_inv(
JNIEnv *env, jobject obj,
jobjectArray inputArr, // your original 'arr' parameter
jobjectArray outputArr, // new: the output to store the results in
jint w, jint h)
As for the C code, you can do away with all the allocation methods and just put values in outputArr using (*env)->SetIntArrayRegion(env, jline, 0, h, a[i]);. I'll leave that to you, as you probably know more about JNI than I do ;-)
回答3:
I am not going to comment on JNI, but would suggest an alternative to solve your problem on Android.
Your application is a good candidate for Android RenderScript. The image inverse algorithm can be implemented as a one-liner RenderScript kernel, which then can be launched from Java for parallel execution on GPU (or the multi-core CPU.)
This would not only lead to simpler code, but also faster code.
Here is a link to RenderScript. You can actually find an image inverse example there.
http://developer.android.com/guide/topics/renderscript/compute.html
来源:https://stackoverflow.com/questions/35186512/jni-unpinned-primitve-array-error