How do I send OpenCV Mat as a putExtra to Android Intent?

陌路散爱 提交于 2020-12-26 08:12:24

问题


I am trying to send either CvCameraViewFrame or Mat to another activity, but they don't implement Serializable or Parcelable and creating a wrapper class for them just to use it once seems like an overkill. How do I proceed?


回答1:


I would have used fragments instead of activities and get/set common Mat present in container Activity from fragments.

If there is a need to stick with multiple activities, assuming it's within process, options are

  1. Sharing - Use global Application subclass to get/set the Mat preferably in some thing like HashMap<String, WeakReference<Mat>> and passing HashMap's key string across activities(1). Make sure you stores a strong reference to the Mat before child activity completes onResume(), or else Mat could be garbage collected.

  2. Copying - Using getNativeObjAddr(2) and pass the long address value as part of invoking Intent. Child activity would recreate the Mat with the native address(3). Cloning Mat in child is necessary since parent activity could be killed any time after onResume of child activity is completed.

Sample below.

// In parent activity
Mat img = ...;
long addr = img.getNativeObjAddr();
Intent intent = new Intent(this, B.class);
intent.putExtra( "myImg", addr );
startActivity( intent );

//In child activity
long addr = intent.getLongExtra("myImg", 0);
Mat tempImg = new Mat( addr );
Mat img = tempImg.clone();  



回答2:


@Kiran is right.

You should get instance of Matrix using its native address.

long frameAddress = intent.getLongExtra("extra_name", 0);
Mat m = new Mat(frameAddress);


来源:https://stackoverflow.com/questions/29060376/how-do-i-send-opencv-mat-as-a-putextra-to-android-intent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!