如何将Mat对象从java类传递给jni cpp。

时间:2015-04-08 09:40:19

标签: java android android-ndk

我只想将一个Mat对象从java类传递给jni,并且需要在jni中进行更改并返回到java类。这是我的示例项目并且遇到了一些问题......

public class MainActivity extends Activity {
Button btn;
ImageView img;
Mat m;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        img = (ImageView) findViewById(R.id.imageView1);
        btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                selectImage();
            }
        });


    }
    protected void selectImage() {
        // TODO Auto-generated method stub
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
        startActivityForResult(intent, 1);
    }
    public native Mat image_mat(long matAddr);
    /** Load the native library where the native method
    * is stored.
    */
    static {
          System.loadLibrary("image-mat");
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 1) {
                File f = new File(Environment.getExternalStorageDirectory()
                        .toString());
                for (File temp : f.listFiles()) {
                    if (temp.getName().equals("temp.jpg")) {
                        f = temp;
                        break;
                    }
                }
                try {
                    Bitmap bm;
                    BitmapFactory.Options btmapOptions = new BitmapFactory.Options();

                    bm = BitmapFactory.decodeFile(f.getAbsolutePath(),
                            btmapOptions);

                    // bm = Bitmap.createScaledBitmap(bm, 70, 70, true);
//                    img.setImageBitmap(bm);

                    String path = android.os.Environment
                            .getExternalStorageDirectory()
                            + File.separator
                            + "mk";
                    f.delete();
                    OutputStream fOut = null;
                    File file = new File(path, String.valueOf(System
                            .currentTimeMillis()) + ".jpg");

                    Bitmap imgMat;
                    BitmapFactory.Options bMat = new BitmapFactory.Options();
                    imgMat = BitmapFactory.decodeFile(file.getAbsolutePath(),bMat);

                    m = new Mat();
                    Mat ret = new Mat();
                    Utils.bitmapToMat(imgMat, m);
                   ret =  image_mat(m.getNativeObjAddr());
                   Bitmap bmp = Bitmap.createBitmap(640, 480, Bitmap.Config.ARGB_8888);  
                   Utils.matToBitmap(ret, bmp);  
}
}
}

这是我的jni

    JNIEXPORT jlong JNICALL
      Java_com_example_matusingnative_MainActivity_image_mat
      (JNIEnv *env, jobject obj, jlong matimage)
      {
          cv::Mat *jni_image  = (cv::Mat*) matimage;

//            Mat *retval;

              return (jlong)jni_image;

      }

我构建上面的代码,我得到了问题

D:/OpenCV-2.4.9-android-sdk \ sdk \ native \ jni / include / opencv2 \ core \ core.hpp:56:21:致命错误:算法:没有这样的文件或目录  #包括                      ^ 编译终止。 make.exe:*** [obj / local / armeabi / objs / example1 / image-mat.o]错误1

1 个答案:

答案 0 :(得分:0)

只需在jni文件夹中创建一个Application.mk文件。

APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a
APP_PLATFORM := android-8