JNI- 未固定的原始数组错误

JNI- unpinned primitve array error

我想在图像处理中实现反转效果。我在 java 侧解码颜色通道,我将二维数组传递到 C 侧,我反转(255 值)然后我 return 一个处理过的二维数组。

这是我的 C 代码:

   #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;
}

这里是 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)

我不是 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]); 将值放入 outputArr。我会把它留给你,因为你可能比我更了解 JNI ;-)

我不会对 JNI 发表评论,但会建议一个替代方案来解决 Android 上的问题。

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

这不仅会使代码更简单,而且代码会更快。

这里是 RenderScript 的 link。您实际上可以在那里找到图像反例。

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

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

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

PS 同样适​​用于输出数组。

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