简体   繁体   中英

How to pass Mat object from java class to jni cpp.

I just want to pass a Mat object from java class to jni and required changes made in jni and return to java class. This is my sample project and get some issues...

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

This is my 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;

      }

i build above code, i get issue

D:/OpenCV-2.4.9-android-sdk\\sdk\\native\\jni/include/opencv2\\core\\core.hpp:56:21: fatal error: algorithm: No such file or directory #include ^ compilation terminated. make.exe: *** [obj/local/armeabi/objs/example1/image-mat.o] Error 1

just create a Application.mk file in jni folder.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM