vuforia + zxing 解析二维码

拷贝下来 直接用。
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;
    Thread qrThread;
    //bool isQuit;
    private Color32[] c;
    private int W, H;
    Image QCARoutput;
    bool updC;
    bool gotResult = false;
    void Start()
    {

    }

    //void OnApplicationQuit()
    //{
       
    //}
    void OnEnable()
    {
        VuforiaBehaviour vuforiaBehaviour = (VuforiaBehaviour)FindObjectOfType(typeof(VuforiaBehaviour));
        if (vuforiaBehaviour)
        {
            vuforiaBehaviour.RegisterTrackablesUpdatedCallback(OnTrackablesUpdated);
        }
        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));

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

            m_RegisteredFormat = true;
        }

        qrThread = new Thread(DecodeQR);
        qrThread.Start();
    }
    void OnDisable()
    {
        Debug.Log("OnDisable");
        VuforiaBehaviour vuforiaBehaviour = (VuforiaBehaviour)FindObjectOfType(typeof(VuforiaBehaviour));
        if (vuforiaBehaviour)
        {
            vuforiaBehaviour.UnregisterTrackablesUpdatedCallback(OnTrackablesUpdated);
        }
        qrThread.Abort();
        qrThread = null;
    }
    public void OnTrackablesUpdated()
    {   
        Vuforia.CameraDevice cam = Vuforia.CameraDevice.Instance;
        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()
    {
        if (reading)
        {
            if (QCARoutput != null)
            {
                if (updC)
                {
                    updC = false;
                    Invoke("ForceUpdateC", 1f);//here we'll postpone next c update, this function literally just switches updC to true
                    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) SceneManager.LoadScene(0);
    }
    void DecodeQR()
    {
        // create a reader with a custom luminance source
        var barcodeReader = new BarcodeReader { AutoRotate = false, TryHarder = false };
        barcodeReader.ResultFound += OnResultF;
        while (true)
        {
            //if (isQuit)//This one is used to be true in OnApplicationQuit() See ZXing's tutorial for more info
            //    break;
            if (reading && c != null)
            {
                try
                {
                    // decode the current frame
                    ZXing.Result result = barcodeReader.Decode(c, W, H);
                    c = null;
                    if (result != null)
                    {
                        QRMessage = result.Text;
                        //download = true;
                        //reading = false;
                        //Debug.Log(QRMessage);     
                        
                    }
                }
                catch(Exception e)
                {
                    Debug.LogError(e.Message);
                }
            }
            else {

            }
            Thread.Sleep(200);
        }
    }
    void OnResultF(Result result)
    {
        Debug.Log(result.Text);
        gotResult = true;
        //SceneManager.LoadScene(0);
    }
    void ForceUpdateC()
    { //To set it to update later
        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;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值