Search in sources :

Example 36 with CameraCaptureSession

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

the class ShadowCameraDeviceImpl method createCaptureSession.

@Implementation(minSdk = VERSION_CODES.P)
protected void createCaptureSession(SessionConfiguration config) throws CameraAccessException {
    checkIfCameraClosedOrInError();
    CameraCaptureSession session = createCameraCaptureSession(config.getStateCallback());
    config.getExecutor().execute(() -> config.getStateCallback().onConfigured(session));
}
Also used : CameraCaptureSession(android.hardware.camera2.CameraCaptureSession) Implementation(org.robolectric.annotation.Implementation)

Example 37 with CameraCaptureSession

use of android.hardware.camera2.CameraCaptureSession 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)

Example 38 with CameraCaptureSession

use of android.hardware.camera2.CameraCaptureSession in project habpanelviewer by vbier.

the class CameraImplV2 method takePicture.

@Override
public void takePicture(IPictureListener iPictureHandler) {
    if (mCamera != null) {
        if (mPictureThread == null) {
            mPictureThread = new HandlerThread("takePictureThread");
            mPictureThread.start();
            mPictureHandler = new Handler(mPictureThread.getLooper());
        }
        try {
            int width = 640;
            int height = 480;
            ImageReader reader = ImageReader.newInstance(width, height, ImageFormat.JPEG, 1);
            List<Surface> outputSurfaces = new ArrayList<>(2);
            outputSurfaces.add(reader.getSurface());
            final CaptureRequest.Builder captureBuilder = mCamera.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
            captureBuilder.addTarget(reader.getSurface());
            captureBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
            ImageReader.OnImageAvailableListener readerListener = reader1 -> {
                Image image = null;
                try {
                    image = reader1.acquireLatestImage();
                    ByteBuffer buffer = image.getPlanes()[0].getBuffer();
                    byte[] bytes = new byte[buffer.capacity()];
                    buffer.get(bytes);
                    iPictureHandler.picture(bytes);
                } finally {
                    if (image != null) {
                        image.close();
                    }
                }
            };
            reader.setOnImageAvailableListener(readerListener, mPictureHandler);
            final CameraCaptureSession.CaptureCallback captureListener = new CameraCaptureSession.CaptureCallback() {

                @Override
                public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
                    super.onCaptureCompleted(session, request, result);
                }
            };
            mCamera.createCaptureSession(outputSurfaces, new CameraCaptureSession.StateCallback() {

                @Override
                public void onConfigured(@NonNull CameraCaptureSession session) {
                    try {
                        session.capture(captureBuilder.build(), captureListener, mPictureHandler);
                    } catch (CameraAccessException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onConfigureFailed(@NonNull CameraCaptureSession session) {
                }
            }, mPictureHandler);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    } else {
        throw new IllegalStateException("Motion detection not running");
    }
}
Also used : Context(android.content.Context) PackageManager(android.content.pm.PackageManager) RequiresApi(android.support.annotation.RequiresApi) NonNull(android.support.annotation.NonNull) CameraManager(android.hardware.camera2.CameraManager) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) Manifest(android.Manifest) Image(android.media.Image) CameraCaptureSession(android.hardware.camera2.CameraCaptureSession) CameraCharacteristics(android.hardware.camera2.CameraCharacteristics) Handler(android.os.Handler) Matrix(android.graphics.Matrix) Build(android.os.Build) Log(android.util.Log) RectF(android.graphics.RectF) SurfaceTexture(android.graphics.SurfaceTexture) ImageFormat(android.graphics.ImageFormat) Surface(android.view.Surface) StreamConfigurationMap(android.hardware.camera2.params.StreamConfigurationMap) CameraMetadata(android.hardware.camera2.CameraMetadata) Point(android.graphics.Point) ImageReader(android.media.ImageReader) TotalCaptureResult(android.hardware.camera2.TotalCaptureResult) TextureView(android.view.TextureView) ActivityCompat(android.support.v4.app.ActivityCompat) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) R(de.vier_bier.habpanelviewer.R) HandlerThread(android.os.HandlerThread) CameraAccessException(android.hardware.camera2.CameraAccessException) CameraDevice(android.hardware.camera2.CameraDevice) CaptureRequest(android.hardware.camera2.CaptureRequest) Activity(android.app.Activity) Size(android.util.Size) ArrayList(java.util.ArrayList) Handler(android.os.Handler) TotalCaptureResult(android.hardware.camera2.TotalCaptureResult) Image(android.media.Image) ByteBuffer(java.nio.ByteBuffer) CameraCaptureSession(android.hardware.camera2.CameraCaptureSession) Point(android.graphics.Point) Surface(android.view.Surface) CameraAccessException(android.hardware.camera2.CameraAccessException) HandlerThread(android.os.HandlerThread) NonNull(android.support.annotation.NonNull) CaptureRequest(android.hardware.camera2.CaptureRequest) ImageReader(android.media.ImageReader)

Example 39 with CameraCaptureSession

use of android.hardware.camera2.CameraCaptureSession in project Relic_Main by TeamOverdrive.

the class Camera2Renderer method createCameraPreviewSession.

private void createCameraPreviewSession() {
    int w = mPreviewSize.getWidth(), h = mPreviewSize.getHeight();
    Log.i(LOGTAG, "createCameraPreviewSession(" + w + "x" + h + ")");
    if (w < 0 || h < 0)
        return;
    try {
        mCameraOpenCloseLock.acquire();
        if (null == mCameraDevice) {
            mCameraOpenCloseLock.release();
            Log.e(LOGTAG, "createCameraPreviewSession: camera isn't opened");
            return;
        }
        if (null != mCaptureSession) {
            mCameraOpenCloseLock.release();
            Log.e(LOGTAG, "createCameraPreviewSession: mCaptureSession is already started");
            return;
        }
        if (null == mSTexture) {
            mCameraOpenCloseLock.release();
            Log.e(LOGTAG, "createCameraPreviewSession: preview SurfaceTexture is null");
            return;
        }
        mSTexture.setDefaultBufferSize(w, h);
        Surface surface = new Surface(mSTexture);
        mPreviewRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        mPreviewRequestBuilder.addTarget(surface);
        mCameraDevice.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback() {

            @Override
            public void onConfigured(CameraCaptureSession cameraCaptureSession) {
                mCaptureSession = cameraCaptureSession;
                try {
                    mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
                    mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
                    mCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(), null, mBackgroundHandler);
                    Log.i(LOGTAG, "CameraPreviewSession has been started");
                } catch (CameraAccessException e) {
                    Log.e(LOGTAG, "createCaptureSession failed");
                }
                mCameraOpenCloseLock.release();
            }

            @Override
            public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) {
                Log.e(LOGTAG, "createCameraPreviewSession failed");
                mCameraOpenCloseLock.release();
            }
        }, mBackgroundHandler);
    } catch (CameraAccessException e) {
        Log.e(LOGTAG, "createCameraPreviewSession");
    } catch (InterruptedException e) {
        throw new RuntimeException("Interrupted while createCameraPreviewSession", e);
    } finally {
    // mCameraOpenCloseLock.release();
    }
}
Also used : CameraAccessException(android.hardware.camera2.CameraAccessException) 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