【问题标题】:Preview size for barcode scanner from vision api来自视觉 api 的条形码扫描仪的预览尺寸
【发布时间】:2017-02-17 17:49:02
【问题描述】:

我正在使用来自 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 线程,但不知道如何限制检测区域。

标签: java android google-play-services google-vision android-vision


【解决方案1】:

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

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

并在此循环中使用 layoutHeight 而不是 "CameraSourcePreview" 类的 childHeight - for (int i = 0; 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:现在你只用layoutWidth和layoutHeight,可以删掉大部分代码。 2:此方法拉伸视频,不保留纵横比。
  • 我已经通过将相机预览的尺寸设置为比例来修复它,因为我需要更小的相机预览尺寸
  • 在CameraSourcePreview类中找到上面提到的if条件并用提到的代码替换它
【解决方案2】:

有两种方法可以让摄像头图像填满整个屏幕。

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

为了裁剪适合图像,您只需将&gt; 更改为&lt;。找到下面的 if 语句并像这样更改条件:

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

【讨论】:

  • 成功了!非常感谢!!
  • 您的解决方案比我见过的任何其他解决方案都要好(而且更简单......)。在任何情况下(拉伸或裁剪)我们仍然存在问题,但对于 QR 码扫描仪,我会说裁剪要好得多。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-05
  • 2020-11-07
  • 2019-06-12
  • 1970-01-01
  • 1970-01-01
  • 2020-11-18
  • 2019-07-03
相关资源
最近更新 更多