使用Android camera2进行全屏预览

J.M*_*.M. 11 camera android camera2 android-camera2

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

我无法找到一种方法来全屏显示相机.在代码示例中,他们使用比率优化来适应所有屏幕,但它只占屏幕高度的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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

}

非常感谢您的帮助.

小智 18

您应该更改测量的宽度和高度以覆盖全屏,而不是如下所示适合屏幕.

从:

if (width < height * mRatioWidth / mRatioHeight) 
Run Code Online (Sandbox Code Playgroud)

if (width > height * mRatioWidth / mRatioHeight)
Run Code Online (Sandbox Code Playgroud)

它对我来说很好.


MrO*_*zko 5

这是您的问题的解决方案.在该行中,纵横比设置为3/4.我更改了chooseVideSize方法,为MediaRecorder选择具有高分辨率的视频大小.

    private static Size chooseVideoSize(Size[] choices) {
        for (Size size : choices) {
            // Note that it will pick only HD video size, you should create more robust solution depending on screen size and available video sizes
            if (1920 == size.getWidth() && 1080 == size.getHeight()) {
                return size;
            }
        }
        Log.e(TAG, "Couldn't find any suitable video size");
        return choices[choices.length - 1];
    }
Run Code Online (Sandbox Code Playgroud)

然后我更正了此方法,根据视频大小宽高比选择预览大小,结果如下.

private static Size chooseOptimalSize(Size[] choices, int width, int height, Size aspectRatio) {
    // Collect the supported resolutions that are at least as big as the preview Surface
    List<Size> bigEnough = new ArrayList<Size>();
    int w = aspectRatio.getWidth();
    int h = aspectRatio.getHeight();
    double ratio = (double) h / w;
    for (Size option : choices) {
        double optionRatio = (double) option.getHeight() / option.getWidth();
        if (ratio == optionRatio) {
            bigEnough.add(option);
        }
    }

    // Pick the smallest of those, assuming we found any
    if (bigEnough.size() > 0) {
        return Collections.min(bigEnough, new CompareSizesByArea());
    } else {
        Log.e(TAG, "Couldn't find any suitable preview size");
        return choices[1];
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望它会对你有所帮助!

  • 有效 !非常感谢@MrOnyszko!点击屏幕时,您是否也实现了自动对焦?我的预览图片现在有点模糊. (2认同)
  • 如果相机预览质量变差,那么将 max 而不是 min `Collections.max(bigEnough, new CompareSizesByArea());` (2认同)