我正在使用 OpenCV 教程 3 代码(相机控制)(请参阅OpenCV4Android 示例onPictureTaken()
)作为我的代码的基础,并且一旦图像保存在该方法中,就需要能够打开和操作图像。在操作过程中,我希望能够显示对图像的更改并使用 发布消息Toast
,这两者都需要我在主线程中。
根据迄今为止收到的建议,我正在BroadcastReceiver
尝试使用Activity
由sendBroadcast()
.JavaCameraView
我已更新 ,Activity
以包含BroadcastReceiver
, 并注册和注销它,如下所示:
public class Tutorial3Activity extends Activity implements CvCameraViewListener2, OnTouchListener {
:
private ActivityReceiver activityReceiver;
:
@Override
public void onCreate(Bundle savedInstanceState) {
:
IntentFilter intentFilter = new IntentFilter();
this.registerReceiver(activityReceiver, intentFilter);
:
}
:
public void onDestroy() {
:
this.unregisterReceiver(activityReceiver);
:
}
:
private class ActivityReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "BroadcastReceiver triggered");
if (intent.getAction().equals(Tutorial3View.IMAGE_READY)) {
Log.i(TAG, "BroadcastReceiver received");
}
}
}
}
sendBroadcast()
调用JavaCameraView
如下:
public class Tutorial3View extends JavaCameraView {
:
public static final String IMAGE_READY = "IMAGE_READY";
private Context context;
:
public Tutorial3View(Context context, AttributeSet attrs) {
:
this.context = context;
:
}
:
public void takePicture(final String fileName) {
PictureCallback callback = new PictureCallback() {
private String mPictureFileName = fileName;
@Override
public void onPictureTaken(byte[] data, Camera camera) {
:
image_read();
:
}
};
mCamera.takePicture(null, null, callback);
}
:
public void image_read() {
Log.i(TAG, "BroadcastReceiver - send");
Intent i = new Intent();
i.setAction(IMAGE_READY);
context.sendBroadcast(i);
Log.i(TAG, "BroadcastReceiver - sent");
}
}
其中Manifest
包括BroadcastReceiver
如下:
<receiver android:name="ActivityReceiver">
<intent-filter android:label="IMAGE_READY"></intent-filter>
</receiver>
当我拍照时,LogCat 显示:
03-30 17:22:53.410: I/Sample::Tutorial3View(3174): BroadcastReceiver - send
03-30 17:22:53.410: I/Sample::Tutorial3View(3174): BroadcastReceiver - sent
BroadcastReceiver triggered
但是or没有日志BroadcastReceiver received
。
有人可以告知为什么没有收到广播吗?
谢谢