使用Vuforia,Unity和Zxing的QR码阅读器

时间:2019-03-15 07:59:03

标签: unity3d qr-code zxing vuforia

我正在为我的项目开发QR码解码器。

我引用了Unity Zxing QR code scanner integration

我正在使用Unity 2018+和Vuforia 7+ 在我犯错的地方有人可以帮助我吗?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vuforia;
using ZXing;

using System;


[AddComponentMenu("System/QRScanner")]
public class QRScanner : 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.GRAYSCALE, 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.GRAYSCALE);
                    if (cameraFeed == null)
                    {
                        return;
                    }
                    var data = barCodeReader.Decode(cameraFeed.Pixels, cameraFeed.BufferWidth, cameraFeed.BufferHeight, RGBLuminanceSource.BitmapFormat.RGB24);
                    if (data != null)
                    {
                    // QRCode detected.
                    Debug.Log("Detected");
                        Debug.Log(data.Text);
                    }
                    else
                    {
                        Debug.Log("No QR code detected !");
                    }
                }
                catch (Exception e)
                {
                    Debug.LogError(e.Message);
                }
            }
        }
    }

当前结果:“未检测到QR码”。 我正在使用其他二维码阅读器应用程序的目标

0 个答案:

没有答案