Search Unity

Bug QR Scanner won't work on iOS

Discussion in 'Scripting' started by CoralStar, Apr 1, 2022.

  1. CoralStar

    CoralStar

    Joined:
    Jul 14, 2020
    Posts:
    4
    Hello,

    Please let me start off by saying I have only been programming for about 2 years now. I am somewhat knew to it. I am also more of an Android developer than an Apple developer.

    Recently, I have been programming a scavenger hunt game for my job, and the QR system works beautifully on Android. iOS, well, not so much. The camera was rotated and inverted. Upgrading the Unity version did help with it being inverted, and I have some code that can rotate the camera on just iOS. The camera comes up, but it cannot read the QR codes.

    Here is the code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using ZXing;
    7.  
    8. public class QRScanner : MonoBehaviour
    9. {
    10.     WebCamTexture webcamTexture;
    11.     string QrCode = string.Empty;
    12.     public GameObject QRCodeScanner;
    13.     public GameObject PasswordPanel;
    14.     public Text ScanInfo;
    15.     public RawImage RawImage;
    16.     public Button Scan;
    17.     public Button PassPanelClose;
    18.     public GameObject FishMenuButton;
    19.     public Text AddFishButtonText;
    20.     public System_Fish System_Fish;
    21.     public string output = "";
    22.     public string stack = "";
    23.  
    24.     void Start()
    25.     {
    26.         PassPanelClose.onClick.AddListener(() => {
    27.             PasswordPanel.SetActive(false);
    28.         });
    29.  
    30.         Scan.onClick.AddListener(() => {
    31.             if (QRCodeScanner.activeInHierarchy)
    32.             {
    33.                 AddFishButtonText.text = "+";
    34.                 QRCodeScanner.SetActive(false);
    35.                 return;
    36.             }
    37.             bool hasRearCamera = false;
    38.             WebCamDevice[] devices = WebCamTexture.devices;
    39.             // No webcam device was found, revert to fallback method
    40.             if (devices.Length == 0)
    41.             {
    42.                 PasswordPanel.SetActive(true);
    43.             } else
    44.             {
    45.                 foreach (WebCamDevice cam in devices)
    46.                 {
    47.                     if (!cam.isFrontFacing)
    48.                     {
    49.                         hasRearCamera = true;
    50.                         break;
    51.                     }
    52.                 }
    53.             }
    54.  
    55.  
    56.             // Webcam was found but is not a rear facing camera, revert to fallback method
    57.             if (!hasRearCamera)
    58.             {
    59.                 PasswordPanel.SetActive(true);
    60.             } else
    61.             {
    62.                 StartCoroutine(GetQRCode());
    63.             }
    64.         });
    65.     }
    66.  
    67.     IEnumerator GetQRCode()
    68.     {
    69.         AddFishButtonText.text = "X";
    70.         FishMenuButton.SetActive(false);
    71.         QRCodeScanner.SetActive(true);
    72.         // Rear facing webcam was found
    73.         var renderer = RawImage;
    74.         webcamTexture = new WebCamTexture(1129, 512);
    75.         renderer.material.mainTexture = webcamTexture;
    76.  
    77.         IBarcodeReader barCodeReader = new BarcodeReader();
    78.         webcamTexture.Play();
    79.  
    80.         if (Application.platform == RuntimePlatform.IPhonePlayer)
    81.         {
    82.             this.RawImage.rectTransform.sizeDelta = new Vector2(1920, 1080 * this.webcamTexture.width / (float)this.webcamTexture.height);
    83.             this.RawImage.rectTransform.localScale = new Vector3(-1, 1, 1);
    84.             this.RawImage.rectTransform.rotation = Quaternion.Euler(0, 0, 180);
    85.         }
    86.  
    87.         var snap = new Texture2D(webcamTexture.width, webcamTexture.height, TextureFormat.ARGB32, false);
    88.         while (string.IsNullOrEmpty(QrCode) && QRCodeScanner.activeInHierarchy)
    89.         {
    90.             try
    91.             {
    92.                 snap.SetPixels32(webcamTexture.GetPixels32());
    93.                 var Result = barCodeReader.Decode(snap.GetRawTextureData(), webcamTexture.width, webcamTexture.height, RGBLuminanceSource.BitmapFormat.ARGB32);
    94.                 if (Result != null)
    95.                 {
    96.                     QrCode = Result.Text;
    97.                     System_Fish.CollectFish(QrCode);
    98.                     if (!string.IsNullOrEmpty(QrCode))
    99.                     {
    100.                         QrCode = string.Empty;
    101.                         break;
    102.                     }
    103.                 }
    104.             }
    105.             // Catch any issues with webcam not functioning correctly
    106.             catch (Exception ex)
    107.             {
    108.                 Debug.LogWarning(ex.Message);
    109.             }
    110.             yield return null;
    111.         }
    112.         webcamTexture.Stop();
    113.         QRCodeScanner.SetActive(false);
    114.         FishMenuButton.SetActive(true);
    115.         AddFishButtonText.text = "+";
    116.         QrCode = string.Empty;
    117.     }
    118. }
    119.  
    To explain the above a little bit, if the camera is disabled, not allowed, etc. it will fall back to a Password system. If the camera is working and allowed, it will turn on.

    If anyone that understands iOS better than me can explain this to me, that would be awesome.
    Thanks for reading!
     
    Last edited: Apr 1, 2022