简体   繁体   中英

Front Camera preview is not full screen

I have made a custom camera on android using camera2 api. I am currently facing an intermittent issue with one device, a OnePlus One. As checked it is working fine with other devices like S3, S4, HTC (all major devices), Moto (all devices).

Please suggest if anything in particular is required/needed to resolve this issue on the OnePlus One and other devices?

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

We have used this link to make this camera

Thanks in advance

You should change measured width and height to cover full screen, not to fit the screen, as below.

From:

if (width < height * mRatioWidth / mRatioHeight) 

to

if (width > height * mRatioWidth / mRatioHeight)

It worked fine for me. Get full screen preview with Android camera2

It looks like that the code section

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

equals to

if (mRatioWidth ==0 && mRatioHeight == 00) {

     int width = MeasureSpec.getSize(widthMeasureSpec);
     int height = MeasureSpec.getSize(heightMeasureSpec);
     setMeasuredDimension(width, height);                

 }else {

     setMeasuredDimension(mRatioWidth, mRatioHeight);
  }

Is it the logic you want ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM