exception - 在 Unity 中使用 ZXing.NET 进行 QR 解码异常

标签 exception unity3d qr-code zxing

我目前正尝试在 Unity(适用于 iOS)中制作一个允许用户扫描二维码的应用程序。 我正在使用针对 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!");
            }

正是在检索结果的文本值时导致了错误。不过,我仍然不知道如何修复它。

有人可以给我建议吗?

谢谢

最佳答案

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

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

关于exception - 在 Unity 中使用 ZXing.NET 进行 QR 解码异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13576333/

相关文章:

java - 如何运行循环直到不生成异常

objective-c - 离线生成二维码

php - 如何使用 PHP 以编程方式柔化图像的边缘?

java - 线程挂起() UnsupportedOperationException

c# - 为什么要创建自定义异常?

Java List.add() UnsupportedOperationException

c# - 在特定位置删除 char,从字符串末尾开始计算

ios - 尝试运行 Unity3D 应用程序时出现 XCode 错误

c# - 在 Unity 5.0 中创建 Wp8 Unity 插件

java - 如何为 Android 应用程序生成二维码?