Search code examples
androidmultithreadingbuttonclick

android camera clickat fixed frequency


I am writing an app which should have the following use case: User clicks the 'Start!' button and the app starts clicking pictures (using CameraPreview class internally) every 1 minute and changes the button text to 'Done!'.

When the user presses the same button (but with a new button text 'Done!'), the app should stop.

I have been able to write the code which clicks the picture every one minute when the user clicks the 'Start!' button. But, once the image capturing starts, the button freezes.

How can I run my image-capture-every-1-minute logic in a way that the button doesn't freeze? What are the best practices around that? Thanks!

Here is the code for OnClick():

    @Override
public void onClick(View v) {
    Button button = (Button)v;
    String buttonText = button.getText().toString();

    if(buttonText.equals(Constant.trainButtonText)) {
        Log.i(TAG, "Robot Training started...");

        while(true) {
            surfaceView.capture(new Camera.PictureCallback() {
                public void onPictureTaken(byte[] data, Camera camera) {
                    Log.v("Still", "Image data received from camera");

                    String[] params = new String[] {
                            Constant.Server, // Server URL
                            Long.toString(new Date().getTime()), // Image Timestamp
                            Constant.userId // Unique user ID for each customer
                    };
                    new UploadImageToWebServiceTask(data).execute(params);
                    camera.startPreview();
                }
            });
            try {
                // Capture every 1 minute until 'training done!' is not clicked
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
    else if(buttonText.equals(Constant.doneTrainingButtonText)) {
        Log.i(TAG, "Robot Training completed...");
        button.setText(Constant.trainButtonText);
        button.clearFocus();
    }
}

In the code the same button is used - only that we decide what to do based on the button text.


Solution

  • Don't block the UI thread, switch to a Runnable. Also you are taking a picture every second: 1000 is one second, 60000 is one minute.

    First create a new field variable:

    public class MainActivity extends Activity {
        Runnable takePictures = new Runnable() {
            @Override
            public void run() {
                // I'll trust that this code works
                surfaceView.capture(new Camera.PictureCallback() {
                    public void onPictureTaken(byte[] data, Camera camera) {
                        Log.v("Still", "Image data received from camera");
    
                        String[] params = new String[] {
                                Constant.skynetNNServer, // Server URL
                                Long.toString(new Date().getTime()), // Image Timestamp
                                Constant.userId // Unique user ID for each customer
                        };
                        new UploadImageToCloudTask(data).execute(params);
                        camera.startPreview();
                    }
                });
    
                // Call this runnable again in 60 seconds (60000 milliseconds) 
                surfaceView.postDelayed(this, 60000);
            }
        };
        // Rest of your code
    

    Then change your onClick method:

    @Override
    public void onClick(View v) {
        Button button = (Button)v;
        String buttonText = button.getText().toString();
    
        if(buttonText.equals(Constant.trainButtonText)) {
            Log.i(TAG, "Robot Training started...");
            surfaceView.post(takePictures);
        }
        else if(buttonText.equals(Constant.doneTrainingButtonText)) {
            Log.i(TAG, "Robot Training completed...");
            surfaceView.removeCallbacks(takePictures);
            button.setText(Constant.trainButtonText);
            button.clearFocus();
        }
    }