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);

有任何想法吗?

谢谢!

4

2 回答 2

15

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

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

并在此循环中使用 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);
    }
}
于 2015-12-08T12:39:53.507 回答
6

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

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

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

if (childHeight < layoutHeight) {
    childHeight = layoutHeight;
    childWidth = (int)(((float) layoutHeight / (float) height) * width);
}
于 2017-02-17T09:49:02.180 回答