'The picture from the unity camera stops coming

My application needs to use the camera (camera from the phone), but when I run it on the phone, a request appears to access the camera. After I click on the consent button, the image does not come. For it to arrive, you need to restart this scene. enter image description here I am using this script:

using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
using UnityEngine.UI;
using ZXing;
using UnityEngine.SceneManagement;
using Newtonsoft.Json.Linq;

public class QRScanner : MonoBehaviour
{
    WebCamTexture webcamTexture;
    string QrCode = string.Empty;

    void Start()
    {
        var renderer = GetComponent<RawImage>();
        webcamTexture = new WebCamTexture(512, 512);
        renderer.material.mainTexture = webcamTexture;
        StartCoroutine(GetQRCode());
    }

    IEnumerator GetQRCode()
    {
        IBarcodeReader barCodeReader = new BarcodeReader();
        webcamTexture.Play();
        var snap = new Texture2D(webcamTexture.width, webcamTexture.height, TextureFormat.ARGB32, false);
        while (string.IsNullOrEmpty(QrCode))
        {
            try
            {
                snap.SetPixels32(webcamTexture.GetPixels32());
                var Result = barCodeReader.Decode(snap.GetRawTextureData(), webcamTexture.width, webcamTexture.height, RGBLuminanceSource.BitmapFormat.ARGB32);
                if (Result != null)
                {
                    QrCode = Result.Text;
                    if (!string.IsNullOrEmpty(QrCode))
                    {
                        Debug.Log("DECODED TEXT FROM QR: " + QrCode);

                        var json = JObject.Parse(QrCode);
                        var Monument = (string) json["Name"];
                        var City = (string) json["City"];

                        DataHolders.Monument = Monument;
                        DataHolders.City = City;
                        break;
                    }
                }
            }
            catch (Exception ex) { Debug.LogWarning(ex.Message); }
            yield return null;
        }
        webcamTexture.Stop();
        SceneManager.LoadScene(3);
    }
}

How can I make the image go after gaining access to the camera?



Solution 1:[1]

You need to Ask for permission runtime and once you get the permission you need to call GetQRCode()

On android you could do something like following

private bool hasCameraPermission = false;

WebCamTexture webcamTexture;
string QrCode = string.Empty;

void Start()
{
    var renderer = GetComponent<RawImage>();
    webcamTexture = new WebCamTexture(512, 512);
    renderer.material.mainTexture = webcamTexture;
}

private void Update() {
    if (!Permission.HasUserAuthorizedPermission(Permission.Camera)) {
        Permission.RequestUserPermission(Permission.Camera);
    }
    else {
        // here you will have camera permission
        if (!hasCameraPermission) {
            hasCameraPermission = true;
            StartCoroutine(GetQRCode());
        }
    }
}

IEnumerator GetQRCode() {
    ...
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Digvijaysinh Gohil