【问题标题】:QR Code Scanner in Unity?Unity中的二维码扫描器?
【发布时间】:2017-04-18 11:58:20
【问题描述】:

我正在尝试让 QRCode 阅读器统一在 ios 和 Android 上运行。

Unity Zxing QR code scanner integration

使用上面的答案,我添加了 Vuforia(完美地单独工作)。然后我还在 plugins 文件夹中添加了 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);
        }
    }
}    
}

但它仍然没有检测到任何二维码。除了Zxing,还有其他方法可以进行二维码读写吗?或您拥有的任何工作示例项目?

【问题讨论】:

  • 嗯。上次我做 QR 的东西时,Zxing 非常适合我。
  • @Draco18s 你能分享你的二维码项目吗?
  • 我没有了。那是一家我不再工作的公司,即使我这样做了,它也可能被埋没在四年的旧项目之下。我只记得我们做过一个二维码项目,Zxing 是我们使用的插件,解码没有任何问题。抱歉,我无法提供更多帮助。 :\
  • 好吧,您使用的是Vuforia,因此您可以在技术上将您的二维码设为ImageTarget 并识别它。

标签: c# unity3d qr-code zxing vuforia


【解决方案1】:

我还尝试使用与您使用的代码几乎相同的代码来实现带有 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。 不幸的是,无法使用 GetPixels32() 从 Vuforia 相机获取 Color32 数组。

另一种选择是使用 QRCodes 作为图像目标,但这样做我有很多错误的检测。

对我来说,目前还没有适合 XZing 和 Vuforia 的解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2014-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-15
    相关资源
    最近更新 更多