Search in sources :

Example 36 with CameraDevice

use of android.hardware.camera2.CameraDevice in project robolectric by robolectric.

the class ShadowCameraDeviceImplTest method createCaptureSession_configuration_throwsIllegalStateExceptionAfterClose.

@Test
@Config(sdk = VERSION_CODES.P)
public void createCaptureSession_configuration_throwsIllegalStateExceptionAfterClose() throws CameraAccessException {
    cameraDevice.close();
    SessionConfiguration configuration = new SessionConfiguration(SessionConfiguration.SESSION_REGULAR, Collections.singletonList(new OutputConfiguration(mock(Surface.class))), MoreExecutors.directExecutor(), new CaptureSessionCallback(/*useExecutor=*/
    true));
    IllegalStateException thrown = assertThrows(IllegalStateException.class, () -> cameraDevice.createCaptureSession(configuration));
    assertThat(thrown).hasMessageThat().contains("CameraDevice was already closed");
}
Also used : OutputConfiguration(android.hardware.camera2.params.OutputConfiguration) SessionConfiguration(android.hardware.camera2.params.SessionConfiguration) Surface(android.view.Surface) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Example 37 with CameraDevice

use of android.hardware.camera2.CameraDevice in project collect by opendatakit.

the class Camera2VideoFragment method startPreview.

/**
 * Start the camera preview.
 */
private void startPreview() {
    if (null == cameraDevice || !textureView.isAvailable() || null == previewSize) {
        return;
    }
    try {
        closePreviewSession();
        SurfaceTexture texture = textureView.getSurfaceTexture();
        assert texture != null;
        texture.setDefaultBufferSize(previewSize.getWidth(), previewSize.getHeight());
        previewBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        Surface previewSurface = new Surface(texture);
        previewBuilder.addTarget(previewSurface);
        cameraDevice.createCaptureSession(Collections.singletonList(previewSurface), new CameraCaptureSession.StateCallback() {

            @Override
            public void onConfigured(@NonNull CameraCaptureSession session) {
                previewSession = session;
                updatePreview();
            }

            @Override
            public void onConfigureFailed(@NonNull CameraCaptureSession session) {
                Activity activity = getActivity();
                if (null != activity) {
                    ToastUtils.showShortToast(getActivity(), "Failed");
                }
            }
        }, backgroundHandler);
    } catch (CameraAccessException e) {
        Timber.e(e);
    }
}
Also used : CameraAccessException(android.hardware.camera2.CameraAccessException) SurfaceTexture(android.graphics.SurfaceTexture) Activity(android.app.Activity) CameraCaptureSession(android.hardware.camera2.CameraCaptureSession) Surface(android.view.Surface)

Example 38 with CameraDevice

use of android.hardware.camera2.CameraDevice in project collect by opendatakit.

the class Camera2VideoFragment method updatePreview.

/**
 * Update the camera preview. {@link #startPreview()} needs to be called in advance.
 */
private void updatePreview() {
    if (null == cameraDevice) {
        return;
    }
    try {
        setUpCaptureRequestBuilder(previewBuilder);
        HandlerThread thread = new HandlerThread("CameraPreview");
        thread.start();
        previewSession.setRepeatingRequest(previewBuilder.build(), null, backgroundHandler);
    } catch (CameraAccessException e) {
        Timber.e(e);
    }
}
Also used : CameraAccessException(android.hardware.camera2.CameraAccessException) HandlerThread(android.os.HandlerThread)

Example 39 with CameraDevice

use of android.hardware.camera2.CameraDevice in project MazeSolver by Thukor.

the class Camera2 method createCameraPreview.

protected void createCameraPreview() {
    try {
        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(Camera2.this, "Configuration change", Toast.LENGTH_SHORT).show();
            }
        }, null);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}
Also used : CameraAccessException(android.hardware.camera2.CameraAccessException) SurfaceTexture(android.graphics.SurfaceTexture) CameraCaptureSession(android.hardware.camera2.CameraCaptureSession) Surface(android.view.Surface)

Aggregations

CameraCaptureSession (android.hardware.camera2.CameraCaptureSession)21 Surface (android.view.Surface)12 CameraAccessException (android.hardware.camera2.CameraAccessException)11 CameraDevice (android.hardware.camera2.CameraDevice)10 BlockingSessionCallback (com.android.ex.camera2.blocking.BlockingSessionCallback)10 Size (android.util.Size)7 CameraCharacteristics (android.hardware.camera2.CameraCharacteristics)6 Rect (android.graphics.Rect)5 CameraMetadataNative (android.hardware.camera2.impl.CameraMetadataNative)5 MeteringRectangle (android.hardware.camera2.params.MeteringRectangle)5 Range (android.util.Range)5 Activity (android.app.Activity)4 SurfaceTexture (android.graphics.SurfaceTexture)4 CameraManager (android.hardware.camera2.CameraManager)3 CameraDeviceImpl (android.hardware.camera2.impl.CameraDeviceImpl)3 Handler (android.os.Handler)3 Context (android.content.Context)2 Point (android.graphics.Point)2 StateCallback (android.hardware.camera2.CameraDevice.StateCallback)2 CaptureRequest (android.hardware.camera2.CaptureRequest)2