安卓+ZXing条形码扫描库-自定义大小和方向

5
我已成功使用ZXing条形码扫描库,但仅限于横屏模式。
我还成功将相机预览设置为纵向模式,并正确显示它(无需拉伸),但现在条形码根本不起作用。 以下是我对“CameraConfigurationManager.java”中的“setDesiredCameraParameters”所做的更改,以便正确显示相机:
void setDesiredCameraParameters(Camera camera)
{
    Camera.Parameters parameters = camera.getParameters();
    Log.d(TAG, "Setting preview size: " + cameraResolution);
    setFlash(parameters);
    setZoom(parameters);
    camera.setDisplayOrientation(90);
    parameters.set("rotation", 90);
    parameters.setPreviewSize(cameraResolution.y, cameraResolution.x);
    camera.setParameters(parameters);
}

I've tried some solutions mentioned on other places, but either they don't work or they work but cannot show the camera preview correctly. Examples: 如何在横屏模式下使用Zxing? http://code.google.com/p/zxing/issues/detail?id=178#c46 https://github.com/pplante/zxing-android/issues When I'm finished with that, I also need to customize the location and size of the rectangle for the scanning. I know that I need to change "setManualFramingRect" in "CameraManager.java", but I'm not sure if I'm doing it correctly. Here's the code for that:
public void setManualFramingRect(Rect rect)
{
    if (initialized)
    {
        Point screenResolution = configManager.getScreenResolution();
        if (rect.right >= screenResolution.x)
            rect.right = screenResolution.x - 1;
        if (rect.left < 0)
            rect.left = 0;
        if (rect.bottom >= screenResolution.y)
            rect.bottom = screenResolution.y - 1;
        if (rect.top < 0)
            rect.top = 0;
        framingRect = rect;
        Log.d(TAG, "Calculated manual framing rect: " + framingRect);
        framingRectInPreview = null;
    }
    else
        _requestedFramingRect = new Rect(rect);
}

当然,我已经修改了“openDriver”的调用方式:
if (_requestedFramingRect != null)
    setManualFramingRect(_requestedFramingRect);

请帮助我。


编辑:现在我发现它不能在某些设备上工作。它在开始时崩溃,如果你调试,你会看到甚至预览也无法正常工作。


1
我认为下一篇帖子可能会回答我的问题:https://dev59.com/I2Qo5IYBdhLWcg3wMs4L#16252917。然而,由于我很久没有测试zxing库了,我不知道它的工作效果如何。 - android developer
1个回答

2

这并不仅限于此。例如,当相机方向与设备方向不同时,您需要实际“旋转”相机数据(或将其作为垂直扫描)。而且,在使用前置摄像头时,您需要考虑其旋转是反向的。


我尝试了我之前写过的链接 - 它没有起作用。你知道有人成功做到了吗? - android developer
是的,我做了:https://play.google.com/store/apps/details?id=com.srowen.bs.android - Sean Owen
我是说如果有人能够免费分享解决这个问题的方法。 :) - android developer
无论如何,我不明白为什么需要旋转输入图像,因为 QR 码可以在任何方向上读取。 - android developer
是的,QR码在任何旋转角度下都可以使用。如果您正在水平扫描,则1D码不行。 - Sean Owen
那么为什么在我修改代码后,即使没有数据旋转(或者有数据旋转),它也无法工作并找到QR码呢?是否有一些我找不到的东西遗漏了? - android developer

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