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);
}
}
}
// 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);