Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

ZXing Library with Unity

Discussion in 'Scripting' started by AwDogsGo2Heaven, Jun 20, 2015.

  1. AwDogsGo2Heaven

    AwDogsGo2Heaven

    Joined:
    Jan 17, 2014
    Posts:
    102
    I've been trying to get the ZXing.Net unity library to work with Unity..with no success so far. No matter how many bar codes I've put up in front of my webcam it doesn't return any results. Has anyone had any success getting these two to play along? this is my filthy work in progress below:

    Code (CSharp):
    1. public class CodeScanner : MonoBehaviour {
    2.  
    3.     public WebCamTexture CamTexture;
    4.     private Thread CodeScannerThread;
    5.     public RawImage Image;
    6.  
    7.     private Color32[] c;
    8.     private sbyte[] d;
    9.     private int W, H, WxH;
    10.     private int x, y, z;
    11.  
    12.     private string DecryptedData;
    13.  
    14.     public Quaternion baseRotation;
    15.     private Rect screenRect;
    16.  
    17.     private bool isQuit;
    18.  
    19.     public string LastResult;
    20.     private bool shouldEncodeNow;
    21.     public Text DebugT;
    22.  
    23.     void OnEnable () {
    24.         if(CamTexture != null) {
    25.             CamTexture.Play();
    26.             W = CamTexture.width;
    27.             H = CamTexture.height;
    28.             WxH = W * H;
    29.  
    30.         }
    31.     }
    32.  
    33.     void OnDisable () {
    34.         if(CamTexture != null) {
    35.             CamTexture.Pause();
    36.         }
    37.     }
    38.  
    39.     void OnDestroy () {
    40.         CodeScannerThread.Abort();
    41.         CamTexture.Stop();
    42.     }
    43.  
    44.  
    45.  
    46.     void Awake () {
    47.         CamTexture = new WebCamTexture();
    48.  
    49.         c = null;
    50.         OnEnable();
    51.  
    52.         Image.texture = CamTexture;
    53.  
    54.         //flip preview on iOS
    55.         #if UNITY_IOS
    56.         //Image.GetComponent<RectTransform>().localScale = new Vector3(1920f/1080f, -1080f/1920f, 0.0f);
    57.         #endif
    58.         baseRotation = transform.rotation;
    59.  
    60.         CodeScannerThread = new Thread(DecodeQR);
    61.         CodeScannerThread.Start();
    62.     }
    63.  
    64.     void Update () {
    65.         //float scaleY = CamTexture.videoVerticallyMirrored ? -1 : 1;
    66.         //Image.transform.localScale = new Vector3(W, scaleY * H, 0);
    67.  
    68.         //transform.rotation = baseRotation * Quaternion.AngleAxis(CamTexture.videoRotationAngle, Vector3.back);
    69.         if(LastResult != null && LastResult.Trim().Length > 0)
    70.         {
    71.             DebugT.text = LastResult;
    72.         }
    73.         if (c == null)
    74.         {
    75.             Debug.Log ("Reading...");
    76.             CamTexture.GetPixels32(c);
    77.         }
    78.     }
    79.  
    80.     void DecodeQR () {
    81.         var barcodeReader = new ZXing.BarcodeReader();
    82.         barcodeReader.AutoRotate = true;
    83.         barcodeReader.TryInverted = true;
    84.         barcodeReader.Options.TryHarder = true;
    85.         while(true) {
    86.             Debug.Log("Scanning...");
    87.             try {
    88.  
    89.  
    90.                 if(c != null)
    91.                 {
    92.                     Debug.Log("Decoding");
    93.                     Result decode = barcodeReader.Decode(c,W,H);
    94.                     if (decode != null)
    95.                     {
    96.                         Debug.Log ("NOT NULL!");
    97.                         LastResult = decode.Text;
    98.                         shouldEncodeNow = true;
    99.                         print(decode.Text);
    100.                     }
    101.                 }
    102.                 // Sleep a little bit and set the signal to get the next frame
    103.                 Thread.Sleep(200);
    104.                 //c = null;
    105.             }
    106.             catch(Exception e) {
    107.                 Debug.Log (e);
    108.                 continue;
    109.             }
    110.         }
    111.     }
    112. }
    113.  
    Found this call stack in xcode that might hint at why their are issues:

    System.NullReferenceException: A null value was found where an object instance was required.

    at UnityStandardAssets.CrossPlatformInput.TiltInput.OnDisable () [0x00000] in <filename unknown>:0

    at ZXing.PDF417.Internal.Detector.detect (Boolean multiple, ZXing.Common.BitMatrix bitMatrix) [0x00000] in <filename unknown>:0

    at ZXing.PDF417.Internal.Detector.detect (ZXing.BinaryBitmap image, IDictionary`2 hints, Boolean multiple) [0x00000] in <filename unknown>:0

    at ZXing.PDF417.PDF417Reader.decode (ZXing.BinaryBitmap image, IDictionary`2 hints, Boolean multiple) [0x00000] in <filename unknown>:0

    at ZXing.PDF417.PDF417Reader.decode (ZXing.BinaryBitmap image, IDictionary`2 hints) [0x00000] in <filename unknown>:0

    at DG.Tweening.Plugins.Core.PathCore.Path.Draw (DG.Tweening.Plugins.Core.PathCore.Path p) [0x00000] in <filename unknown>:0

    at ZXing.MultiFormatReader.decodeInternal (ZXing.BinaryBitmap image) [0x00000] in <filename unknown>:0
     
    Last edited: Jun 21, 2015
  2. AwDogsGo2Heaven

    AwDogsGo2Heaven

    Joined:
    Jan 17, 2014
    Posts:
    102
    OK, So I figured it out, the UnityEngine dll that they built with is too old. Download the source for ZXing.net, open the Unity project, change the reference to the UnityEngine.dll on your machine rebuild and use this DLL and everything works.

    EDIT:

    Spoke too soon, still getting the above error in XCODE for iOS but I at least have it working for PC

    EDIT 2:

    OK, you can get this to work in XCODE it appears, iOS's WebCamTexture doesn't immediately return its proper width and height, and instead reports 16 initially. I was creating too small of an array for the image data. Coincidently , if you give XZing a 16 by 16 image array it will just ignore it and break the code.
     
    Last edited: Jun 21, 2015
    artemiys930519 likes this.
  3. romaing

    romaing

    Joined:
    Feb 11, 2010
    Posts:
    24
    You're right, OS's WebCamTexture doesn't return its proper width and height and instead reports 16 and there is an active issue http://issuetracker.unity3d.com/issues/ios-webcamtexture-dot-width-slash-height-always-returns-16 about that.
    Did you find a workaround?
     
  4. FrankHao_2and2

    FrankHao_2and2

    Joined:
    Jun 15, 2015
    Posts:
    1
    Hi, guys, I have a similar issue, when I was running the Unity demo on iOS device, I got an exception like this :
    Setting up 1 worker threads for Enlighten.

    Thread -> id: 5f77000 -> priority: 1

    Unhandled Exception: System.NullReferenceException: A null value was found where an object instance was required.

    at ZXing.QrCode.Internal.FinderPatternFinder.find (IDictionary`2 hints) [0x00000] in <filename unknown>:0

    at ZXing.QrCode.Internal.Detector.detect (IDictionary`2 hints) [0x00000] in <filename unknown>:0

    at ZXing.QrCode.QRCodeReader.decode (ZXing.BinaryBitmap image, IDictionary`2 hints) [0x00000] in <filename unknown>:0

    at ZXing.MultiFormatReader.decodeInternal (ZXing.BinaryBitmap image) [0x00000] in <filename unknown>:0

    at ZXing.MultiFormatReader.decode (ZXing.BinaryBitmap image, IDictionary`2 hints) [0x00000] in <filename unknown>:0

    at ZXing.BarcodeReaderGeneric`1[T].Decode (ZXing.LuminanceSource luminanceSource) [0x00000] in <filename unknown>:0

    at ZXing.BarcodeReaderGeneric`1[T].Decode (.T rawRGB, Int32 width, Int32 height) [0x00000] in <filename unknown>:0

    at BarcodeCam.DecodeQR () [0x00000] in <filename unknown>:0

    at System.Threading.ThreadStart.Invoke () [0x00000] in <filename unknown>:0


    (Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 64)

    then I updated my Unity3d to 5.2.3, but the error still there.
     
  5. killer_mech

    killer_mech

    Joined:
    Jan 13, 2015
    Posts:
    4
  6. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    If you want to detect barcodes on iOS and Android using the camera, don't use ZXing. Use NatCam. It can read 8 different types of barcodes including QR, EAN_13, EAN_8, and much more.
     
  7. InvaIidUsername

    InvaIidUsername

    Joined:
    Jun 26, 2017
    Posts:
    2
    Don't use NatCam, NatCorder NatMic, they seem all good products but they are all expensive and buggy as hell
     
  8. bakno

    bakno

    Joined:
    Mar 18, 2007
    Posts:
    604
    I agree. NatCam is a ripoff