android - 使用 Android camera2 获取全屏预览

标签 android camera camera2 android-camera2

我正在使用新的 camera2 API 构建自定义相机。我的代码是基于谷歌提供的代码示例here .

我找不到全屏预览相机的方法。在代码示例中,他们使用比例优化来适应所有屏幕,但它只占用屏幕高度的 3/4 左右。

这是我的 AutoFitTextureView 代码:

public class AutoFitTextureView extends TextureView {

private int mRatioWidth = 0;
private int mRatioHeight = 0;

public AutoFitTextureView(Context context) {
    this(context, null);
}

public AutoFitTextureView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public AutoFitTextureView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

/**
 * Sets the aspect ratio for this view. The size of the view will be measured based on the ratio
 * calculated from the parameters. Note that the actual sizes of parameters don't matter, that
 * is, calling setAspectRatio(2, 3) and setAspectRatio(4, 6) make the same result.
 *
 * @param width  Relative horizontal size
 * @param height Relative vertical size
 */
public void setAspectRatio(int width, int height) {
    if (width < 0 || height < 0) {
        throw new IllegalArgumentException("Size cannot be negative.");
    }
    mRatioWidth = width;
    mRatioHeight = height;
    requestLayout();
}

@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);
        } else {
            setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
        }
    }
}

非常感谢您的帮助。

最佳答案

您应该更改测量的宽度和高度以覆盖整个屏幕,而不是像下面这样适应屏幕。

来自:

if (width < height * mRatioWidth / mRatioHeight) 

if (width > height * mRatioWidth / mRatioHeight)

它对我来说效果很好。

关于android - 使用 Android camera2 获取全屏预览,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39044494/

相关文章:

android - iOS 连接数据库的正确架构?

android - 银河 s3 模拟器

android - 为什么覆盖不能正常工作?

Android Camera2 触摸对焦实现 - 取消新触摸

Android Camera2 API MediaRecoder 如何捕获图像(不是流也不是文件)

android - Android 是否可以访问通话设置的振动?

java - android 将字符串值名称连接到表达式

iphone - UIImagePickerController 与相机源 : video trimming doesn't work

iphone - 如何从 iPhone 相机进行快速图像处理?

android - 在camera2上裁剪矩形分段,传给opencv处理