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?
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
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.
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();