Unity中的QR码扫描仪?

时间:2017-04-18 11:58:21

标签: c# unity3d qr-code zxing vuforia

我正在努力让QRCode读卡器在ios和Android上运行。

Unity Zxing QR code scanner integration

使用上面的答案我添加了Vuforia(完美独立工作)。然后我也在插件文件夹中添加了Zxing.unity.dll,然后在场景中将此脚本添加到ARCamera。

using UnityEngine;
using System;
using System.Collections;

using Vuforia;

using System.Threading;

using ZXing;
using ZXing.QrCode;
using ZXing.Common;


[AddComponentMenu("System/VuforiaScanner")]
public class VuforiaScanner : MonoBehaviour
{    
private bool cameraInitialized;

private BarcodeReader barCodeReader;

void Start()
{        
    barCodeReader = new BarcodeReader();
    StartCoroutine(InitializeCamera());
}

private IEnumerator InitializeCamera()
{
    // Waiting a little seem to avoid the Vuforia's crashes.
    yield return new WaitForSeconds(1.25f);

    var isFrameFormatSet = CameraDevice.Instance.SetFrameFormat(Image.PIXEL_FORMAT.RGB888, true);
    Debug.Log(String.Format("FormatSet : {0}", isFrameFormatSet));

    // Force autofocus.
    var isAutoFocus = CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
    if (!isAutoFocus)
    {
        CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_NORMAL);
    }
    Debug.Log(String.Format("AutoFocus : {0}", isAutoFocus));
    cameraInitialized = true;
}

private void Update()
{
    if (cameraInitialized)
    {
        try
        {
            var cameraFeed = CameraDevice.Instance.GetCameraImage(Image.PIXEL_FORMAT.RGB888);
            if (cameraFeed == null)
            {
                return;
            }
            var data = barCodeReader.Decode(cameraFeed.Pixels, cameraFeed.BufferWidth, cameraFeed.BufferHeight, RGBLuminanceSource.BitmapFormat.RGB24);
            if (data != null)
            {
                // QRCode detected.
                Debug.Log(data.Text);
            }
            else
            {
                Debug.Log("No QR code detected !");
            }
        }
        catch (Exception e)
        {
            Debug.LogError(e.Message);
        }
    }
}    
}

但它仍未检测到任何QRCode。除了Zxing之外,还有其他方法可以进行QRcode读写吗?或者你有任何工作样本项目?

1 个答案:

答案 0 :(得分:1)

我还尝试使用与您使用的几乎相同的代码,通过Vuforia和XZing实现QRCode阅读器。对我来说,它起作用了,但是检测QRCode花了很长时间。 当我使用Color32数组而不是cameraFeed.pixels时,它要快得多:

GUI.DrawTexture(screenRect, webCamTexture, ScaleMode.ScaleToFit);
        try
        {
            IBarcodeReader barcodeReader = new BarcodeReader();
            var result = barcodeReader.Decode(webCamTexture.GetPixels32(),
                webCamTexture.width, webCamTexture.height);

            if (result != null)
            {
                Debug.Log("DECODED TEXT FROM QR: " + result.Text);
                loadNewPoi(Convert.ToInt32(result.Text));

                PlayerPrefs.SetInt("camera_enabled", Convert.ToInt32(false));
                webCamTexture.Stop();
            }
        }

但是在此示例中,我使用的是WebCamTexture而不是Vuforia。 不幸的是,无法从Vuforia相机使用GetPixels32()获得Color32阵列。

另一种选择是将QRCode用作图像目标,但是这样做有很多错误检测。

对我来说,目前尚无适合与Vuforia一起使用XZing的解决方案。

相关问题