Eddy 关于纵横比是正确的。相机传感器是 4:3。屏幕通常为 16:9。示例代码选择显示整个相机预览,因此部分屏幕未填充。您需要拉伸以填满整个屏幕,但是在这种情况下,捕获的图像包含预览中未显示的区域。
要全屏查看,在 AutoFitTextureView 中,将 onMeasure 方法更改为:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
if (0 == mRatioWidth || 0 == mRatioHeight) {
setMeasuredDimension(width, height);
} else {
if (width < height * mRatioWidth / mRatioHeight) {
// setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
} else {
//setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
}
}
}
Run Code Online (Sandbox Code Playgroud)
更新:正如 Eddy 所指出的,这将在全屏上显示相机预览的左上部分,将底部和右侧留在视图之外,结果是图像偏离中心。在我的特殊情况下,主体需要水平居中,因此我修改了变换矩阵以将主体水平居中。这是代码:
// adjust the x shift because we are not showing the whole camera sensor on the screen, need to center the capture area
int screenWidth = Resources.getSystem().getDisplayMetrics().widthPixels;
Log.d(TAG, "screen width " + screenWidth);
int xShift = (viewWidth - screenWidth)/2;
matrix.setTranslate(-xShift, 0);
mTextureView.setTransform(matrix);
Run Code Online (Sandbox Code Playgroud)