Search in sources :

Example 1 with CameraCaptureSession

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

the class Camera2Fragment method startPreview.

private void startPreview() {
    if (null == mCameraDevice || !mTextureView.isAvailable() || null == mPreviewSize)
        return;
    try {
        if (!mInterface.useStillshot()) {
            if (!setUpMediaRecorder()) {
                return;
            }
        }
        SurfaceTexture texture = mTextureView.getSurfaceTexture();
        assert texture != null;
        texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
        List<Surface> surfaces = new ArrayList<>();
        Surface previewSurface = new Surface(texture);
        surfaces.add(previewSurface);
        if (mInterface.useStillshot()) {
            mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
            mPreviewBuilder.addTarget(previewSurface);
            surfaces.add(mImageReader.getSurface());
        } else {
            mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
            mPreviewBuilder.addTarget(previewSurface);
            Surface recorderSurface = mMediaRecorder.getSurface();
            surfaces.add(recorderSurface);
            mPreviewBuilder.addTarget(recorderSurface);
        }
        mCameraDevice.createCaptureSession(surfaces, new CameraCaptureSession.StateCallback() {

            @Override
            public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
                if (mCameraDevice == null) {
                    return;
                }
                mPreviewSession = cameraCaptureSession;
                updatePreview();
            }

            @Override
            public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
                throwError(new Exception("Camera configuration failed"));
            }
        }, mBackgroundHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}
Also used : CameraAccessException(android.hardware.camera2.CameraAccessException) SurfaceTexture(android.graphics.SurfaceTexture) ArrayList(java.util.ArrayList) CameraCaptureSession(android.hardware.camera2.CameraCaptureSession) IOException(java.io.IOException) CameraAccessException(android.hardware.camera2.CameraAccessException) Surface(android.view.Surface)

Example 2 with CameraCaptureSession

use of android.hardware.camera2.CameraCaptureSession in project android_frameworks_base by DirtyUnicorns.

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 3 with CameraCaptureSession

use of android.hardware.camera2.CameraCaptureSession in project android_frameworks_base by AOSPA.

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)

Example 4 with CameraCaptureSession

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

the class CameraTestUtils method configureReprocessableCameraSession.

public static CameraCaptureSession configureReprocessableCameraSession(CameraDevice camera, InputConfiguration inputConfiguration, List<Surface> outputSurfaces, CameraCaptureSession.StateCallback listener, Handler handler) throws CameraAccessException {
    BlockingSessionCallback sessionListener = new BlockingSessionCallback(listener);
    camera.createReprocessableCaptureSession(inputConfiguration, outputSurfaces, sessionListener, handler);
    Integer[] sessionStates = { BlockingSessionCallback.SESSION_READY, BlockingSessionCallback.SESSION_CONFIGURE_FAILED };
    int state = sessionListener.getStateWaiter().waitForAnyOfStates(Arrays.asList(sessionStates), SESSION_CONFIGURE_TIMEOUT_MS);
    assertTrue("Creating a reprocessable session failed.", state == BlockingSessionCallback.SESSION_READY);
    CameraCaptureSession session = sessionListener.waitAndGetSession(SESSION_CONFIGURE_TIMEOUT_MS);
    assertTrue("Camera session should be a reprocessable session", session.isReprocessable());
    return session;
}
Also used : BlockingSessionCallback(com.android.ex.camera2.blocking.BlockingSessionCallback) CameraCaptureSession(android.hardware.camera2.CameraCaptureSession)

Example 5 with CameraCaptureSession

use of android.hardware.camera2.CameraCaptureSession in project android_frameworks_base by crdroidandroid.

the class CameraTestUtils method configureReprocessableCameraSession.

public static CameraCaptureSession configureReprocessableCameraSession(CameraDevice camera, InputConfiguration inputConfiguration, List<Surface> outputSurfaces, CameraCaptureSession.StateCallback listener, Handler handler) throws CameraAccessException {
    BlockingSessionCallback sessionListener = new BlockingSessionCallback(listener);
    camera.createReprocessableCaptureSession(inputConfiguration, outputSurfaces, sessionListener, handler);
    Integer[] sessionStates = { BlockingSessionCallback.SESSION_READY, BlockingSessionCallback.SESSION_CONFIGURE_FAILED };
    int state = sessionListener.getStateWaiter().waitForAnyOfStates(Arrays.asList(sessionStates), SESSION_CONFIGURE_TIMEOUT_MS);
    assertTrue("Creating a reprocessable session failed.", state == BlockingSessionCallback.SESSION_READY);
    CameraCaptureSession session = sessionListener.waitAndGetSession(SESSION_CONFIGURE_TIMEOUT_MS);
    assertTrue("Camera session should be a reprocessable session", session.isReprocessable());
    return session;
}
Also used : BlockingSessionCallback(com.android.ex.camera2.blocking.BlockingSessionCallback) CameraCaptureSession(android.hardware.camera2.CameraCaptureSession)

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