Search in sources :

Example 16 with CameraCaptureSession

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

the class Camera2Fragment method captureStillPicture.

/**
 * Capture a still picture. This method should be called when we get a response in
 * {@link #captureCallback} from both {@link #lockFocus()}.
 */
private void captureStillPicture() {
    try {
        final Activity activity = getActivity();
        if (null == activity || null == cameraDevice) {
            return;
        }
        // This is the CaptureRequest.Builder that we use to take a picture.
        final CaptureRequest.Builder captureBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
        captureBuilder.addTarget(imageReader.getSurface());
        // Use the same AE and AF modes as the preview.
        captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
        setAutoFlash(captureBuilder);
        // Orientation
        int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
        captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(rotation));
        CameraCaptureSession.CaptureCallback captureCallback = new CameraCaptureSession.CaptureCallback() {

            @Override
            public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
                unlockFocus();
                activity.setResult(Activity.RESULT_OK);
                activity.finish();
            }
        };
        captureSession.stopRepeating();
        captureSession.capture(captureBuilder.build(), captureCallback, null);
    } catch (CameraAccessException e) {
        Timber.e(e);
    }
}
Also used : CameraAccessException(android.hardware.camera2.CameraAccessException) NonNull(androidx.annotation.NonNull) Activity(android.app.Activity) TotalCaptureResult(android.hardware.camera2.TotalCaptureResult) CaptureRequest(android.hardware.camera2.CaptureRequest) CameraCaptureSession(android.hardware.camera2.CameraCaptureSession) SuppressLint(android.annotation.SuppressLint) Point(android.graphics.Point)

Example 17 with CameraCaptureSession

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

the class Camera2Fragment method createCameraPreviewSession.

/**
 * Creates a new {@link CameraCaptureSession} for camera preview.
 */
private void createCameraPreviewSession() {
    try {
        SurfaceTexture texture = textureView.getSurfaceTexture();
        assert texture != null;
        // We configure the size of default buffer to be the size of camera preview we want.
        texture.setDefaultBufferSize(previewSize.getWidth(), previewSize.getHeight());
        // 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.
        previewRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        previewRequestBuilder.addTarget(surface);
        // Here, we create a CameraCaptureSession for camera preview.
        cameraDevice.createCaptureSession(Arrays.asList(surface, imageReader.getSurface()), 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.
                captureSession = cameraCaptureSession;
                try {
                    // Auto focus should be continuous for camera preview.
                    previewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
                    // Flash is automatically enabled when necessary.
                    setAutoFlash(previewRequestBuilder);
                    // Finally, we start displaying the camera preview.
                    previewRequest = previewRequestBuilder.build();
                    captureSession.setRepeatingRequest(previewRequest, captureCallback, backgroundHandler);
                    getActivity().runOnUiThread(() -> textureView.setOnClickListener(Camera2Fragment.this));
                } catch (CameraAccessException e) {
                    Timber.e(e);
                }
            }

            @Override
            public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
            }
        }, null);
    } catch (CameraAccessException e) {
        Timber.e(e);
    }
}
Also used : CameraAccessException(android.hardware.camera2.CameraAccessException) SurfaceTexture(android.graphics.SurfaceTexture) CameraCaptureSession(android.hardware.camera2.CameraCaptureSession) Surface(android.view.Surface)

Example 18 with CameraCaptureSession

use of android.hardware.camera2.CameraCaptureSession in project material-camera by afollestad.

the class Camera2Fragment method captureStillPicture.

/**
     * Capture a still picture. This method should be called when we get a response in
     * {@link #mCaptureCallback} from both {@link #takeStillshot()}.
     */
private void captureStillPicture() {
    try {
        final Activity activity = getActivity();
        if (null == activity || null == mCameraDevice) {
            return;
        }
        // This is the CaptureRequest.Builder that we use to take a picture.
        final CaptureRequest.Builder captureBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
        captureBuilder.addTarget(mImageReader.getSurface());
        // Use the same AE and AF modes as the preview.
        captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
        setFlashMode(captureBuilder);
        // Orientation
        CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
        CameraCharacteristics characteristics = manager.getCameraCharacteristics(mCameraDevice.getId());
        //noinspection ConstantConditions,ResourceType
        @Degrees.DegreeUnits final int sensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
        int displayRotation = activity.getWindowManager().getDefaultDisplay().getRotation();
        // default camera orientation used to be 90 degrees, for Nexus 5X, 6P it is 270 degrees
        if (sensorOrientation == Degrees.DEGREES_270) {
            displayRotation += 2 % 3;
        }
        captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, ORIENTATIONS.get(displayRotation));
        CameraCaptureSession.CaptureCallback CaptureCallback = new CameraCaptureSession.CaptureCallback() {

            @Override
            public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
                Log.d("stillshot", "onCaptureCompleted");
                unlockFocus();
            }
        };
        mPreviewSession.stopRepeating();
        mPreviewSession.capture(captureBuilder.build(), CaptureCallback, null);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}
Also used : Activity(android.app.Activity) CameraManager(android.hardware.camera2.CameraManager) TotalCaptureResult(android.hardware.camera2.TotalCaptureResult) CameraCaptureSession(android.hardware.camera2.CameraCaptureSession) Point(android.graphics.Point) CameraAccessException(android.hardware.camera2.CameraAccessException) NonNull(android.support.annotation.NonNull) CameraCharacteristics(android.hardware.camera2.CameraCharacteristics) CaptureRequest(android.hardware.camera2.CaptureRequest)

Example 19 with CameraCaptureSession

use of android.hardware.camera2.CameraCaptureSession in project platform_frameworks_base by android.

the class CameraTestUtils method configureCameraSession.

/**
     * Configure a new camera session with output surfaces and type.
     *
     * @param camera The CameraDevice to be configured.
     * @param outputSurfaces The surface list that used for camera output.
     * @param listener The callback CameraDevice will notify when capture results are available.
     */
public static CameraCaptureSession configureCameraSession(CameraDevice camera, List<Surface> outputSurfaces, boolean isHighSpeed, CameraCaptureSession.StateCallback listener, Handler handler) throws CameraAccessException {
    BlockingSessionCallback sessionListener = new BlockingSessionCallback(listener);
    if (isHighSpeed) {
        camera.createConstrainedHighSpeedCaptureSession(outputSurfaces, sessionListener, handler);
    } else {
        camera.createCaptureSession(outputSurfaces, sessionListener, handler);
    }
    CameraCaptureSession session = sessionListener.waitAndGetSession(SESSION_CONFIGURE_TIMEOUT_MS);
    assertFalse("Camera session should not be a reprocessable session", session.isReprocessable());
    String sessionType = isHighSpeed ? "High Speed" : "Normal";
    assertTrue("Capture session type must be " + sessionType, isHighSpeed == CameraConstrainedHighSpeedCaptureSession.class.isAssignableFrom(session.getClass()));
    return session;
}
Also used : BlockingSessionCallback(com.android.ex.camera2.blocking.BlockingSessionCallback) CameraCaptureSession(android.hardware.camera2.CameraCaptureSession)

Example 20 with CameraCaptureSession

use of android.hardware.camera2.CameraCaptureSession in project platform_frameworks_base by android.

the class CameraCaptureSessionImpl method getDeviceStateCallback.

/**
     *
     * Create an internal state callback, to be invoked on the mDeviceHandler
     *
     * <p>It has a few behaviors:
     * <ul>
     * <li>Convert device state changes into session state changes.
     * <li>Keep track of async tasks that the session began (idle, abort).
     * </ul>
     * </p>
     * */
@Override
public CameraDeviceImpl.StateCallbackKK getDeviceStateCallback() {
    final CameraCaptureSession session = this;
    return new CameraDeviceImpl.StateCallbackKK() {

        private boolean mBusy = false;

        private boolean mActive = false;

        @Override
        public void onOpened(CameraDevice camera) {
            throw new AssertionError("Camera must already be open before creating a session");
        }

        @Override
        public void onDisconnected(CameraDevice camera) {
            if (DEBUG)
                Log.v(TAG, mIdString + "onDisconnected");
            close();
        }

        @Override
        public void onError(CameraDevice camera, int error) {
            // Should not be reached, handled by device code
            Log.wtf(TAG, mIdString + "Got device error " + error);
        }

        @Override
        public void onActive(CameraDevice camera) {
            mIdleDrainer.taskStarted();
            mActive = true;
            if (DEBUG)
                Log.v(TAG, mIdString + "onActive");
            mStateCallback.onActive(session);
        }

        @Override
        public void onIdle(CameraDevice camera) {
            boolean isAborting;
            if (DEBUG)
                Log.v(TAG, mIdString + "onIdle");
            synchronized (session) {
                isAborting = mAborting;
            }
            /*
                 * Check which states we transitioned through:
                 *
                 * (ACTIVE -> IDLE)
                 * (BUSY -> IDLE)
                 *
                 * Note that this is also legal:
                 * (ACTIVE -> BUSY -> IDLE)
                 *
                 * and mark those tasks as finished
                 */
            if (mBusy && isAborting) {
                mAbortDrainer.taskFinished();
                synchronized (session) {
                    mAborting = false;
                }
            }
            if (mActive) {
                mIdleDrainer.taskFinished();
            }
            mBusy = false;
            mActive = false;
            mStateCallback.onReady(session);
        }

        @Override
        public void onBusy(CameraDevice camera) {
            mBusy = true;
            // Don't signal the application since there's no clean mapping here
            if (DEBUG)
                Log.v(TAG, mIdString + "onBusy");
        }

        @Override
        public void onUnconfigured(CameraDevice camera) {
            if (DEBUG)
                Log.v(TAG, mIdString + "onUnconfigured");
        }

        @Override
        public void onSurfacePrepared(Surface surface) {
            if (DEBUG)
                Log.v(TAG, mIdString + "onPrepared");
            mStateCallback.onSurfacePrepared(session, surface);
        }
    };
}
Also used : CameraDevice(android.hardware.camera2.CameraDevice) CameraCaptureSession(android.hardware.camera2.CameraCaptureSession) Surface(android.view.Surface)

Aggregations

CameraCaptureSession (android.hardware.camera2.CameraCaptureSession)38 CameraAccessException (android.hardware.camera2.CameraAccessException)18 Surface (android.view.Surface)18 BlockingSessionCallback (com.android.ex.camera2.blocking.BlockingSessionCallback)10 ArrayList (java.util.ArrayList)10 CaptureRequest (android.hardware.camera2.CaptureRequest)9 TotalCaptureResult (android.hardware.camera2.TotalCaptureResult)8 SurfaceTexture (android.graphics.SurfaceTexture)6 CameraDevice (android.hardware.camera2.CameraDevice)6 Activity (android.app.Activity)5 Point (android.graphics.Point)5 CameraCharacteristics (android.hardware.camera2.CameraCharacteristics)4 CaptureFailure (android.hardware.camera2.CaptureFailure)4 LinkedList (java.util.LinkedList)4 List (java.util.List)4 CameraManager (android.hardware.camera2.CameraManager)3 Image (android.media.Image)3 NonNull (android.support.annotation.NonNull)3 Size (android.util.Size)3 IOException (java.io.IOException)3