[英]Camera2 full screen preview and image capture
涡流是正确的长宽比。摄像头传感器为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);
}
}
}
更新:正如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);
请注意,摄像头传感器的纵横比通常与设备屏幕的纵横比不匹配-传感器通常为4:3,屏幕通常在16:9左右。 虽然两者都有很大的差异。
由于宽高比不匹配,因此您需要确定是否要使用黑条,或缩放相机预览以使其充满屏幕(但是图像的某些部分不可见)。
无论如何,要使预览全屏显示,您只需要使TextureView(或SurfaceView)成为整个布局即可。 因此,基本上,编辑以下布局文件: fragment_camera2_basic.xml ,删除所有其他视图,并在源代码中删除对其的引用。 或将它们更改为TextureView的顶部,而不是旁边的-这是所有标准的Android UI布局。 而且,您需要将“活动”设为全屏 。
默认情况下,AutoFillTextureView将尝试保持宽高比,因此会产生黑条。 如果您不希望出现黑条,则必须更改AutoFillTextureView,这不是很简单,我在这里不再赘述。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.