1

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

Sirconrad
  • 69
  • 1
  • 3
  • 7

2 Answers2

6

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

Community
  • 1
  • 1
Adrian Rusin
  • 605
  • 1
  • 6
  • 10
-1

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 ?

Zephyr
  • 6,123
  • 34
  • 33
  • Hi Zephyr, Thanks for response. This solution does not work for me . the main issue is front camera preview is not full screen only on One plus one mobile. – Sirconrad Jun 05 '15 at 05:37
  • @Sirconrad, I just suppose there maybe something missed in your onMeasure() or onLayout() function, so please check the logic in your code. – Zephyr Jun 05 '15 at 15:06
  • @Sirconrad, as to the One plus phone, maybe you need to install some other sample application to check whether there are some special tricks to get a full screen display on that phone. Or using the tool to check the screen/display information for that phone. – Zephyr Jun 05 '15 at 15:08
  • the One plus phone uses the CYANOGEN 11S operating system based on Android, so that is a customized OS. – Zephyr Jun 05 '15 at 15:27
  • @Zenhyr. I am pretty sure that main issue because of set camera preview method. – Sirconrad Jun 09 '15 at 05:51
  • @Sirconrad, since you need to feed a SurfaceView to camera to draw preview picture and it goes wrong, maybe you can check the size of the surfaceview before you give it to camera. – Zephyr Jun 10 '15 at 14:21
  • Those two code snippets are not equivalent. mRatioWidth/Height are not usually in layout pixel units, they're typically something like 640x480 (the size of the camera output, not the size of the texture view they're being drawn in) – Eddy Talvala Feb 22 '18 at 21:35