user1855146
user1855146

Reputation: 41

QR decode exceptions using ZXing.NET in Unity

I'm currently trying to make an application in Unity (for iOS) that allows a user to scan a QR code. I am using the ZXing.NET library which has been optimized for Unity.

This is the current decode thread I am using

    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;
            }

      }
    }

The execution reaches the "Start Decode!" print statement, but fails to reach the "Got past decode!" statement.

This is because the Decode() method is generating an unknown exception every time, even when the camera is looking at a very clear QR code.

For reference: c is of type Color32[] and is generated using WebCamTexture.GetPixels32() W, H are integers representing the width and height of the camera texture.

For some reason, I cannot catch a generic Exception within the catch clause, meaning I cannot determine what kind of exception the Decode() method is generating.

EDIT: The code I have used is adapted from the Unity demo available from the ZXing.NET project. I am using the current version of ZXing.NET. I should also mention that I am currently testing this on an iMac, not on an iOS device or the simulator. I have tried running the Unity demo from scratch and I obtain the same result.

This is how the c variable (Color32[]) is updated:

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



   }

EDIT 2: I have separated the decode stage into two bits, firstly generating the result object then retrieving the text property of the result as shown:

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!");
            }

It is when the text value of the result is being retrieved that is causing the error. I still do not know how to fix it though.

Can someone please advise me?

Thanks

Upvotes: 3

Views: 3954

Answers (2)

user1855146
user1855146

Reputation: 41

I have solved the problem. The problem was that the texture being obtained from the camera was not in the correct aspect ratio and was 'squished'. As a result, the ZXing library could not properly recognise the code.

After correcting this issue, recognition worked flawlessly.

Upvotes: 1

Michael
Michael

Reputation: 2486

The code looks like the unity demo from the ZXing.Net project. I tried the demo again with the current version 0.10.0.0. It works like a charm for me. But I can only test it with unity on windows. I don't have a chance to try it with iOS.

Did you try the latest version of ZXing.Net? What version of unity do you use? Is the variable c correctly set within the Update method?

Upvotes: 2

Related Questions