0

我正在使用 OpenCV 教程 3 代码(相机控制)(请参阅OpenCV4Android 示例onPictureTaken())作为我的代码的基础,并且一旦图像保存在该方法中,就需要能够打开和操作图像。在操作过程中,我希望能够显示对图像的更改并使用 发布消息Toast,这两者都需要我在主线程中。

根据迄今为止收到的建议,我正在BroadcastReceiver尝试使用ActivitysendBroadcast().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

有人可以告知为什么没有收到广播吗?

谢谢

4

2 回答 2

1

您需要调用该runOnUiThread()方法:

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        // stuff to be done on UI Thread
    }
});
于 2013-03-30T12:04:51.637 回答
0

您可以使用AsyncTask来执行此操作。

用于doInBackground()进行背景图像处理操作,完成后显示里面的 Toast 消息onPostExecute()

于 2013-03-30T12:12:23.827 回答