Search in sources :

Example 26 with CaptureCallback

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

the class CameraDeviceImpl method submitCaptureRequest.

private int submitCaptureRequest(List<CaptureRequest> requestList, CaptureCallback callback, Handler handler, boolean repeating) throws CameraAccessException {
    // Need a valid handler, or current thread needs to have a looper, if
    // callback is valid
    handler = checkHandler(handler, callback);
    // Make sure that there all requests have at least 1 surface; all surfaces are non-null
    for (CaptureRequest request : requestList) {
        if (request.getTargets().isEmpty()) {
            throw new IllegalArgumentException("Each request must have at least one Surface target");
        }
        for (Surface surface : request.getTargets()) {
            if (surface == null) {
                throw new IllegalArgumentException("Null Surface targets are not allowed");
            }
        }
    }
    synchronized (mInterfaceLock) {
        checkIfCameraClosedOrInError();
        if (repeating) {
            stopRepeating();
        }
        SubmitInfo requestInfo;
        CaptureRequest[] requestArray = requestList.toArray(new CaptureRequest[requestList.size()]);
        requestInfo = mRemoteDevice.submitRequestList(requestArray, repeating);
        if (DEBUG) {
            Log.v(TAG, "last frame number " + requestInfo.getLastFrameNumber());
        }
        if (callback != null) {
            mCaptureCallbackMap.put(requestInfo.getRequestId(), new CaptureCallbackHolder(callback, requestList, handler, repeating, mNextSessionId - 1));
        } else {
            if (DEBUG) {
                Log.d(TAG, "Listen for request " + requestInfo.getRequestId() + " is null");
            }
        }
        if (repeating) {
            if (mRepeatingRequestId != REQUEST_ID_NONE) {
                checkEarlyTriggerSequenceComplete(mRepeatingRequestId, requestInfo.getLastFrameNumber());
            }
            mRepeatingRequestId = requestInfo.getRequestId();
        } else {
            mRequestLastFrameNumbersList.add(new RequestLastFrameNumbersHolder(requestList, requestInfo));
        }
        if (mIdle) {
            mDeviceHandler.post(mCallOnActive);
        }
        mIdle = false;
        return requestInfo.getRequestId();
    }
}
Also used : SubmitInfo(android.hardware.camera2.utils.SubmitInfo) CaptureRequest(android.hardware.camera2.CaptureRequest) Surface(android.view.Surface)

Example 27 with CaptureCallback

use of android.hardware.camera2.CameraCaptureSession.CaptureCallback 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 28 with CaptureCallback

use of android.hardware.camera2.CameraCaptureSession.CaptureCallback 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 29 with CaptureCallback

use of android.hardware.camera2.CameraCaptureSession.CaptureCallback 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 30 with CaptureCallback

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

Aggregations

CaptureRequest (android.hardware.camera2.CaptureRequest)53 TotalCaptureResult (android.hardware.camera2.TotalCaptureResult)28 ArrayList (java.util.ArrayList)26 SimpleCaptureCallback (com.android.mediaframeworktest.helpers.CameraTestUtils.SimpleCaptureCallback)25 CaptureCallback (android.hardware.camera2.CameraCaptureSession.CaptureCallback)11 Image (android.media.Image)10 CameraTestUtils.getDataFromImage (com.android.mediaframeworktest.helpers.CameraTestUtils.getDataFromImage)10 CaptureFailure (android.hardware.camera2.CaptureFailure)6 Surface (android.view.Surface)6 SubmitInfo (android.hardware.camera2.utils.SubmitInfo)5 Size (android.util.Size)5 CameraCaptureSession (android.hardware.camera2.CameraCaptureSession)4 CameraAccessException (android.hardware.camera2.CameraAccessException)3 Activity (android.app.Activity)2 Point (android.graphics.Point)2 SuppressLint (android.annotation.SuppressLint)1 SurfaceTexture (android.graphics.SurfaceTexture)1 CameraCharacteristics (android.hardware.camera2.CameraCharacteristics)1 CameraManager (android.hardware.camera2.CameraManager)1 NonNull (android.support.annotation.NonNull)1