I think that this is an issue related to performance and compatibility reasons, by default the browser will clear WebGL canvas’s drawing buffer after you’ve drawn to it, as stated in this article:
So I’ve tried to set
“preserveDrawingBuffer”: true
Now, I’m trying to explain better what I’m doing.
Consider this code and attach it to a Plane then add a button or whatever and call Scan():
using UnityEngine;
using System.Collections;
using ZXing;
public class QRCodeReader : MonoBehaviour
{
public GameObject qrCodePlane;
private WebCamTexture webCamTexture;
IEnumerator Start()
{
// Request permission to use the camera
yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
// If permission is granted, start the camera
if (Application.HasUserAuthorization(UserAuthorization.WebCam))
{
// Get the default camera device
WebCamDevice[] devices = WebCamTexture.devices;
webCamTexture = new WebCamTexture(devices[0].name);
// Start the camera
qrCodePlane.GetComponent<Renderer>().material.mainTexture = webCamTexture;
webCamTexture.Play();
}
}
private IBarcodeReader barcodeReader;
private Result m_ScanResult;
public void Scan()
{
barcodeReader = new BarcodeReader { AutoRotate = false, Options = new ZXing.Common.DecodingOptions { TryHarder = false } };
Color32[] m_CameraBuffer = webCamTexture.GetPixels32();
m_ScanResult = barcodeReader.Decode(m_CameraBuffer, webCamTexture.width, webCamTexture.height);
if (m_ScanResult != null)
{
Debug.Log("ChatGPT wins, you are a LOOSER");
}
}
void Update()
{
// Check if the camera is playing and the texture is not null
if (webCamTexture != null && webCamTexture.isPlaying)
{
// Capture a frame from the camera
Texture2D texture = new Texture2D(webCamTexture.width, webCamTexture.height, TextureFormat.RGBA32, false);
texture.SetPixels(webCamTexture.GetPixels());
texture.Apply();
}
}
}
Working on: 2021.3.11f1
Not working on: 2022.2.6f1
Not working on: 2023.1.0b4
Try to run this on Chrome ( win or mobile doesn’t matter, the problem is present on both sides).
The problem is that the call to GetPixels32 is giving a totally black texture when built with 2022.2.6f1.
Hope someone says to me hot to make a build. My project is stucked.
Thank you a lot @Nyro ! Clever work around!
You unblocked our project, because we can at least test our complete workflows and demo for our client.
We can now wait for the official fix without struggling.