Skip to content

Instantly share code, notes, and snippets.

@3p3r
Last active February 13, 2024 01:44
Show Gist options
  • Save 3p3r/d52d0ee173c5c28d1f324729b35cb96a to your computer and use it in GitHub Desktop.
Save 3p3r/d52d0ee173c5c28d1f324729b35cb96a to your computer and use it in GitHub Desktop.
Zebra Crossing Unity3D WebCam QR Decoder
using ZXing;
using ZXing.Common;
using UnityEngine;
using System.Collections.Generic;
/// <summary>
/// Extremely basic usage of BarcodeDecoder in ZXing
/// library to actively decode QR codes from WebCam.
/// You may download ZXing C# binaries from:
/// http://zxingnet.codeplex.com/
/// This is tested with Unity3D 5.3.5f1
///
/// A Multi threaded version of this class is available at:
/// https://gist.github.com/sepehr-laal/0b636942406a385097cbd5bcd1130b52
///
/// Attach this script to an object in your scene and
/// hit play. If you hold a QR code in front of your
/// WebCam, its decoded string appears in "decodedResult"
/// </summary>
public class ZXingScanner : MonoBehaviour
{
public string decodedResult;
WebCamTexture webCamTexture;
BarcodeReader barcodeReader;
void Start()
{
var formats = new List<BarcodeFormat>();
formats.Add(BarcodeFormat.QR_CODE);
barcodeReader = new BarcodeReader
{
AutoRotate = false,
Options = new DecodingOptions
{
PossibleFormats = formats,
TryHarder = true,
}
};
webCamTexture = new WebCamTexture();
webCamTexture.Play();
}
void Update()
{
if (webCamTexture != null && webCamTexture.isPlaying)
DecodeQR();
}
void DecodeQR()
{
if (webCamTexture == null)
return;
Result result = barcodeReader.Decode(
webCamTexture.GetPixels32(),
webCamTexture.width,
webCamTexture.height);
if (result != null)
decodedResult = result.Text;
}
void OnDestroy()
{
if (webCamTexture != null)
webCamTexture.Stop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment