【问题标题】:Front Camera preview is not full screen前置摄像头预览不是全屏
【发布时间】:2017-02-09 22:06:44
【问题描述】:

我使用 camera2 api 在 android 上制作了一个自定义相机。我目前正面临一个设备的间歇性问题,即 OnePlus One。经检查,它可以与 S3、S4、HTC(所有主要设备)、Moto(所有设备)等其他设备正常工作。

请建议在 OnePlus One 和其他设备上解决此问题是否需要/需要任何特别的东西?

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

@SuppressLint("NewApi")
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
 */
@SuppressLint("NewApi")
public void setAspectRatio(int width, int height) {
    if (width < 0 || height < 0) {
        throw new IllegalArgumentException("Size cannot be negative.");
    }
    mRatioWidth = width;
    mRatioHeight = height;
    requestLayout();
}

@SuppressLint("NewApi")
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    if (mRatioWidth ==0 && mRatioHeight == 00) {
           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);
               }
           }
    }else {
        setMeasuredDimension(mRatioWidth, mRatioHeight);
    }
}

我们使用this 链接制作了这款相机

提前致谢

【问题讨论】:

    标签: android camera


    【解决方案1】:

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

    发件人:

    if (width < height * mRatioWidth / mRatioHeight) 
    

    if (width > height * mRatioWidth / mRatioHeight)
    

    对我来说效果很好。 Get full screen preview with Android camera2

    【讨论】:

      【解决方案2】:

      看起来是代码部分

      if (mRatioWidth ==0 && mRatioHeight == 00) {
                 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);
                     }
                 }
          }else {
              setMeasuredDimension(mRatioWidth, mRatioHeight);
          }
      

      等于

      if (mRatioWidth ==0 && mRatioHeight == 00) {
      
           int width = MeasureSpec.getSize(widthMeasureSpec);
           int height = MeasureSpec.getSize(heightMeasureSpec);
           setMeasuredDimension(width, height);                
      
       }else {
      
           setMeasuredDimension(mRatioWidth, mRatioHeight);
        }
      

      是你想要的逻辑吗?

      【讨论】:

      • 您好 Zephyr,感谢您的回复。这个解决方案对我不起作用。主要问题是前置摄像头预览不是只有一加一手机才能全屏。
      • @Sirconrad,我只是想你的 onMeasure() 或 onLayout() 函数中可能遗漏了一些东西,所以请检查代码中的逻辑。
      • @Sirconrad,对于一加手机,也许你需要安装一些其他的示例应用程序来检查是否有一些特殊的技巧可以在该手机上获得全屏显示。或使用该工具检查该手机的屏幕/显示信息。
      • 一加手机采用基于Android的CYANOGEN 11S操作系统,是定制化操作系统。
      • @Zenhyr。由于设置相机预览方法,我很确定主要问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多