{"id":824,"date":"2020-09-08T20:22:15","date_gmt":"2020-09-08T10:22:15","guid":{"rendered":"http:\/\/www.nickdu.com\/?p=824"},"modified":"2020-09-08T20:22:15","modified_gmt":"2020-09-08T10:22:15","slug":"scanning-qr-codes-with-c-in-unity-3d","status":"publish","type":"post","link":"https:\/\/nickdu.com\/?p=824","title":{"rendered":"Scanning QR codes with C# in Unity 3D"},"content":{"rendered":"\n<p>It&#8217;s been a while since my last post. These days, I have been busy with our <a rel=\"noreferrer noopener\" href=\"https:\/\/unity.com\/\" target=\"_blank\">Unity<\/a> project to integrate Unity3D graphic engine into our software eco system. An interesting sub-task is to implement a QR code scanner in our Unity project.<\/p>\n\n\n\n<p>The <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/micjahn\/ZXing.Net\" target=\"_blank\">ZXing.Net<\/a> library did a wonderful job for me. You can download from <a rel=\"noreferrer noopener\" href=\"https:\/\/www.nuget.org\/packages\/ZXing.Net\/\" target=\"_blank\">nuget<\/a> or <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/micjahn\/ZXing.Net\" target=\"_blank\">github<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System.Collections;\nusing System.Collections.Generic;\nusing System.Net;\nusing UnityEngine;\nusing UnityEngine.UI;\nusing ZXing;\n\npublic class QRScanner : MonoBehaviour\n{\n    WebCamTexture webcamTexture;\n    string QrCode = string.Empty;\n    public AudioSource beepSound;\n\n    void Start()\n    {\n        var renderer = GetComponent&lt;RawImage>();\n        webcamTexture = new WebCamTexture(512, 512);\n        renderer.material.mainTexture = webcamTexture;\n        StartCoroutine(GetQRCode());\n    }\n\n    IEnumerator GetQRCode()\n    {\n        IBarcodeReader barCodeReader = new BarcodeReader();\n        webcamTexture.Play();\n        var snap = new Texture2D(webcamTexture.width, webcamTexture.height, TextureFormat.ARGB32, false);\n        while (string.IsNullOrEmpty(QrCode))\n        {\n            try\n            {\n                snap.SetPixels32(webcamTexture.GetPixels32());\n                var Result= barCodeReader.Decode(snap.GetRawTextureData(), webcamTexture.width, webcamTexture.height, RGBLuminanceSource.BitmapFormat.ARGB32);\n                if (Result!= null)\n                {\n                    QrCode = Result.Text;\n                    if (!string.IsNullOrEmpty(QrCode))\n                    {\n                         Debug.Log(\"DECODED TEXT FROM QR: \" + QrCode);\n                        break;\n                    }\n                }\n            }\n            catch (Exception ex) { Debug.LogWarning(ex.Message); }\n            yield return null;\n        }\n        webcamTexture.Stop();\n    }\n}<\/code><\/pre>\n\n\n\n<p>I was using Unity 2019 LTS to experiment with this.<\/p>\n\n\n\n<p>The whole project can be found at <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/nickdu088\/unityqrscanner\" target=\"_blank\">https:\/\/github.com\/nickdu088\/unityqrscanner<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s been a while since my last post. These days, I have been busy with our Unity project to integrate Unity3D graphic engine into our software eco system. An interesting sub-task is to implement a QR code scanner in our Unity project. The ZXing.Net library did a wonderful job for me. You can download from &hellip; <a href=\"https:\/\/nickdu.com\/?p=824\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Scanning QR codes with C# in Unity 3D&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,2],"tags":[],"class_list":["post-824","post","type-post","status-publish","format-standard","hentry","category-net","category-it"],"_links":{"self":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts\/824","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=824"}],"version-history":[{"count":0,"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts\/824\/revisions"}],"wp:attachment":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=824"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=824"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=824"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}