Unity+Vuforia+ZXing解析二维码

unity中扫描二维码最经常使用的是EasyCodeScan,但使用后仍是以为太过麻烦。这须要对IOS和Android都有必定了解才能根据本身的想法作更改,这对不少半路出家的unity新手们来讲就有些困难了(固然我也是新手)。
我作的项目主要AR,因此我就想办法将vuforia与二维码扫描结合到一块儿。web

using UnityEngine;
using System.Collections;
using System;
using Vuforia;
using System.Threading;
using ZXing;
using ZXing.QrCode;
using ZXing.Common;
using UnityEngine.SceneManagement;

public class CameraImageAccess : MonoBehaviour
{
    private Image.PIXEL_FORMAT m_PixelFormat = Image.PIXEL_FORMAT.GRAYSCALE;
    private bool m_RegisteredFormat = false;

    public bool reading;
    public string QRMessage;
    public UnityEngine.UI.Text labelQrc;
    public AudioSource audioSource;
    Thread qrThread;
    private Color32[] c;
    private int W, H;
    Image QCARoutput;
    bool updC;
    bool gotResult = false;
    void Start()
    {

    }


    void OnEnable()
    {
                                VuforiaARController.Instance.RegisterTrackablesUpdatedCallback (OnTrackablesUpdated);

        }
        var isAutoFocus = CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
        if (!isAutoFocus)
        {
            CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_NORMAL);
        }
        );


        qrThread = new Thread(DecodeQR);
        qrThread.Start();
    }


    void OnDisable()
    {

        qrThread.Abort();
        qrThread = null;
    }
    public void OnTrackablesUpdated()
    {   
        Vuforia.CameraDevice cam = Vuforia.CameraDevice.Instance;

        if (!m_RegisteredFormat)
        {
            Vuforia.CameraDevice.Instance.SetFrameFormat(m_PixelFormat, true);

            m_RegisteredFormat = true;
        }
        QCARoutput = cam.GetCameraImage(m_PixelFormat);
        if (QCARoutput != null)
        {
            reading = true;

            updC = true;
        }
        else
        {
            reading = false;
            Debug.Log(m_PixelFormat + " image is not available yet");
        }
    }

    void Update()
    {
        CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
        if (reading)
        {
            if (QCARoutput != null)
            {
                if (updC)
                {
                    updC = false;
                    Invoke("ForceUpdateC", 1f);
                    if (QCARoutput == null)
                    {
                        return;
                    }
                    c = null;
                    c = ImageToColor32(QCARoutput);
                    if (W == 0 | H == 0)
                    {
                        W = QCARoutput.BufferWidth;
                        H = QCARoutput.BufferHeight;
                    }
                    QCARoutput = null;
                }
            }
        }

        labelQrc.text = QRMessage;
        if (gotResult) {
            audioSource.Play ();
            gotResult=false;
        }
    }
    void DecodeQR()
    {
        var barcodeReader = new BarcodeReader { AutoRotate = false, TryHarder = false };
        barcodeReader.ResultFound += OnResultF;
        while (true)
        {

            if (reading && c != null)
            {
                try
                {
                    ZXing.Result result = barcodeReader.Decode(c, W, H);
                    c = null;
                    if (result != null)
                    {
                        QRMessage = result.Text;

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

        }
    }
    void OnResultF(Result result)
    {

        Debug.Log(result.Text);
        gotResult = true;

    }
    void ForceUpdateC()
    { 
        updC = true;
    }

    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;
    }
}

直接复制就能够用,固然可能由于unity和vuforia的版本问题,会有一些报错的状况,这就须要查看文档自行更改了。svg