Unity Zxing QR码扫描仪集成

时间:2015-05-30 13:03:49

标签: unity3d zxing qr-code barcode-scanner vuforia

我需要将Zxing与vuforia集成在Unity中制作QR码扫描应用程序吗? 我不知道如何将Zxing与Vuforia整合在一起。有人可以指导我如何做到这一点吗?我有Zxing .dll文件和Vuforia统一包。谢谢你。

3 个答案:

答案 0 :(得分:9)

我今天正在寻找将Zxing与vuforia整合在Unity中。

要做的第一件事就是从https://zxingnet.codeplex.com/下载dll并将unity dll复制到Plugins文件夹(应该在Assets文件夹中)

然后,我设法找到了一些例子(其中一些论文已经过时):

在合并这些示例并简化它们之后,我得到了类似的东西(放置在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);
            }
        }
    }    
}

我设法让它在AVD(Android虚拟设备)中运行,因此它可以在真实设备上运行。

答案 1 :(得分:1)

如果您使用Unity 5.x和64位Windows,则可能会收到错误

  

无法加载资产/插件/ QCARWrapper.dll

解决方案很简单,如问题Unity3d - Failed to load 'Assets/Plugins/QCARWrapper.dll'

中所述
  1. 要将Vuforia与Unity 64位一起使用,只需将QCARWrapper DLL从/Plugins移动到/Plugins/x86.这些是DLL:

  2. 在Unity项目视图中选择QCARWrapper.bundle(位于资产>插件下),以便在Unity Inspector中显示其设置更改Unity检查器中QCARWrapper.bundle的设置从任何平台到独立+编辑。

  3. 比起它的魅力。

答案 2 :(得分:1)

如果您在扫描过程中出现延迟,则此代码应为您提供帮助。我使用了KDelli的答案,并使用了另一个线程来解码qr。

using UnityEngine;
using System;
using System.Collections;

using Vuforia;

using System.Threading;

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


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

private BarcodeReader barCodeReader;
private bool isDecoding = false;

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 && !isDecoding)
    {
        try
        {
            var cameraFeed = CameraDevice.Instance.GetCameraImage(Image.PIXEL_FORMAT.RGB888);

            if (cameraFeed == null)
            {
                return;
            }
            ThreadPool.QueueUserWorkItem(new WaitCallback(DecodeQr), cameraFeed);

        }
        catch (Exception e)
        {
            Debug.LogError(e.Message);
        }
    }
}

private void DecodeQr(object state){
    isDecoding = true;
    var cameraFeed = (Image)state;
    var data = barCodeReader.Decode(cameraFeed.Pixels, cameraFeed.BufferWidth, cameraFeed.BufferHeight, RGBLuminanceSource.BitmapFormat.RGB24);
    if (data != null)
        {
        // QRCode detected.
            isDecoding = false;
        }
    else
        {
            isDecoding = false;
            Debug.Log("No QR code detected !");
        }
}    
}