我正在关注Google的camera2basic样本,以了解Camera2 API。我正在努力理解以下方法,特别是该方法的需求以及它在做什么,因为它没有正确记录。
/**
* Configures the necessary {@link android.graphics.Matrix} transformation to `mTextureView`.
* This method should be called after the camera preview size is determined in
* setUpCameraOutputs and also the size of `mTextureView` is fixed.
*
* @param viewWidth The width of `mTextureView`
* @param viewHeight The height of `mTextureView`
*/
private void configureTransform(int viewWidth, int viewHeight) {
Activity activity = getActivity();
if (null == mTextureView || null == mPreviewSize || null == activity) {
return;
}
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
Matrix matrix = new Matrix();
RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
RectF bufferRect = new RectF(0, 0, mPreviewSize.getHeight(), mPreviewSize.getWidth());
float centerX = viewRect.centerX();
float centerY = viewRect.centerY();
if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY());
matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL);
float scale = Math.max(
(float) viewHeight / mPreviewSize.getHeight(),
(float) viewWidth / mPreviewSize.getWidth());
matrix.postScale(scale, scale, centerX, centerY);
matrix.postRotate(90 * (rotation - 2), centerX, centerY);
} else if (Surface.ROTATION_180 == rotation) {
matrix.postRotate(180, centerX, centerY);
}
mTextureView.setTransform(matrix);
}
任何线索都将不胜感激。
这是回购的链接: - camera2basicfragment.java
我添加了0º和180º
@Override public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int width, int height) {
configCamara(width, height);
openCamera();
previewRotation(width, height);
}
private void previewRotation(int width, int height) {
int rotation = getWindowManager().getDefaultDisplay().getRotation();
Matrix matrix = new Matrix();
RectF textureRectF = new RectF(0, 0, width, height);
RectF previewRectF = new RectF(0, 0, previewSize.getHeight(), previewSize.getWidth());
float centerX = textureRectF.centerX();
float centery = textureRectF.centerY();
if (rotation == Surface.ROTATION_270 || rotation == Surface.ROTATION_90) {
previewRectF.offset(centerX - previewRectF.centerX(), centery - previewRectF.centerY());
matrix.setRectToRect(textureRectF, previewRectF, Matrix.ScaleToFit.FILL);
float scale = Math.max((float) width / mTextureView.getWidth(), (float) height / mTextureView.getHeight());
matrix.postScale(scale, scale, centery, centerX);
matrix.postRotate(90 * (rotation - 2), centerX, centery);
} else if (rotation == Surface.ROTATION_0) {
matrix.postRotate(rotation, centerX, centery);
}else if (rotation == Surface.ROTATION_180) {
matrix.postRotate(180, centerX, centery);
}
mTextureView.setTransform(matrix);
}
当设备方向为Surface.Rotation_90或Surface.Rotation_270时,有两个步骤可以构造TextureView的转换:缩放和旋转。在Android演示的代码中,第一步是缩放,第二步是旋转。很难理解。
我更改了两个步骤的顺序。在我的代码中,第一步是旋转,第二步是缩放。
private Matrix mTempMatrix = new Matrix();
private void configureTransform(int viewWidth, int viewHeight) {
Activity activity = getActivity();
if (null == mTextureView || null == mPreviewSize || null == activity) {
return;
}
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
Matrix matrix = new Matrix();
RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
float centerX = viewRect.centerX();
float centerY = viewRect.centerY();
if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
//1. scale
matrix.postRotate(90 * (rotation - 2), centerX, centerY);
//2. rotate
RectF rotatedRect = new RectF(0, 0, viewHeight, viewWidth);
rotatedRect.offset(centerX - rotatedRect.centerX(), centerY - rotatedRect.centerY());
RectF bufferRect = new RectF(0, 0, mPreviewSize.getWidth(), mPreviewSize.getHeight());
bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY());
mTempMatrix.setRectToRect(rotatedRect, bufferRect, Matrix.ScaleToFit.FILL);
matrix.postConcat(mTempMatrix);
float scale = Math.max(
(float) viewHeight / mPreviewSize.getHeight(),
(float) viewWidth / mPreviewSize.getWidth());
matrix.postScale(scale, scale, centerX, centerY);
} else if (Surface.ROTATION_180 == rotation) {
matrix.postRotate(180, centerX, centerY);
}
mTextureView.setTransform(matrix);
}