问题
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
Sharing - Use global
Application
subclass to get/set theMat
preferably in some thing likeHashMap<String, WeakReference<Mat>>
and passing HashMap's key string across activities(1). Make sure you stores a strong reference to theMat
before child activity completesonResume()
, or elseMat
could be garbage collected.Copying - Using
getNativeObjAddr
(2) and pass thelong
address value as part of invoking Intent. Child activity would recreate theMat
with the native address(3). CloningMat
in child is necessary since parent activity could be killed any time afteronResume
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