Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

With Android, how do I pass control back to the main thread?

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 the 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. What do I need to add to both the Tutorial3Activity and Tutorial3View classes to allow me to pass that control back?

With Android, how do I pass control back to the main thread?

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 the an image once it has been saved in the onPictureTaken onPictureTaken() method. During the manipulation I want to be able to display the changes to the image and post messages using Toast, Toast, both of which require me to be in the main thread. What do

Based on advice received to date, I need to add to both the 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 and 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 classes to allow me to pass that control back?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