Vuforia + zxing parse QR code

Source: Internet
Author: User
Copy and use it directly.
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;    }}

Vuforia + zxing parse QR code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.