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.

Revisions

  1. Sepehr Laal revised this gist Jul 4, 2016. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions ZXingScanner.cs
    Original file line number Diff line number Diff line change
    @@ -11,6 +11,9 @@
    /// 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"
  2. Sepehr Laal created this gist Jul 4, 2016.
    68 changes: 68 additions & 0 deletions ZXingScanner.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    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
    ///
    /// 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();
    }
    }