Search in sources :

Example 1 with CameraSelector

use of androidx.camera.core.CameraSelector in project Signal-Android by WhisperSystems.

the class SignalCameraXModule method bindToLifecycleAfterViewMeasured.

@RequiresPermission(permission.CAMERA)
void bindToLifecycleAfterViewMeasured() {
    if (mNewLifecycle == null) {
        return;
    }
    clearCurrentLifecycle();
    if (mNewLifecycle.getLifecycle().getCurrentState() == Lifecycle.State.DESTROYED) {
        // Lifecycle is already in a destroyed state. Since it may have been a valid
        // lifecycle when bound, but became destroyed while waiting for layout, treat this as
        // a no-op now that we have cleared the previous lifecycle.
        mNewLifecycle = null;
        return;
    }
    mCurrentLifecycle = mNewLifecycle;
    mNewLifecycle = null;
    if (mCameraProvider == null) {
        // try again once the camera provider is no longer null
        return;
    }
    Set<Integer> available = getAvailableCameraLensFacing();
    if (available.isEmpty()) {
        Logger.w(TAG, "Unable to bindToLifeCycle since no cameras available");
        mCameraLensFacing = null;
    }
    // Ensure the current camera exists, or default to another camera
    if (mCameraLensFacing != null && !available.contains(mCameraLensFacing)) {
        Logger.w(TAG, "Camera does not exist with direction " + mCameraLensFacing);
        // Default to the first available camera direction
        mCameraLensFacing = available.iterator().next();
        Logger.w(TAG, "Defaulting to primary camera with direction " + mCameraLensFacing);
    }
    // were no available cameras, which should be logged in the logic above.
    if (mCameraLensFacing == null) {
        return;
    }
    // Set the preferred aspect ratio as 4:3 if it is IMAGE only mode. Set the preferred aspect
    // ratio as 16:9 if it is VIDEO or MIXED mode. Then, it will be WYSIWYG when the view finder
    // is in CENTER_INSIDE mode.
    boolean isDisplayPortrait = getDisplayRotationDegrees() == 0 || getDisplayRotationDegrees() == 180;
    // Begin Signal Custom Code Block
    int resolution = CameraXUtil.getIdealResolution(Resources.getSystem().getDisplayMetrics().widthPixels, Resources.getSystem().getDisplayMetrics().heightPixels);
    // End Signal Custom Code Block
    Rational targetAspectRatio;
    // Begin Signal Custom Code Block
    mImageCaptureBuilder.setTargetResolution(CameraXUtil.buildResolutionForRatio(resolution, ASPECT_RATIO_16_9, isDisplayPortrait));
    targetAspectRatio = isDisplayPortrait ? ASPECT_RATIO_9_16 : ASPECT_RATIO_16_9;
    // End Signal Custom Code Block
    // Begin Signal Custom Code Block
    mImageCaptureBuilder.setCaptureMode(CameraXUtil.getOptimalCaptureMode());
    // End Signal Custom Code Block
    mImageCaptureBuilder.setTargetRotation(getDisplaySurfaceRotation());
    mImageCapture = mImageCaptureBuilder.build();
    // Begin Signal Custom Code Block
    Size size = VideoUtil.getVideoRecordingSize();
    mVideoCaptureBuilder.setTargetResolution(size);
    mVideoCaptureBuilder.setMaxResolution(size);
    // End Signal Custom Code Block
    mVideoCaptureBuilder.setTargetRotation(getDisplaySurfaceRotation());
    // Begin Signal Custom Code Block
    if (MediaConstraints.isVideoTranscodeAvailable()) {
        mVideoCapture = mVideoCaptureBuilder.build();
    }
    // End Signal Custom Code Block
    // Adjusts the preview resolution according to the view size and the target aspect ratio.
    int height = (int) (getMeasuredWidth() / targetAspectRatio.floatValue());
    mPreviewBuilder.setTargetResolution(new Size(getMeasuredWidth(), height));
    mPreview = mPreviewBuilder.build();
    mPreview.setSurfaceProvider(mCameraView.getPreviewView().getSurfaceProvider());
    CameraSelector cameraSelector = new CameraSelector.Builder().requireLensFacing(mCameraLensFacing).build();
    if (getCaptureMode() == SignalCameraView.CaptureMode.IMAGE) {
        mCamera = mCameraProvider.bindToLifecycle(mCurrentLifecycle, cameraSelector, mImageCapture, mPreview);
    } else if (getCaptureMode() == SignalCameraView.CaptureMode.VIDEO) {
        mCamera = mCameraProvider.bindToLifecycle(mCurrentLifecycle, cameraSelector, mVideoCapture, mPreview);
    } else {
        mCamera = mCameraProvider.bindToLifecycle(mCurrentLifecycle, cameraSelector, mImageCapture, mVideoCapture, mPreview);
    }
    setZoomRatio(UNITY_ZOOM_SCALE);
    mCurrentLifecycle.getLifecycle().addObserver(mCurrentLifecycleObserver);
    // Enable flash setting in ImageCapture after use cases are created and binded.
    setFlash(getFlash());
}
Also used : Rational(android.util.Rational) CameraSelector(androidx.camera.core.CameraSelector) Size(android.util.Size) SuppressLint(android.annotation.SuppressLint) RequiresPermission(androidx.annotation.RequiresPermission)

Aggregations

SuppressLint (android.annotation.SuppressLint)1 Rational (android.util.Rational)1 Size (android.util.Size)1 RequiresPermission (androidx.annotation.RequiresPermission)1 CameraSelector (androidx.camera.core.CameraSelector)1