- Home /
Local Camera to show QRcode with ZXing not turning on in Unity
I wrote a Script with ZXing to call the local Camera on my PC, I attach the script to my camera in unity and the camera showed blocking. Please Help
The script is shown:
using System.Threading;
using UnityEngine;
using ZXing;
using ZXing.QrCode;
public class BarcodeCam : MonoBehaviour
{
// Texture for encoding test
public Texture2D encoded;
private WebCamTexture camTexture;
private Thread qrThread;
private Color32[] c;
private int W, H;
private Rect screenRect;
private bool isQuit;
public string LastResult;
private bool shouldEncodeNow;
void OnGUI()
{
GUI.DrawTexture(screenRect, camTexture, ScaleMode.ScaleToFit);
}
void OnEnable()
{
if (camTexture != null)
{
camTexture.Play();
W = camTexture.width;
H = camTexture.height;
}
}
void OnDisable()
{
if (camTexture != null)
{
camTexture.Pause();
}
}
void OnDestroy()
{
qrThread.Abort();
camTexture.Stop();
}
// It's better to stop the thread by itself rather than abort it.
void OnApplicationQuit()
{
isQuit = true;
}
void Start()
{
encoded = new Texture2D(256, 256);
LastResult = "http://www.bing.com";
shouldEncodeNow = true;
screenRect = new Rect(0, 0, Screen.width, Screen.height);
camTexture = new WebCamTexture();
camTexture.requestedHeight = Screen.height; // 480;
camTexture.requestedWidth = Screen.width; //640;
OnEnable();
qrThread = new Thread(DecodeQR);
qrThread.Start();
}
void Update()
{
if (c == null)
{
c = camTexture.GetPixels32();
}
// encode the last found
var textForEncoding = LastResult;
if (shouldEncodeNow &&
textForEncoding != null)
{
var color32 = Encode(textForEncoding, encoded.width, encoded.height);
encoded.SetPixels32(color32);
encoded.Apply();
shouldEncodeNow = false;
}
}
void DecodeQR()
{
// create a reader with a custom luminance source
var barcodeReader = new BarcodeReader {AutoRotate = false, TryHarder = false};
while (true)
{
if (isQuit)
break;
try
{
// decode the current frame
var result = barcodeReader.Decode(c, W, H);
if (result != null)
{
LastResult = result.Text;
shouldEncodeNow = true;
print(result.Text);
}
// Sleep a little bit and set the signal to get the next frame
Thread.Sleep(200);
c = null;
}
catch
{
}
}
}
private static Color32[] Encode(string textForEncoding, int width, int height)
{
var writer = new BarcodeWriter
{
Format = BarcodeFormat.QR_CODE,
Options = new QrCodeEncodingOptions
{
Height = height,
Width = width
}
};
return writer.Write(textForEncoding);
}
}
The Script need the Zxing.unity.dll file, it is attached with the link below, take it if you need to rebuild the situation: https://drive.google.com/drive/folders/1O3PbiMAdW_TLfDMadPQZWlbCVGgLL4oC?usp=sharing
capture.png
(59.3 kB)
Comment