Example usage for android.hardware.camera2 CameraCaptureSession.StateCallback CameraCaptureSession.StateCallback

List of usage examples for android.hardware.camera2 CameraCaptureSession.StateCallback CameraCaptureSession.StateCallback

Introduction

In this page you can find the example usage for android.hardware.camera2 CameraCaptureSession.StateCallback CameraCaptureSession.StateCallback.

Prototype

CameraCaptureSession.StateCallback

Source Link

Usage

From source file:com.microsoft.projectoxford.emotionsample.RecognizeActivity.java

protected void createCameraPreview() {
    try {// www  .  j a va2  s .  c o  m
        SurfaceTexture texture = textureView.getSurfaceTexture();
        assert texture != null;
        texture.setDefaultBufferSize(imageDimension.getWidth(), imageDimension.getHeight());
        Surface surface = new Surface(texture);
        captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        captureRequestBuilder.addTarget(surface);
        cameraDevice.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback() {
            @Override
            public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
                //The camera is already closed
                if (null == cameraDevice) {
                    return;
                }
                // When the session is ready, we start displaying the preview.
                cameraCaptureSessions = cameraCaptureSession;
                updatePreview();
            }

            @Override
            public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
                Toast.makeText(RecognizeActivity.this, "Configuration change", Toast.LENGTH_SHORT).show();
            }
        }, null);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

From source file:com.example.aschere.cdhprototype2.Camera2RawFragment.java

/**
 * Creates a new {@link CameraCaptureSession} for camera preview.
 * <p/>//from   w  w  w .j  a va 2  s. c  o m
 * Call this only with {@link #mCameraStateLock} held.
 */
private void createCameraPreviewSessionLocked() {
    try {
        Log.i(TAG, "create camera preview called");
        SurfaceTexture texture = new SurfaceTexture(2);

        // This is the output Surface we need to start preview.
        Surface surface = new Surface(texture);

        // We set up a CaptureRequest.Builder with the output Surface.
        if (mCameraDevice == null) {
            setUpCameraOutputs();
            openCamera();
        }
        if (mCameraDevice != null) {
            mPreviewRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
            mPreviewRequestBuilder.addTarget(surface);

            // Here, we create a CameraCaptureSession for camera preview.
            mCameraDevice.createCaptureSession(Arrays.asList(surface, mJpegImageReader.get().getSurface(),
                    mRawImageReader.get().getSurface()), new CameraCaptureSession.StateCallback() {
                        @Override
                        public void onConfigured(CameraCaptureSession cameraCaptureSession) {
                            synchronized (mCameraStateLock) {
                                // The camera is already closed
                                if (null == mCameraDevice) {
                                    return;
                                }

                                try {
                                    setup3AControlsLocked(mPreviewRequestBuilder);
                                    // Finally, we start displaying the camera preview.
                                    cameraCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(),
                                            mPreCaptureCallback, mBackgroundHandler);
                                    mState = STATE_PREVIEW;
                                } catch (CameraAccessException | IllegalStateException e) {
                                    e.printStackTrace();
                                    return;
                                }
                                // When the session is ready, we start displaying the preview.
                                mCaptureSession = cameraCaptureSession;
                            }
                        }

                        @Override
                        public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) {
                            showToast("Failed to configure camera.");
                        }
                    }, mBackgroundHandler);
        }
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}