Search in sources :

Example 1 with CameraController2

use of net.sourceforge.opencamera.CameraController.CameraController2 in project OpenCamera by ageback.

the class Preview method openCameraCore.

/**
 * Open the camera - this should be called from background thread, to avoid hogging the UI thread.
 */
private CameraController openCameraCore(int cameraId) {
    long debug_time = 0;
    if (MyDebug.LOG) {
        Log.d(TAG, "openCameraCore()");
        debug_time = System.currentTimeMillis();
    }
    // We pass a camera controller back to the UI thread rather than assigning to camera_controller here, because:
    // * If we set camera_controller directly, we'd need to synchronize, otherwise risk of memory barrier issues
    // * Risk of race conditions if UI thread accesses camera_controller before we have called cameraOpened().
    CameraController camera_controller_local;
    try {
        if (MyDebug.LOG) {
            Log.d(TAG, "try to open camera: " + cameraId);
            Log.d(TAG, "openCamera: time before opening camera: " + (System.currentTimeMillis() - debug_time));
        }
        if (test_fail_open_camera) {
            if (MyDebug.LOG)
                Log.d(TAG, "test failing to open camera");
            throw new CameraControllerException();
        }
        CameraController.ErrorCallback cameraErrorCallback = new CameraController.ErrorCallback() {

            public void onError() {
                if (MyDebug.LOG)
                    Log.e(TAG, "error from CameraController: camera device failed");
                if (camera_controller != null) {
                    camera_controller = null;
                    camera_open_state = CameraOpenState.CAMERAOPENSTATE_CLOSED;
                    applicationInterface.onCameraError();
                }
            }
        };
        if (using_android_l) {
            CameraController.ErrorCallback previewErrorCallback = new CameraController.ErrorCallback() {

                public void onError() {
                    if (MyDebug.LOG)
                        Log.e(TAG, "error from CameraController: preview failed to start");
                    applicationInterface.onFailedStartPreview();
                }
            };
            camera_controller_local = new CameraController2(Preview.this.getContext(), cameraId, previewErrorCallback, cameraErrorCallback);
            if (applicationInterface.useCamera2FakeFlash()) {
                camera_controller_local.setUseCamera2FakeFlash(true);
            }
        } else
            camera_controller_local = new CameraController1(cameraId, cameraErrorCallback);
    // throw new CameraControllerException(); // uncomment to test camera not opening
    } catch (CameraControllerException e) {
        if (MyDebug.LOG)
            Log.e(TAG, "Failed to open camera: " + e.getMessage());
        e.printStackTrace();
        camera_controller_local = null;
    }
    if (MyDebug.LOG) {
        Log.d(TAG, "openCamera: total time for openCameraCore: " + (System.currentTimeMillis() - debug_time));
    }
    return camera_controller_local;
}
Also used : CameraController(net.sourceforge.opencamera.CameraController.CameraController) CameraController2(net.sourceforge.opencamera.CameraController.CameraController2) CameraController1(net.sourceforge.opencamera.CameraController.CameraController1) CameraControllerException(net.sourceforge.opencamera.CameraController.CameraControllerException)

Aggregations

CameraController (net.sourceforge.opencamera.CameraController.CameraController)1 CameraController1 (net.sourceforge.opencamera.CameraController.CameraController1)1 CameraController2 (net.sourceforge.opencamera.CameraController.CameraController2)1 CameraControllerException (net.sourceforge.opencamera.CameraController.CameraControllerException)1