从视觉 API 中预览条形码扫描器的大小

12
我正在使用Google的Android Vision API中的条形码阅读器示例。 预览大小似乎没有填满整个可用空间(我正在使用一台Nexus 4手机,在预览右侧有一个未使用的白色空间,大约为宽度的1/3)。
我希望能够在各种设备上运行此示例并始终填满整个可用空间。
因此,我一直在尝试调整以下部分:
CameraSource.Builder builder = new CameraSource.Builder(getApplicationContext(), barcodeDetector).setFacing(CameraSource.CAMERA_FACING_BACK).setRequestedPreviewSize(?, ?).setRequestedFps(15.0f);

有什么想法吗?

谢谢!


我面临着同样的问题,请提供解决方案。我已经浏览了Github的线程,但是不知道如何限制检测区域。 - Madhu
2个回答

15

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

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

在这个循环中,使用"CameraSourcePreview"类的layoutHeight而不是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);
    }
}

1
虽然这个方法在技术上是可行的,但我有两点评论。1:现在你只使用layoutWidth和layoutHeight,可以削减大部分代码。2:这种方法会拉伸视频,而不是保持纵横比。 - Reinstate Monica
我通过将相机预览的大小设置为比例来解决了这个问题,因为我需要更小的相机预览尺寸。 - Akash Dubey
在CameraSourcePreview类中找到上述if条件,并将其替换为所提供的代码。 - Akash Dubey

6

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

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

为了裁剪适配图像,你只需要将一个>改成<。找到以下if语句并像这样更改条件:

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

1
成功了!非常感谢!! - Manikandan Selvanathan
1
你的解决方案比我见过的任何其他方案都要好得多(而且更简单)。无论如何,我们仍然存在一个问题(拉伸或裁剪),但在二维码扫描器的情况下,裁剪会更好。 - Keysaw

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接