Camera2 标量裁剪区域和传感器图像大小

Camera2 Scalar Crop Region and Sensor Image Size

我正在为 Galaxy S7 编写一个应用程序,它显示实时 Camera2 预览,我想在其中使用以下方法控制缩放级别:

 captureRequestBuilder.set(CaptureRequest.SCALER_CROP_REGION, zoomCropPreview);
 cameraCaptureSessions.setRepeatingRequest(captureRequestBuilder.build(), null, mBackgroundHandler);

Rect zoomCropPreview 已针对所需的不同 crops/zooms 进行了更改。由于 S7 支持最大 8 倍数码变焦并且其传感器阵列大小为 4032x3024,因此我很难确定 Rect 值。我查看了 https://source.android.com/devices/camera/camera3_crop_reprocess.html 以寻求指导,但我被卡住了。

例如,zoomCropPreview = (0,0,4032, 3024) 和 (0,0,504,378)- 8 倍缩放一样有效。但是其他区域,如 (250,250,504,378) 或 (1512, 1134, 1008, 756) 不起作用,即使它们的边界在传感器阵列内。这是基于 https://inducesmile.com/android/android-camera2-api-example-tutorial/

的代码的相机预览部分
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.
                //try to change zoom
                captureRequestBuilder.set(CaptureRequest.SCALER_CROP_REGION, zoomCropPreview);
                cameraCaptureSessions = cameraCaptureSession;
                updatePreview();                    //update preview screen
            }

            @Override
            public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
                Toast.makeText(AndroidCameraApi.this, "Configuration change", Toast.LENGTH_SHORT).show();
            }
        }, null);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

如何确定 scalar_crop_region 的正确 Rect 值?

如果您查看 SCALER_CROP_REGION 的文档,它会告诉您

The width and height of the crop region cannot be set to be smaller than floor( activeArraySize.width / android.scaler.availableMaxDigitalZoom ) and floor( activeArraySize.height / android.scaler.availableMaxDigitalZoom ), respectively.

该文档还指出它将根据硬件等进行舍入 - 您想要计算裁剪区域的高度和宽度。从这些项目中,您可以计算左上角和右下角。这些角将提供裁剪区域的面积。

float cropW = activeArraySize.width() / zoomLevel;
float cropH = activeArraySize.height() / zoomLevel;

// now we calculate the corners
int top = activeArraySize.centerY() - (int) (cropH / 2f);
int left = activeArraySize.centerX() - (int) (cropW / 2f);
int right = activeArraySize.centerX() + (int) (cropW / 2f);
int bottom = activeArraySize.centerY() + (int) (cropH / 2f);

记住屏幕的左上角是 0,0。把这个画出来进一步说明