c# - 将 Unity 3D 与 ZXing 集成

标签 c# unity3d qr-code vuforia image-scanner

我一直在从事一个使用 Unity3D 和 Vuforia 提供增强现实的项目。现在我想集成 XZing 以从二维码获取数据并将这些信息显示在我的一个 Unity Assets /对象中。

包含 XZing Assets 并将 VuforiaScanner.cs 脚本放置到 ARCamera 上后,无法从相机设备对象加载图像。

你知道如何解决这个问题或者知道一些在 Vuforia/Unity3D 中实现 XZing 的教程吗?

这是 VuforiaScanner.cs 的代码片段

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
            {
                /** 
                 * AT THIS POINT CAMERAFEED IS NULL 
                 **/
                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);
            }
        }
    }    
}

提前致谢。

最佳答案

你谷歌了吗

"Unity3D ZXing"

?有大量的讨论,例如:

http://forum.unity3d.com/threads/zxing-library-with-unity.335017/

请注意,正如它所说的那样,

iOS's WebCamTexture doesn't return its proper width and height...

这是 Unity3D 的一个相当大的问题,多年来一直如此,

http://answers.unity3d.com/answers/687987/view.html

 private IEnumerator _workAroundRisibleUnityBug()
     {
     while ( frontCam.width < 100 )
         {
         Debug.Log("the width/height values are not yet ready.");
         Debug.Log( frontCam.width +" " +frontCam.height);
         yield return null;
         }

     Debug.Log("the width/height values are now meaningful.");
     Debug.Log( frontCam.width +" " +frontCam.height);

关于c# - 将 Unity 3D 与 ZXing 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34636559/

相关文章:

javascript - 使用 Phonegap 和 JavaScript 解码二维码

java - 在java中解压字节数组

c# - 如何在 stream.read 中传递一个 long 作为偏移量?

c# - Unity3D:从转换中唯一可用的脚本访问方法

c# - 未知的C#错误

c# - 无法破坏错误的Transform组件

c# - 在 C# 中更改输出语音

c# - 我可以在 tfs 上检查我的 cs 项目属性吗?

c# - 错误 403 Google API 电子邮件设置 .Net 正在获取签名

javascript - 客户端之间的离线 PWA 通信