从视觉api预览条形码扫描仪的大小

MMa*_*ian 11 java android google-play-services google-vision android-vision

我正在使用谷歌Android Vision API的条码阅读器示例.预览大小似乎没有填满整个可用空间(我使用的是Nexus 4,预览右侧有一个未使用的白色空间,约为宽度的1/3).

我希望能够在各种设备上运行此示例,并始终将其填满整个可用空间.

所以我一直在玩的是:

CameraSource.Builder builder = new CameraSource.Builder(getApplicationContext(), barcodeDetector).setFacing(CameraSource.CAMERA_FACING_BACK).setRequestedPreviewSize(?, ?).setRequestedFps(15.0f);
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

谢谢!

Aka*_*bey 14

只需从CameraSourcePreview类中删除或注释下面的代码即可

if (childHeight > layoutHeight) {
    childHeight = layoutHeight;
    childWidth = (int)(((float) layoutHeight / (float) height) * width);
}
Run Code Online (Sandbox Code Playgroud)

并在此循环中使用layoutHeight而不是"CameraSourcePreview"类的childHeight - 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);
    }
}
Run Code Online (Sandbox Code Playgroud)


Rei*_*ica 6

有两种方法可以使相机图像填满整个屏幕.

  1. Akesh Dubey的答案,通过拉伸整个图像以适应布局的宽度和高度来显示整个图像.但是,不保留纵横比.
  2. 我在下面的答案,它裁剪图像,使其适应而不牺牲纵横比.

为了裁剪图像,您所要做的就是将一个>变为一个<.找到下面的if语句并改变条件如下:

if (childHeight < layoutHeight) {
    childHeight = layoutHeight;
    childWidth = (int)(((float) layoutHeight / (float) height) * width);
}
Run Code Online (Sandbox Code Playgroud)