固定时间后在前后摄像头预览之间切换

时间:2017-06-17 11:54:16

标签: android

所以我在视图中显示前后摄像头预览,我想切换预览(从前到后,然后每2秒再次前面),无论我尝试什么,我都失败了。这是我尝试切换视图的代码(它甚至是在摄像机之间切换的正确位置吗?):

fatal: http://username@servernet:8000/scm/~user/filename.git/info/refs not valid: is this a git repository?

这是我尝试做的完整课程(使用开关更改相机预览):

@Override
public void surfaceCreated(SurfaceHolder holder) {
    // The Surface has been created, now tell the camera where to draw the preview.
    try {
        // infinite for loop - doesn't work
        if(camIter == 1){
            cleanup();
            initCamera(getContext());
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
            camIter = 2;
            // Thread.sleep(2000); - Doesn't work!
        }
        if(camIter == 2){
            cleanup();
            initCamera(getContext());
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
            camIter = 1;
            // Thread.sleep(2000); - Doesn't work!
        }                

    } catch (IOException e) {
        Log.d(TAG, "CameraExample: " + "Error setting camera preview: " + e.getMessage());
    }
}

如何做到这一点?谢谢!

1 个答案:

答案 0 :(得分:0)

Handler h = new Handler();
int delay =2000; //milliseconds

//action will perform every two seconds 
h.postDelayed(new Runnable(){
    public void run(){
        switchCamera();
        h.postDelayed(this, delay);
    }
}, delay);

private void switchCamera() {
        cleanup();
        initCamera(getContext());
        mCamera.setPreviewDisplay(holder);
        mCamera.startPreview();
        camIter = camIter == 1 ? 2 : 1;
}
相关问题