android相机clickat固定频率

时间:2012-12-02 06:17:54

标签: android multithreading buttonclick

我正在编写一个应该具有以下用例的应用:用户点击“开始!”按钮和应用程序每1分钟开始点击图片(内部使用CameraPreview类),并将按钮文本更改为“完成!”。

当用户按下相同的按钮时(但是新的按钮文字'完成!'),该应用程序应该停止。

当用户点击“开始!”时,我已经能够编写每分钟点击图片的代码按钮。但是,一旦图像捕获开始,按钮就会冻结。

如何以按钮不冻结的方式运行我的图像捕获 - 每1分钟逻辑?有什么最好的做法?谢谢!

以下是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();
    }
}

在代码中使用相同的按钮 - 只是我们根据按钮文本决定做什么。

1 个答案:

答案 0 :(得分:2)

不要阻止UI线程,切换到Runnable。你也每秒拍一张照片:1000是一秒,60000是一分钟。

首先创建一个新的字段变量:

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

然后更改您的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...");
        surfaceView.post(takePictures);
    }
    else if(buttonText.equals(Constant.doneTrainingButtonText)) {
        Log.i(TAG, "Robot Training completed...");
        surfaceView.removeCallbacks(takePictures);
        button.setText(Constant.trainButtonText);
        button.clearFocus();
    }
}