gpt4 book ai didi

android - Camera2全屏预览和图像捕捉

转载 作者:太空宇宙 更新时间:2023-11-03 12:46:49 24 4
gpt4 key购买 nike

我正在使用 sample code对于 Camera2,我想知道如何全屏预览和捕获图像? This SO 问题似乎可以在视频模式下解决,但我找不到任何图像捕获解决方案。示例 fragment 底部有一个蓝色区域,状态也为 bas。我想隐藏这两个并使用整个屏幕来显示预览,并以全屏尺寸捕获图像。

最佳答案

Eddy 关于宽高比是正确的。相机传感器是 4:3。屏幕通常为 16:9。示例代码选择显示整个相机预览,因此部分屏幕未被填充。您需要拉伸(stretch)以填满整个屏幕,但在这种情况下,捕获的图像包括预览中未显示的区域。

要全屏查看,在 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 所指出的,这将在全屏上显示相机预览的左上部分,将底部和右侧留在 View 之外,结果是图像偏离中心。在我的特殊情况下,主体需要水平居中,因此我修改了变换矩阵以将主体水平居中。这是代码:

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

关于android - Camera2全屏预览和图像捕捉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41985926/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com