JNI- unpinned primitve数组错误

我想在图像处理中实现反转效果。 我在java端解码颜色通道,我将2D数组传递给C端,我反转(255值)然后我返回一个处理过的2D数组。

这是我的C代码:

#include  #include #include  #include 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; iGetObjectArrayElement(env, arr, i); int dim2 = (*env)->GetArrayLength(env, line1); jdouble *pos1 = (*env)->GetDoubleArrayElements(env, line1, 0); for (j=0; jGetObjectArrayElement(env, line1, j); int dim3 = (*env)->GetArrayLength(env, line2); jdouble *pos2 = (*env)->GetDoubleArrayElements(env, line2, 0); for (k=0; kReleaseDoubleArrayElements(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; kFindClass(env,"[[D"); jclass doubleArrayClass = (*env)->FindClass(env,"[D"); jobjectArray ret = (*env)->NewObjectArray(env,w, doubleArrayArrayClass, NULL); for( i = 0; iNewObjectArray(env, w, doubleArrayClass, NULL); for( j = 0; jNewDoubleArray(env,h); jdouble tmp[256]; for( k = 0; kSetDoubleArrayRegion(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; } 

这是java代码:

 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); ... } 

应用程序崩溃并出现logcat错误: W/dalvikvm(5009): JNI: unpinPrimitiveArray(0x424eaea0) failed to find entry (valid=1)

应仅调用ReleaseDoubleArrayElements(A, B, 0)对应于B=GetDoubleArrayElements(env, A, 0) 。 在你的代码中, Release…上的参数是错误的,( arr而不是line1line2等)。 永远不应该调用它们来匹配GetObjectArrayElement()

作为优化通知,在Java和JNI中访问三维数组要比访问相同大小的一维数组慢得多。 因此我强烈建议在Java中创建imgArray = new double[w*h*3]并使用它。

PS同样适用于输出数组。

PPS使用SetDoubleArrayRegion() ,你引入了额外的memcopy; 更好的是,使用double* cArray = GetDoubleArrayElements(env, jArray, 0) ,将值直接放入cArray ,然后使用ReleaseDoubleArrayElements(env, jArray, cArray, 0)将其释放到Java。 这意味着在Java端的jArray中将看到对cArray的更改。

我不是JNI高手,所以这是一个解决方法:在Java中分配输出数组。 它是这样的:

 // 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); 

并且C方法签名将变为:

 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) 

至于C代码,你可以取消所有的分配方法,只需使用(*env)->SetIntArrayRegion(env, jline, 0, h, a[i]);将值放在(*env)->SetIntArrayRegion(env, jline, 0, h, a[i]); 。 我会把它留给你,因为你可能比我更了解JNI 😉

我不打算对JNI发表评论,但会建议在Android上解决您的问题。

您的应用程序是Android RenderScript的理想选择。 图像逆算法可以实现为单行RenderScript内核,然后可以从Java启动以在GPU(或多核CPU)上并行执行。

这不仅会导致更简单的代码,还会导致更快的代码。

这是RenderScript的链接。 你可以在那里找到一个图像反例。

http://developer.android.com/guide/topics/renderscript/compute.html