使用Android camera2进行全屏预览

使用Android camera2进行全屏预览,第1张

概述我正在使用新的camera2 API构建自定义相机.我的代码基于Google here提供的代码示例. 我无法找到一种方法来全屏显示相机.在代码示例中,他们使用比率优化来适应所有屏幕,但它只占屏幕高度的3/4左右. 这是我的AutoFitTextureView代码: public class AutoFitTextureView extends TextureView {private int 我正在使用新的camera2 API构建自定义相机.我的代码基于Google 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,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();}@OverrIDeprotected 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);        }    }}

}

非常感谢您的帮助.

解决方法 这是您的问题的解决方案.在此 line中,纵横比设置为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];    }

然后我更正了this method以根据视频大小纵横比选择预览尺寸,结果如下.

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];    }}

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

总结

以上是内存溢出为你收集整理的使用Android camera2进行全屏预览全部内容,希望文章能够帮你解决使用Android camera2进行全屏预览所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1137018.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存