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

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

我正在尝试将 CvCameraViewFrame 或 Mat 发送到另一个 activity,但它们没有实现 Serializable 或 Parcelable 并为它们创建一个包装器 class 只是为了使用它一次似乎是矫枉过正。 我该如何进行?

我会使用片段而不是活动,get/set 常见 Mat 存在于容器 Activity 中来自片段。

如果需要坚持多项活动,假设它在流程内,选项是

  1. 共享 - 使用全局 Application 子类到 get/set Mat 最好在 [=14] =] 并跨活动传递 HashMap 的键字符串 (1)。确保在 child activity 完成 onResume() 之前存储对 Mat 的强引用,否则 Mat 可能会被垃圾回收。

  2. 复制 - 使用getNativeObjAddr(2) and pass the long address value as part of invoking Intent. Child activity would recreate the Mat with the native address(3)。在 child 中克隆 Mat 是必要的,因为 parent activity 可能在 child activity 的 onResume 完成后随时被杀死。

示例如下。

// 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 是对的。

您应该使用其本机地址获取 Matrix 的实例。

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