Read QR Code in Unity with ARKit and ZXing

I’m trying to read a QR code with the following libraries:

  • ARKit
  • ZXing

However it doesn’t seem to go well. After several hours I still don’t manage to read out a decent QR code. When debugging i apply the texture to see my result. It looks red because of the Texture Y but other than that it shows the QR code. Interpreting the texture doesn’t return any data analysed by ZXing.

This is the following code i’m using for this:

#if UNITY_IOS && !UNITY_EDITOR
   void Update()
   {
       if (!done) {
           ARTextureHandles handles = arSession.GetARVideoTextureHandles();
           //ARTextureHandles handles = UnityARSessionNativeInterface.GetARSessionNativeInterface().GetARVideoTextureHandles();
           if (handles.IsNull())
           {
               return;
           }
           if (handles.TextureY != System.IntPtr.Zero) {
               ReadQRCode (handles.TextureY);
           }
       }

}
#endif

private void ReadQRCode(System.IntPtr mtlTexPtr)
{
   Debug.Log("---------------");
   Debug.Log("Scanning...");

   Resolution currentResolution = Screen.currentResolution;

   tex = (UnityEngine.Texture2D)GameObject.Find("Camera").GetComponent<UnityARVideo>().m_ClearMaterial.GetTexture("_textureCbCr");

   tex.UpdateExternalTexture(mtlTexPtr);

   try
   {
       if(barCodeReader == null) {
           Debug.Log("Could not find barcorereader");
       }
       if(tex == null) {
           Debug.Log("Could not find texture");
       }

       var data = barCodeReader.Decode(tex.GetPixels32(), currentResolution.width, currentResolution.height);
       if (data != null)
       {
           Debug.Log("QR: " + data.Text);
       }
       else
       {
           Debug.Log("NO QR: " + "No QR code detected !");
       }
   }
   catch (Exception e)
   {
       Debug.LogError("Error reading QR");
       Debug.LogError(e.Message);
   }
}

Issue solved and handled here c# - Read QR Code in Unity with ARKit and ZXing - Stack Overflow

1 Like