繁体   English   中英

如何将Mat对象从Java类传递到jni cpp。

[英]How to pass Mat object from java class to jni cpp.

我只想将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:致命错误:算法:没有这样的文件或目录#include ^编译终止。 make.exe:*** [obj / local / armeabi / objs / example1 / image-mat.o]错误1

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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