Jul
19
2016

Decode QRCode with ZXing.net + Unity

Simple example to decode QRCode from texture image.

– Download XZing.net from http://zxingnet.codeplex.com/
– Unzip, Copy zxing.unity.dll from Unity/ folder into your Unity project (create Plugins/ folder and place the dll there)
– Download some QRCode image, like:
– Create new script (see source below)
– Attach script to empty gameobject
– Assign your QRCode texture into inputTexture field
– Hit Play!
– See console output:

  QR_CODE
  goo.gl/BF4kdV

 

Your Project window structure

xzing_unity

Source: ReadBarcodeFromFile.cs


using UnityEngine;
using System.Collections;
using ZXing;
public class ReadBarcodeFromFile : MonoBehaviour
{
public Texture2D inputTexture; // Note: [x] Read/Write must be enabled from texture import settings
void Start()
{
// create a barcode reader instance
IBarcodeReader reader = new BarcodeReader();
// get texture Color32 array
var barcodeBitmap = inputTexture.GetPixels32();
// detect and decode the barcode inside the Color32 array
var result = reader.Decode(barcodeBitmap, inputTexture.width, inputTexture.height);
// do something with the result
if (result != null)
{
Debug.Log(result.BarcodeFormat.ToString());
Debug.Log(result.Text);
}
}
}


12 Comments + Add Comment

Leave a comment

Connect

Twitter View LinkedIn profile Youtube Github Join Discord Twitch Instagram BlueSky

UnityLauncherPro

Get UnityLauncherPRO and work faster with Unity Projects!
*free unity hub alternative

@unitycoder_com

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.