Search in sources :

Example 6 with CameraCaptureSession

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

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

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

the class Camera2 method takePicture.

protected void takePicture() throws IOException {
    if (null == cameraDevice) {
        Log.e(TAG, "cameraDevice is null");
        return;
    }
    CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    try {
        CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraDevice.getId());
        Size[] jpegSizes = null;
        if (characteristics != null) {
            jpegSizes = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP).getOutputSizes(ImageFormat.JPEG);
        }
        int width = 480;
        int height = 480;
        if (jpegSizes != null && 0 < jpegSizes.length) {
            width = jpegSizes[0].getWidth();
            height = jpegSizes[0].getHeight();
        }
        ImageReader reader = ImageReader.newInstance(width, height, ImageFormat.JPEG, 1);
        List<Surface> outputSurfaces = new ArrayList<Surface>(2);
        outputSurfaces.add(reader.getSurface());
        outputSurfaces.add(new Surface(textureView.getSurfaceTexture()));
        final CaptureRequest.Builder captureBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
        captureBuilder.addTarget(reader.getSurface());
        captureBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
        // Orientation
        int rotation = getWindowManager().getDefaultDisplay().getRotation();
        captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, ORIENTATIONS.get(rotation));
        // final File file = new File(Environment.getExternalStorageDirectory()+"/MazeSolver/nude.jpg");
        final File file = createImageFileName();
        lastFile = file;
        ImageReader.OnImageAvailableListener readerListener = new ImageReader.OnImageAvailableListener() {

            @Override
            public void onImageAvailable(ImageReader reader) {
                Image image = null;
                try {
                    image = reader.acquireLatestImage();
                    ByteBuffer buffer = image.getPlanes()[0].getBuffer();
                    byte[] bytes = new byte[buffer.capacity()];
                    buffer.get(bytes);
                    save(bytes);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (image != null) {
                        image.close();
                    }
                }
            }

            private void save(byte[] bytes) throws IOException {
                OutputStream output = null;
                try {
                    output = new FileOutputStream(file);
                    output.write(bytes);
                } finally {
                    if (null != output) {
                        output.close();
                    }
                }
            }
        };
        reader.setOnImageAvailableListener(readerListener, mBackgroundHandler);
        final CameraCaptureSession.CaptureCallback captureListener = new CameraCaptureSession.CaptureCallback() {

            @Override
            public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
                super.onCaptureCompleted(session, request, result);
                Toast.makeText(Camera2.this, "Saved:" + file, Toast.LENGTH_SHORT).show();
                createCameraPreview();
            }
        };
        cameraDevice.createCaptureSession(outputSurfaces, new CameraCaptureSession.StateCallback() {

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

            @Override
            public void onConfigureFailed(CameraCaptureSession session) {
            }
        }, mBackgroundHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}
Also used : Size(android.util.Size) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) Image(android.media.Image) CameraCaptureSession(android.hardware.camera2.CameraCaptureSession) Surface(android.view.Surface) CameraAccessException(android.hardware.camera2.CameraAccessException) ImageReader(android.media.ImageReader) CameraManager(android.hardware.camera2.CameraManager) TotalCaptureResult(android.hardware.camera2.TotalCaptureResult) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) FileOutputStream(java.io.FileOutputStream) CameraCharacteristics(android.hardware.camera2.CameraCharacteristics) CaptureRequest(android.hardware.camera2.CaptureRequest) File(java.io.File)

Example 8 with CameraCaptureSession

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

the class CameraImplV2 method startPreview.

@Override
public void startPreview(SurfaceTexture surface, IPreviewListener previewListener) {
    if (mPreviewRunning) {
        previewListener.started();
        return;
    }
    if (mCamera != null) {
        Log.v(TAG, "trying to start preview...");
        try {
            CameraCharacteristics characteristics = mCamManager.getCameraCharacteristics(mCamera.getId());
            StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
            if (map == null) {
                Log.w(TAG, "Could not find a valid preview size");
            }
            final Point previewSize = chooseOptimalSize(toPointArray(map.getOutputSizes(ImageFormat.YUV_420_888)));
            Log.v(TAG, "preview image size is " + previewSize.x + "x" + previewSize.y);
            mActivity.runOnUiThread(() -> configureTransform(previewSize, mActivity.getWindowManager().getDefaultDisplay().getRotation()));
            mImageReader = ImageReader.newInstance(previewSize.x, previewSize.y, ImageFormat.YUV_420_888, 2);
            mImageReader.setOnImageAvailableListener(reader -> {
                if (!mPreviewRunning) {
                    mPreviewRunning = true;
                    previewListener.started();
                }
                Image i = reader.acquireLatestImage();
                try {
                    LumaData ld = null;
                    for (ILumaListener l : mListeners) {
                        if (l.needsPreview()) {
                            if (ld == null && i != null) {
                                Log.v(TAG, "preview image available and needed: size " + i.getWidth() + "x" + i.getHeight());
                                ByteBuffer luma = i.getPlanes()[0].getBuffer();
                                final byte[] data = new byte[luma.capacity()];
                                luma.get(data);
                                ld = new LumaData(data, i.getWidth(), i.getHeight());
                            }
                            l.preview(ld);
                        }
                    }
                } finally {
                    if (i != null) {
                        i.close();
                    }
                }
            }, mPreviewHandler);
            CaptureRequest.Builder mPreviewRequestBuilder = mCamera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
            mPreviewRequestBuilder.addTarget(mImageReader.getSurface());
            Surface previewSurface = new Surface(surface);
            mPreviewRequestBuilder.addTarget(previewSurface);
            ArrayList<Surface> surfaces = new ArrayList<>();
            surfaces.add(mImageReader.getSurface());
            surfaces.add(previewSurface);
            mCamera.createCaptureSession(surfaces, new CameraCaptureSession.StateCallback() {

                @Override
                public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
                    Log.d(TAG, "Capture session configured");
                    // The mCamera is already closed
                    if (null == mCamera) {
                        previewListener.error("Capture session has no camera");
                        return;
                    }
                    mCaptureSession = cameraCaptureSession;
                    try {
                        // Auto focus should be continuous for mCamera preview.
                        mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
                        // Finally, we start displaying the mCamera preview.
                        cameraCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(), null, null);
                    } catch (CameraAccessException e) {
                        previewListener.exception(e);
                        previewListener.error("Could not create preview request");
                    }
                }

                @Override
                public void onClosed(@NonNull CameraCaptureSession session) {
                    super.onClosed(session);
                    mPreviewRunning = false;
                }

                @Override
                public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
                    previewListener.error("Could not create capture session");
                }
            }, mPreviewHandler);
        } catch (CameraAccessException e) {
            previewListener.exception(e);
            previewListener.error("Could not create preview");
        }
    } else {
        previewListener.error("Camera not found");
    }
}
Also used : ArrayList(java.util.ArrayList) StreamConfigurationMap(android.hardware.camera2.params.StreamConfigurationMap) Point(android.graphics.Point) Image(android.media.Image) ByteBuffer(java.nio.ByteBuffer) CameraCaptureSession(android.hardware.camera2.CameraCaptureSession) Surface(android.view.Surface) CameraAccessException(android.hardware.camera2.CameraAccessException) CameraCharacteristics(android.hardware.camera2.CameraCharacteristics) CaptureRequest(android.hardware.camera2.CaptureRequest)

Example 9 with CameraCaptureSession

use of android.hardware.camera2.CameraCaptureSession in project android_packages_apps_Snap by LineageOS.

the class ClearSightImageProcessor method capture.

public void capture(boolean bayer, CameraCaptureSession session, CaptureRequest.Builder requestBuilder, Handler captureCallbackHandler) throws CameraAccessException {
    Log.d(TAG, "capture: " + bayer);
    final int cam = bayer ? CAM_TYPE_BAYER : CAM_TYPE_MONO;
    CameraCaptureSession.CaptureCallback captureCallback = new CameraCaptureSession.CaptureCallback() {

        @Override
        public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
            Log.d(TAG, "capture - onCaptureCompleted: " + cam);
            if (isClosing())
                Log.d(TAG, "capture - onCaptureCompleted - closing");
            else {
                mImageProcessHandler.obtainMessage(MSG_NEW_CAPTURE_RESULT, cam, 0, result).sendToTarget();
                if (cam == CAM_TYPE_BAYER) {
                    float lensFocusDistance = result.get(CaptureResult.LENS_FOCUS_DISTANCE);
                    Log.d(TAG, "lensFocusDistance=" + lensFocusDistance);
                    mDepthProcessHandler.obtainMessage(MSG_NEW_LENS_FOCUS_DISTANCE_BAYER, 0, 0, lensFocusDistance).sendToTarget();
                }
            }
        }

        @Override
        public void onCaptureFailed(CameraCaptureSession session, CaptureRequest request, CaptureFailure result) {
            Log.d(TAG, "capture - onCaptureFailed: " + cam);
            if (isClosing())
                Log.d(TAG, "capture - onCaptureFailed - closing");
            else
                mImageProcessHandler.obtainMessage(MSG_NEW_CAPTURE_FAIL, cam, 0, result).sendToTarget();
        }

        @Override
        public void onCaptureSequenceCompleted(CameraCaptureSession session, int sequenceId, long frameNumber) {
            Log.d(TAG, "capture - onCaptureSequenceCompleted: " + cam);
        }
    };
    List<CaptureRequest> burstList = new ArrayList<CaptureRequest>();
    requestBuilder.addTarget(mImageReader[cam].getSurface());
    for (int i = 0; i < mNumBurstCount; i++) {
        requestBuilder.setTag(new Object());
        CaptureRequest request = requestBuilder.build();
        burstList.add(request);
    }
    mImageProcessHandler.obtainMessage(MSG_START_CAPTURE, cam, burstList.size(), 0).sendToTarget();
    session.captureBurst(burstList, captureCallback, captureCallbackHandler);
}
Also used : CaptureCallback(android.hardware.camera2.CameraCaptureSession.CaptureCallback) CaptureFailure(android.hardware.camera2.CaptureFailure) ArrayList(java.util.ArrayList) TotalCaptureResult(android.hardware.camera2.TotalCaptureResult) CaptureCallback(android.hardware.camera2.CameraCaptureSession.CaptureCallback) CaptureRequest(android.hardware.camera2.CaptureRequest) CameraCaptureSession(android.hardware.camera2.CameraCaptureSession)

Example 10 with CameraCaptureSession

use of android.hardware.camera2.CameraCaptureSession in project android_packages_apps_Snap by LineageOS.

the class ClearSightImageProcessor method createCaptureSession.

public void createCaptureSession(boolean bayer, CameraDevice device, List<Surface> surfaces, CameraCaptureSession.StateCallback captureSessionCallback) throws CameraAccessException {
    Log.d(TAG, "createCaptureSession: " + bayer);
    int cam = bayer ? CAM_TYPE_BAYER : CAM_TYPE_MONO;
    surfaces.add(mImageReader[cam].getSurface());
    surfaces.add(mEncodeImageReader[cam].getSurface());
    // Here, we create a CameraCaptureSession for camera preview.
    device.createReprocessableCaptureSession(new InputConfiguration(mImageReader[cam].getWidth(), mImageReader[cam].getHeight(), mImageReader[cam].getImageFormat()), surfaces, captureSessionCallback, null);
}
Also used : InputConfiguration(android.hardware.camera2.params.InputConfiguration)

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