Unity Vuforai + QRcode Vuforal 实现二维码识别

Unity Vuforai + QRcode Vuforal 实现二维码识别

准备工具

1.Vuforai AR 插件(Unity2017之后自带)
2. QR CodeBarcode Scanner and Generator4.9 二维码生成加识别插件web

功能开发

忽然接到公司需求,需实现图片和二维码识别,网上查了资料发现有不少问题,因此写了这篇文章。

1.导入两给插件。

在这里插入图片描述

2.编写功能代码
using UnityEngine;
using ZXing;
using ZXing.Client.Result;
using Vuforia;
using System;
using System.Threading;
using Tool;

public class VuforaiARCameraScanQRCode : MonoBehaviour
{
    private VuforaiARCameraScanQRCode _Instance = null;
    private Image.PIXEL_FORMAT mPixelFormat = Image.PIXEL_FORMAT.GRAYSCALE;
    private VuforiaBehaviour mVuforiaBehaviour;
    private bool mIsDecoding = false;
    private bool mIsInit = false;
    BarcodeReader mBarcodeReader = new BarcodeReader();

    //识别二维码回调函数
    public static event Action<string> IdentifyQRCodeCallbackFunction = delegate { };

    private bool mIsQRCode = false;
    /// <summary>
    /// 获得实例
    /// </summary>
    /// <returns></returns>
    public static VuforaiARCameraScanQRCode GetInstance()
    {
        if (_Instance == null)
        {
            _Instance = new GameObject("_VuforaiARCameraScanQRCode").AddComponent<VuforaiARCameraScanQRCode>();
        }
        return _Instance;
    }

    void Start()
    {
        if (mVuforiaBehaviour == null) mVuforiaBehaviour = UnityHelper.GetSceneComponetScript<VuforiaBehaviour>();
        if (mVuforiaBehaviour!=null)
        {
            VuforiaARController.Instance.RegisterTrackablesUpdatedCallback(OnTrackablesUpdated);
        }
    }
       
    public bool GetIsQRCode
    {
        get
        {
            return mIsQRCode;
        }
        set
        {
            mIsQRCode = value;
        }
    }
    public void OnTrackablesUpdated()
    {
        if (mVuforiaBehaviour.enabled && !mIsInit)
        {
            //Wait 1/4 seconds for the device to initialize (otherwise it seems to crash sometimes)
            mIsInit = true;
            Loom.QueueOnMainThread(() =>
            {
                mIsInit = Vuforia.CameraDevice.Instance.SetFrameFormat(mPixelFormat, true);
            }, 0.25f);


        }
    }
    void Update()
    {
        if (mVuforiaBehaviour == null && mIsQRCode)
            return;

        if (mVuforiaBehaviour.enabled && Vuforia.CameraDevice.Instance != null && !mIsDecoding)
        {
            Vuforia.Image image = Vuforia.CameraDevice.Instance.GetCameraImage(mPixelFormat);
            if (image != null)
            {
                mIsDecoding = true;

                Loom.RunAsync(() =>
                {
                    try
                    {
                        var data = mBarcodeReader.Decode(ImageToColor32(image), image.BufferWidth, image.BufferHeight);
                        if (data != null)
                        {
                            Loom.QueueOnMainThread(() =>
                            {
                                if (data.BarcodeFormat == BarcodeFormat.QR_CODE)
                                {
                                    //识别二维码
                                    Debug.LogError(data.Text);
                                    if (IdentifyQRCodeCallbackFunction != null)
                                    {
                                        IdentifyQRCodeCallbackFunction.Invoke(data.Text);
                                    }
                                }
                            });
                        }
                    }
                    finally
                    {
                        mIsDecoding = false;
                    }
                });
            }
        }
    }
    Color32[] ImageToColor32(Vuforia.Image a)
    {
        if (!a.IsValid()) return null;
        Color32[] r = new Color32[a.BufferWidth * a.BufferHeight];
        for (int i = 0; i < r.Length; i++)
        {
            r[i].r = r[i].g = r[i].b = a.Pixels[i];
        }
        return r;
    }
}

3.使用方法
1)new 一个新场景,添加Vuforai 中的AR Camera ,在须要功能的脚本中初始化建立脚本及绑定识别二维码回调函数public event Action<string> IdentifyQRCodeCallbackFunction = delegate { };
2)Test 调用功能脚本。
在这里插入图片描述svg

3)而后运行场景(而后ARCamera 能够识别识别图了)

在这里插入图片描述

注意事项

1).Vuforia.CameraDevice.Instance.SetFrameFormat(mPixelFormat, true);设置帧格式的时候不支持Image.PIXEL_FORMAT.RGB888格式,否则会报“Failed to set frame format”错误。
2).在使用mBarcodeReader.Decode这个函数的时候不能直接使用Vuforia.Image.Pixels,要将Image 转成Color32 格式。
3).代码UnityHelper.GetSceneComponetScript<VuforiaBehaviour>();为本身编写代码,会报错误,你们请使用本身的获取代码。函数

参考大神博客

1.https://blog.csdn.net/caozhaodan/article/details/63254633工具

2.https://blog.csdn.net/xufeifandj/article/details/51744379.net

结尾

第一次写博客,技术有限、写的很差请你们勿喷…插件