12

I'm using the barcode-reader example from Google's Android Vision API. The preview size doesn't seem to fill up the whole space available (I'm using a Nexus 4 and there is a white unused space to the right of preview, about 1/3 of the width).

I would like to be able to run this example on various devices and always have it fill up the whole space available.

So the bit I've been playing with is:

CameraSource.Builder builder = new CameraSource.Builder(getApplicationContext(), barcodeDetector).setFacing(CameraSource.CAMERA_FACING_BACK).setRequestedPreviewSize(?, ?).setRequestedFps(15.0f);

Any ideas?

Thanks!

mmagician
  • 1,970
  • 2
  • 15
  • 26
  • I'm facing the same problem. could please provide the solution. Already i gone through github threads but couldn't get how to limit the detection area. – Madhu Feb 21 '18 at 12:52

2 Answers2

15

just remove or comment below code from CameraSourcePreview class

if (childHeight > layoutHeight) {
    childHeight = layoutHeight;
    childWidth = (int)(((float) layoutHeight / (float) height) * width);
}

and use layoutHeight instead of childHeight of "CameraSourcePreview" class in this loop - for (int i = 0; i < getChildCount(); ++i){...}

if (mCameraSource != null)
    {
        Size size = mCameraSource.getPreviewSize();
        if (size != null)
        {
            width = size.getWidth();
            height = size.getHeight();
        }
    }

    // Swap width and height sizes when in portrait, since it will be rotated 90 degrees
    if (isPortraitMode())
    {
        int tmp = width;

        //noinspection SuspiciousNameCombination
        width = height;
        height = tmp;
    }

    final int layoutWidth = right - left;
    final int layoutHeight = bottom - top;

    // Computes height and width for potentially doing fit width.
    int childWidth = layoutWidth;
    int childHeight = (int) (((float) layoutWidth / (float) width) * height);

    for (int i = 0; i < getChildCount(); ++i)
    {
        getChildAt(i).layout(0, 0, childWidth, layoutHeight);
    }

    try
    {
        startIfReady();
    }
    catch (SecurityException se)
    {
        Log.e(TAG, "Do not have permission to start the camera", se);
    }
    catch (IOException e)
    {
        Log.e(TAG, "Could not start camera source.", e);
    }
}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Akash Dubey
  • 1,508
  • 17
  • 34
  • 1
    While this does technically work, I do have two remarks. 1: right now you only use layoutWidth and layoutHeight, you can cut out most of the code. 2: This method stretches the video, it does NOT preserve aspect ratio. – Reinstate Monica Feb 17 '17 at 09:29
  • I have fixed it by making the size of Camera Preview as in Ratio, Because I need smaller size of Camera preview – Akash Dubey Feb 17 '17 at 14:06
  • At CameraSourcePreview class find the above mentioned if condition and replace it by mentioned code – Akash Dubey May 12 '21 at 07:26
6

There are two ways to make the camera image fill the entire screen.

  1. Akesh Dubey's answer, which displays the whole image by stretching it to fit the layout's width and height. However, aspect ratio is not preserved.
  2. My answer below, which crops the image to make it fit without sacrificing aspect ratio.

In order to crop-fit the image, all you have to do is change one > into a <. Find the below if-statement and change the condition like so:

if (childHeight < layoutHeight) {
    childHeight = layoutHeight;
    childWidth = (int)(((float) layoutHeight / (float) height) * width);
}
Community
  • 1
  • 1
Reinstate Monica
  • 2,767
  • 3
  • 31
  • 40