如何将OpenCV Mat作为putExtra发送到Android Intent? [英] How do I send OpenCV Mat as a putExtra to Android Intent?
问题描述
我正在尝试将CvCameraViewFrame或Mat发送到另一个活动,但是他们没有实现Serializable或Parcelable,并且为他们创建一个包装器类只是为了使用它一次似乎太过分了.我该如何进行?
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?
推荐答案
我会使用片段代替活动,并从片段中获取/设置容器活动中存在的常见 Mat
.
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
-
共享-使用全局
Application
子类来获取/设置Mat
,最好使用HashMap< String之类的东西,WeakReference< Mat>>
并在活动之间传递HashMap的键字符串(
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.
复制-使用 getNativeObjAddr
(Mat rel ="nofollow noreferrer"> 3 ).必须在子级中克隆 Mat
,因为父级活动可以在子级活动的 onResume
完成后随时终止.
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.
下面的示例.
// 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();
这篇关于如何将OpenCV Mat作为putExtra发送到Android Intent?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!