我一直在努力在Android上制作自定义相机活动,但是当旋转相机时,表面视图的纵横比会变得混乱.
在我的oncreate活动中,我设置了framelayout,它保存了显示摄像机参数的表面视图.
//FrameLayout that will hold the camera preview
FrameLayout previewHolder = (FrameLayout) findViewById(R.id.camerapreview);
//Setting camera's preview size to the best preview size
Size optimalSize = null;
camera = getCameraInstance();
double aspectRatio = 0;
if(camera != null){
//Setting the camera's aspect ratio
Camera.Parameters parameters = camera.getParameters();
List<Size> sizes = parameters.getSupportedPreviewSizes();
optimalSize = CameraPreview.getOptimalPreviewSize(sizes, getResources().getDisplayMetrics().widthPixels, getResources().getDisplayMetrics().heightPixels);
aspectRatio = (float)optimalSize.width/optimalSize.height;
}
if(optimalSize!= null){
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, (int)(getResources().getDisplayMetrics().widthPixels*aspectRatio));
previewHolder.setLayoutParams(params);
LayoutParams surfaceParams = new LayoutParams(LayoutParams.MATCH_PARENT, (int)(getResources().getDisplayMetrics().widthPixels*aspectRatio));
cameraPreview.setLayoutParams(surfaceParams);
}
cameraPreview.setCamera(camera);
//Adding the preview …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用camera2接口在Android 5.0上获得数字缩放帧.适用于该功能的doc是developer.android.com/camera2/captureRequest
我的应用中使用的表面:
相机的传感器尺寸为3280x2464(4:3宽高比)
我想从传感器获得的作物区域是:
Rect zoomCropPreview = new Rect(1094, 822, 2186, 1642); //(1092x820, 4:3 aspect ratio)
Run Code Online (Sandbox Code Playgroud)
我将此Rect设置为预览请求的参数:
previewRequestBuilder.set(CaptureRequest.SCALER_CROP_REGION, zoomCropPreview);
captureSession.setRepeatingRequest(previewRequestBuilder.build(), null, null);
Run Code Online (Sandbox Code Playgroud)
并采取静止图像:
stillRequestBuilder.set(CaptureRequest.SCALER_CROP_REGION, zoomCropPreview);
captureSession.capture(stillRequestBuilder.build(), new captureListener() , null);
Run Code Online (Sandbox Code Playgroud)
预期成绩:
真实结果:

我究竟做错了什么?