How to Create QR Code Scanner in Unity

 How to Create QR Code Scanner in Unity

Hi, Dear's here we learn how to implement QR Code Scanner in Unity C#. Ilyasoft software company provide full project step by step training on our YouTube Channel Code With ilyasoft so now subscribe, share and like for more amazing video's 


Description:
First we are going to create unity project.
Add Raw Image to show camera (name : BackgroundRawImage).
Add Image which is mark as Scan Zone (name : ScanZone).
Add Button Mesh Pro to Start Scan the Qr Code (name : Button).
Add Label Mesh Pro to show QRCode Scan result (name : outputTextFromQrcode).
Add Create Empty Object, which is QRCodeReader(name : QrCodeReader).

After creating all these components goto assets folder and add Plugins folder,
Add zxing.unity.dll to plugins folder. 

One all these steps is done then add Scripts folder to assets folder.
Add c# script(QR Code Scanner) to Scripts folder then open the copy the below code and past in c# script file.

after completing these steps watch video and link the components to c# script.
Source Code: 
C# Script Code :
using UnityEngine;
using ZXing;
using TMPro;
using UnityEngine.UI;

public class QRCodeScanner : MonoBehaviour
{
    [SerializeField]
    private RawImage _rawImageBackground;
    [SerializeField]
    private AspectRatioFitter _aspectRatioFitter;
    [SerializeField]
    private TextMeshProUGUI _textOut;
    [SerializeField]
    private RectTransform _scanZone;

    private bool _isCamAvaible;
    private WebCamTexture _cameraTexture;
    void Start()
    {
        SetUpCamera();
    }

    // Update is called once per frame
    void Update()
    {
        UpdateCameraRender();
    }

    private void SetUpCamera()
    {
        WebCamDevice[] devices = WebCamTexture.devices;
        if (devices.Length == 0)
        {
            _isCamAvaible = false;
            return;
        }
        for (int i = 0; i < devices.Length; i++)
        {
            if (devices[i].isFrontFacing == false)
            {
                _cameraTexture = new WebCamTexture(devices[i].name, (int)_scanZone.rect.width, (int)_scanZone.rect.height);
                break;
            }
        }
        _cameraTexture.Play();
        _rawImageBackground.texture = _cameraTexture;
        _isCamAvaible = true;
    }

    private void UpdateCameraRender()
    {
        if (_isCamAvaible == false)
        {
            return;
        }
        float ratio = (float)_cameraTexture.width / (float)_cameraTexture.height;
        _aspectRatioFitter.aspectRatio = ratio;

        int orientation = _cameraTexture.videoRotationAngle;
        orientation = orientation * 3;
        _rawImageBackground.rectTransform.localEulerAngles = new Vector3(0, 0, orientation);

    }
    public void OnClickScan()
    {
        Scan();
    }
    private void Scan()
    {
        try
        {
            IBarcodeReader barcodeReader = new BarcodeReader();
            Result result = barcodeReader.Decode(_cameraTexture.GetPixels32(), _cameraTexture.width, _cameraTexture.height);
            if (result != null)
            {
                _textOut.text = result.Text;
            }
            else {
                _textOut.text = "Failed to Read QR CODE";
            }
        }
        catch
        {
            _textOut.text = "FAILED IN TRY";
        }
    }
}

Comments