简体   繁体   English

如何将OpenCV Mat作为putExtra发送到Android Intent?

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

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. 我试图将CvCameraViewFrame或Mat发送到另一个活动,但他们没有实现Serializable或Parcelable并为它们创建一个包装类只是为了使用它曾经看起来像一个矫枉过正。 How do I proceed? 我该怎么办?

I would have used fragments instead of activities and get/set common Mat present in container Activity from fragments. 我会使用片段而不是活动,并从片段中获取/设置容器Activity中的公共Mat

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 ). 共享 - 使用全局Application子类来优先获取/设置Mat ,例如HashMap<String, WeakReference<Mat>> ,并在活动( 1 )中传递HashMap的键字符串。 Make sure you stores a strong reference to the Mat before child activity completes onResume() , or else Mat could be garbage collected. 确保在子活动完成onResume()之前存储对Mat的强引用,否则Mat可能被垃圾收集。

  2. Copying - Using getNativeObjAddr ( 2 ) and pass the long address value as part of invoking Intent. 复制 - 使用getNativeObjAddr2 )并传递long地址值作为调用Intent的一部分。 Child activity would recreate the Mat with the native address( 3 ). 子活动将使用本机地址重新创建Mat3 )。 Cloning Mat in child is necessary since parent activity could be killed any time after onResume of child activity is completed. 由于在完成子活动的onResume之后的任何时间都可以杀死父活动,因此必须在子项中克隆Mat

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

@Kiran is right. @Kiran是对的。

You should get instance of Matrix using its native address. 您应该使用其本机地址获取Matrix的实例。

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

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

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