Unity 中的连续二维码扫描

Continuous QR-Code scanning in Unity

问题只是给出解决方案:)

主要思想是在 Unity 上识别 QR 码,无需任何额外操作,如点击屏幕或类似这样的操作。

(对我来说,"vuforia free"没有必要有水印,所以这是我的解决方案)

(Vuforia 与相机的配合速度更快,无需手动实现自动对焦)

使用 Vuforia 作为网络摄像头源和 ZXing 库作为 QR 识别器的连续 QR 码识别

using UnityEngine;
using Vuforia;
using ZXing;

public class QRCodeReader : MonoBehaviour {

    private bool _isFrameFormatSet;

    IBarcodeReader _barcodeReader = new BarcodeReader();

    void Start () {
        InvokeRepeating("Autofocus", 2f, 2f);
    }

    void Autofocus () {
        CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_TRIGGERAUTO);

        RegognizeQR();
    }

    private Vuforia.Image GetCurrFrame()
    {
        return CameraDevice.Instance.GetCameraImage(Vuforia.Image.PIXEL_FORMAT.GRAYSCALE);
    }

    void RegognizeQR()
    {
        if (!_isFrameFormatSet == _isFrameFormatSet)
        {
            _isFrameFormatSet = CameraDevice.Instance.SetFrameFormat(Vuforia.Image.PIXEL_FORMAT.GRAYSCALE, true);
        }

        var currFrame = GetCurrFrame();

        if (currFrame == null)
        {
            Debug.Log("Camera image capture failure;");
        }
        else
        {
            var imgSource = new RGBLuminanceSource(currFrame.Pixels, currFrame.BufferWidth, currFrame.BufferHeight, true);

            var result = _barcodeReader.Decode(imgSource);
            if (result != null)
            {
                Debug.Log("RECOGNIZED: " + result.Text);
            }
        }
    }
}

不用Vuforia,ofc也可以实现。 Unity 提供了获取摄像头并在网络摄像头纹理上显示其输入的可能性。可以找到更多文档 here.

ZXing 库你可以找到 here, or build it by your own hands using sourse code located on github.

两个库都是跨平台的,所以在不同的设备上一定没有问题。