之前由@Romanski提供的解决方案可以正常工作,但它会随着裁剪而缩放。如果您需要按比例缩放,则使用以下解决方案。每当表面视图更改时调用updateTextureMatrix:即在onSurfaceTextureAvailable和onSurfaceTextureSizeChanged方法中调用。还请注意,此解决方案依赖于活动忽略配置更改(即android:configChanges="orientation|screenSize|keyboardHidden"或类似内容):
private void updateTextureMatrix(int width, int height)
{
boolean isPortrait = false;
Display display = getWindowManager().getDefaultDisplay();
if (display.getRotation() == Surface.ROTATION_0 || display.getRotation() == Surface.ROTATION_180) isPortrait = true;
else if (display.getRotation() == Surface.ROTATION_90 || display.getRotation() == Surface.ROTATION_270) isPortrait = false;
int previewWidth = orgPreviewWidth;
int previewHeight = orgPreviewHeight;
if (isPortrait)
{
previewWidth = orgPreviewHeight;
previewHeight = orgPreviewWidth;
}
float ratioSurface = (float) width / height;
float ratioPreview = (float) previewWidth / previewHeight;
float scaleX;
float scaleY;
if (ratioSurface > ratioPreview)
{
scaleX = (float) height / previewHeight;
scaleY = 1;
}
else
{
scaleX = 1;
scaleY = (float) width / previewWidth;
}
Matrix matrix = new Matrix();
matrix.setScale(scaleX, scaleY);
textureView.setTransform(matrix);
float scaledWidth = width * scaleX;
float scaledHeight = height * scaleY;
float dx = (width - scaledWidth) / 2;
float dy = (height - scaledHeight) / 2;
textureView.setTranslationX(dx);
textureView.setTranslationY(dy);
}
您还需要以下字段:
private int orgPreviewWidth;
private int orgPreviewHeight;
在调用updateTextureMatrix方法之前,请在onSurfaceTextureAvailable方法中进行初始化:
Camera.Parameters parameters = camera.getParameters()
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)
Pair<Integer, Integer> size = getMaxSize(parameters.getSupportedPreviewSizes())
parameters.setPreviewSize(size.first, size.second)
orgPreviewWidth = size.first
orgPreviewHeight = size.second
camera.setParameters(parameters)
getMaxSize方法:
private static Pair<Integer, Integer> getMaxSize(List<Camera.Size> list)
}
return new Pair<Integer, Integer>(width, height);
}
最后一件事 - 您需要纠正相机旋转。因此在Activity的onConfigurationChanged方法中调用setCameraDisplayOrientation方法(并且还要在onSurfaceTextureAvailable方法中进行初始调用):
而最后一件事-你需要校准照相机的旋转。所以在Activity的onConfigurationChanged方法中调用setCameraDisplayOrientation方法(同时也要在onSurfaceTextureAvailable方法中进行初始调用):
public static void setCameraDisplayOrientation(Activity activity, int cameraId, Camera camera)
{
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(cameraId, info);
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
int degrees = 0;
switch (rotation)
{
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT)
{
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360;
}
else
{
result = (info.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(result);
Camera.Parameters params = camera.getParameters();
params.setRotation(result);
camera.setParameters(params);
}