With Android, how do I pass control back to the main thread? [closed]
I am using the OpenCV Tutorial 3 code (Camera Control) (see OpenCV4Android samples) as the basis for my code and need to be able to open and manipulate an image once it has been saved in the onPictureTaken()
method. During the manipulation I want to be able to display the changes to the image and post messages using Toast
, both of which require me to be in the main thread.
Based on advice received to date, I am trying to use a BroadcastReceiver
in the Activity
which is triggered by a sendBroadcast()
in the JavaCameraView
.
I have updated the Activity
to include the BroadcastReceiver
, and register and unregister it as follows:
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");
}
}
}
}
The sendBroadcast()
is invoked in the JavaCameraView
as follows:
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");
}
}
The Manifest
includes the BroadcastReceiver
as follows:
<receiver android:name="ActivityReceiver">
<intent-filter android:label="IMAGE_READY"></intent-filter>
</receiver>
When I take the picture, the LogCat shows:
03-30 17:22:53.410: I/Sample::Tutorial3View(3174): BroadcastReceiver - send
03-30 17:22:53.410: I/Sample::Tutorial3View(3174): BroadcastReceiver - sent
but there is no log for BroadcastReceiver triggered
or BroadcastReceiver received
.
Can someone advise why the broadcast isn't being received?
Thanks