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

时间:2017-03-25 18:25:03

标签: android camera2

我正在为Galaxy S7编写应用程序,该应用程序显示实时Camera2预览,我希望使用以下方法控制缩放级别:

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

针对所需的不同裁剪/缩放更改Rect zoomCropPreview。由于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值?

1 个答案:

答案 0 :(得分:0)

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

  

裁剪区域的宽度和高度不能分别设置为小于floor(activeArraySize.width / android.scaler.availableMaxDigitalZoom)和floor(activeArraySize.height / android.scaler.availableMaxDigitalZoom)。

该文档还声明它将基于硬件等进行舍入 - 您想要计算裁剪区域的高度和宽度。然后,您可以从这些项目计算左上角和右下角。然后,这些角将提供裁剪区域的区域。

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。借此进一步澄清