我正在使用Camera2的sample code,我想知道如何在全屏模式下预览和捕获图像?This因此问题似乎在视频模式下解决了它,但我找不到任何图像捕获的解决方案。样品片段在底部有一个蓝色区域,并且也具有bas状态。我想隐藏这两个,并使用整个屏幕来显示预览,并捕获全屏大小的图像。
发布于 2017-12-22 15:50:42
Eddy关于宽高比ratio.The相机传感器为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);
发布于 2017-02-02 22:32:26
请注意,相机传感器的宽高比和设备屏幕的宽高比通常不匹配-传感器的宽高比一般为4:3,屏幕的宽高比一般在16:9左右。尽管两者之间存在很大的差异。
由于宽高比不匹配,您需要决定是否要设置黑条,或者缩放相机预览以使其充满屏幕(但图像的某些部分不可见)。
在任何情况下,要让预览是全屏的,只需要让TextureView (或SurfaceView)成为整个布局。因此,基本上,编辑布局文件:fragment_camera2_basic.xml,删除所有其他视图,并在源代码中删除对它们的引用。或者将它们更改为在TextureView的顶部,而不是旁边--这都是标准的Android UI布局。而且你需要让你的活动成为full-screen。
默认情况下,AutoFillTextureView将尝试保持纵横比,因此它将生成黑条。如果您不想使用黑条,那么您必须更改AutoFillTextureView,这并不简单,我不会在此赘述。
https://stackoverflow.com/questions/41985926
复制相似问题