前置摄像头预览不是全屏

时间:2015-06-03 14:13:55

标签: android camera

我使用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链接制作此相机

提前致谢

2 个答案:

答案 0 :(得分:6)

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

来自:

if (width < height * mRatioWidth / mRatioHeight) 

if (width > height * mRatioWidth / mRatioHeight)

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

答案 1 :(得分:-1)

看起来像代码部分

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

这是你想要的逻辑吗?