【问题标题】:QR decode exceptions using ZXing.NET in UnityUnity 中使用 ZXing.NET 的 QR 解码异常
【发布时间】:2012-11-27 02:02:33
【问题描述】:

我目前正在尝试在 Unity(适用于 iOS)中创建一个允许用户扫描 QR 码的应用程序。 我正在使用针对 Unity 进行了优化的 ZXing.NET 库。

这是我正在使用的当前解码线程

    void DecodeQR()
   {  
    // create a reader with a custom luminance source

      var barcodeReader = new BarcodeReader {AutoRotate=false, TryHarder=false};

      while (true)

      {
         if (isQuit)

            break;

         try

         {
            string result = "Cry if you see this."; 

            // decode the current frame

            if (c != null){

                print ("Start Decode!");
                result = barcodeReader.Decode(c, W, H).Text; //This line of code is generating unknown exceptions for some arcane reason
                print ("Got past decode!");
            }       
            if (result != null)
            {           
               LastResult = result;        
               print(result);
            }
            // Sleep a little bit and set the signal to get the next frame
        c = null;
            Thread.Sleep(200); 
         }
            catch 
            {   
                continue;
            }

      }
    }

执行到达“开始解码!”打印语句,但未能达到“已通过解码!”声明。

这是因为 Decode() 方法每次都会产生一个未知的异常,即使相机正在查看一个非常清晰的二维码。

供参考: c 是 Color32[] 类型,使用 WebCamTexture.GetPixels32() 生成 W,H 是表示相机纹理宽度和高度的整数。

由于某种原因,我无法在 catch 子句中捕获通用异常,这意味着我无法确定 Decode() 方法正在生成哪种异常。

编辑:我使用的代码改编自 ZXing.NET 项目提供的 Unity 演示。我正在使用当前版本的 ZXing.NET。我还应该提到,我目前正在 iMac 上进行测试,而不是在 iOS 设备或模拟器上进行测试。我尝试从头开始运行 Unity 演示,并获得了相同的结果。

这就是 c 变量 (Color32[]) 的更新方式:

void Update()
   {
      if (c == null)
      {
            c = camTexture.GetPixels32();
            H = camTexture.height;
            W = camTexture.width;
      }



   }

编辑2:我将解码阶段分成两个位,首先生成结果对象,然后检索结果的文本属性,如图所示:

if (c != null){
                print ("Start Decode!");
                var initResult = barcodeReader.Decode(c, W, H); 
                print ("Got past decode!");
                result = initResult.Text; //This line of code is generating unknown exceptions for some arcane reason
                print ("Got past text conversion!");
            }

是在检索结果的文本值时导致错误。不过我还是不知道怎么解决。

有人可以给我建议吗?

谢谢

【问题讨论】:

    标签: exception unity3d qr-code zxing


    【解决方案1】:

    代码看起来像来自 ZXing.Net 项目的统一演示。 我使用当前版本 0.10.0.0 再次尝试了演示。它对我来说就像一个魅力。 但我只能在 Windows 上统一测试它。我没有机会在 iOS 上试用它。

    你试过最新版的ZXing.Net吗?你使用什么版本的统一? 变量 c 是否在 Update 方法中正确设置?

    【讨论】:

    • 我使用的代码改编自同一个 Unity 演示。我正在使用当前版本的 ZXing.NET。我还应该提到,我目前正在 iMac 上进行测试,而不是在 iOS 设备或模拟器上进行测试。我尝试从头开始运行 Unity 演示,并获得了相同的结果。
    • 请试试这个:print ("Got past decode!"); if (initResult != null) result = initResult.Text; print ("Got past text conversion!"); initResult 是 null 如果 BarcodeReader 无法解码图像中的条形码。
    【解决方案2】:

    我已经解决了这个问题。问题是从相机获得的纹理没有正确的纵横比并且被“压扁”。结果导致ZXing库无法正确识别代码。

    纠正此问题后,识别工作完美无缺。

    【讨论】:

      猜你喜欢
      • 2019-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多