Unity中的QR码扫描仪?

问题描述 投票:0回答:1

我正在尝试统一获得可在ios和Android上使用的QRCode阅读器。

Unity Zxing QR code scanner integration

使用以上答案,我添加了Vuforia(可以完美地工作)。然后我还在插件文件夹中添加了Zxing.unity.dll,然后将该脚本添加到了场景中的ARCamera。

using UnityEngine;
using System;
using System.Collections;

using Vuforia;

using System.Threading;

using ZXing;
using ZXing.QrCode;
using ZXing.Common;


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

但是它仍未检测到任何QRCode。除了Zxing以外,还有其他方法可以读取和写入QRcode吗?或您有任何可行的示例项目?

c# unity3d qr-code zxing vuforia
1个回答
0
投票

我还尝试使用与您使用的几乎相同的代码在Vuforia和XZing中实现QRCode阅读器。对我来说,它起作用了,但是检测QRCode花了很长时间。当我使用Color32数组而不是cameraFeed.pixels时,它要快得多:

GUI.DrawTexture(screenRect, webCamTexture, ScaleMode.ScaleToFit);
        try
        {
            IBarcodeReader barcodeReader = new BarcodeReader();
            var result = barcodeReader.Decode(webCamTexture.GetPixels32(),
                webCamTexture.width, webCamTexture.height);

            if (result != null)
            {
                Debug.Log("DECODED TEXT FROM QR: " + result.Text);
                loadNewPoi(Convert.ToInt32(result.Text));

                PlayerPrefs.SetInt("camera_enabled", Convert.ToInt32(false));
                webCamTexture.Stop();
            }
        }

但是在此示例中,我没有使用WebCamTexture而不是Vuforia。不幸的是,无法从Vuforia相机使用GetPixels32()获得Color32阵列。

另一种选择是将QRCode用作图像目标,但是这样做有很多错误检测。

对我来说,目前没有适合与Vuforia一起使用XZing的解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.