Search Unity

Read QR Code in Unity with ARKit and ZXing

Discussion in 'AR' started by c4d3r, Nov 23, 2018.

  1. c4d3r

    c4d3r

    Joined:
    Nov 20, 2018
    Posts:
    2
    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:

    Code (CSharp):
    1. #if UNITY_IOS && !UNITY_EDITOR
    2.    void Update()
    3.    {
    4.        if (!done) {
    5.            ARTextureHandles handles = arSession.GetARVideoTextureHandles();
    6.            //ARTextureHandles handles = UnityARSessionNativeInterface.GetARSessionNativeInterface().GetARVideoTextureHandles();
    7.            if (handles.IsNull())
    8.            {
    9.                return;
    10.            }
    11.            if (handles.TextureY != System.IntPtr.Zero) {
    12.                ReadQRCode (handles.TextureY);
    13.            }
    14.        }
    15.  
    16. }
    17. #endif
    18.  
    19. private void ReadQRCode(System.IntPtr mtlTexPtr)
    20. {
    21.    Debug.Log("---------------");
    22.    Debug.Log("Scanning...");
    23.  
    24.    Resolution currentResolution = Screen.currentResolution;
    25.  
    26.    tex = (UnityEngine.Texture2D)GameObject.Find("Camera").GetComponent<UnityARVideo>().m_ClearMaterial.GetTexture("_textureCbCr");
    27.  
    28.    tex.UpdateExternalTexture(mtlTexPtr);
    29.  
    30.    try
    31.    {
    32.        if(barCodeReader == null) {
    33.            Debug.Log("Could not find barcorereader");
    34.        }
    35.        if(tex == null) {
    36.            Debug.Log("Could not find texture");
    37.        }
    38.  
    39.        var data = barCodeReader.Decode(tex.GetPixels32(), currentResolution.width, currentResolution.height);
    40.        if (data != null)
    41.        {
    42.            Debug.Log("QR: " + data.Text);
    43.        }
    44.        else
    45.        {
    46.            Debug.Log("NO QR: " + "No QR code detected !");
    47.        }
    48.    }
    49.    catch (Exception e)
    50.    {
    51.        Debug.LogError("Error reading QR");
    52.        Debug.LogError(e.Message);
    53.    }
    54. }
     
  2. c4d3r

    c4d3r

    Joined:
    Nov 20, 2018
    Posts:
    2