Mike Talbot's Blog
Posts Tagged Vuforia
Unity, Vuforia & ZXing – Barcode scanning in AR games
Posted by whydoidoit in Project With Code on July 18, 2012
I’ve been struggling with finding a good barcode scanner/QR code reader that works with Unity and doesn’t kill the framerate. Given that I also have an AR application I thought that I would try to incorporate Vuforia with ZXing to get the best of both worlds and the good news is that it works pretty well and is very fast. I’m attaching a download here of the package that contains a Unity friendly version of the C# port of ZXing together with my mulithreading manager Loom class. Then here is an example of a script that will scan barcodes and/or QR codes and fire an event and send a message to a target object when something is decoded.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Linq;
using com.google.zxing.qrcode;
using com.google.zxing.multi;
using com.google.zxing.common;
using com.google.zxing;
using com.google.zxing.client.result;
[AddComponentMenu("System/VuforiaScanner")]
public class VuforiaScanner : MonoBehaviour
{
public QCARBehaviour vuforia;
public bool barcode;
public GameObject target;
public static event Action<ParsedResult, string> Scanned = delegate {};
public static event Action<string> ScannedQRCode = delegate {};
public static event Action<string> ScannedBarCode = delegate {};
bool decoding;
bool initGray;
MultiFormatReader mfReader = new MultiFormatReader();
QRCodeReader qrReader = new QRCodeReader();
void Update()
{
if(vuforia == null)
return;
if(vuforia.enabled && !initGray)
{
//Wait 1/4 seconds for the device to initialize (otherwise it seems to crash sometimes)
initGray = true;
Loom.QueueOnMainThread(()=>{
initGray = CameraDevice.Instance.SetFrameFormat(Image.PIXEL_FORMAT.GRAYSCALE, true);
}, 0.25f);
}
if (vuforia.enabled && CameraDevice.Instance != null && !decoding)
{
var image = CameraDevice.Instance.GetCameraImage(Image.PIXEL_FORMAT.GRAYSCALE);
if (image != null)
{
decoding = true;
Loom.RunAsync(() =>
{
try
{
var bitmap = new BinaryBitmap(new HybridBinarizer(new RGBLuminanceSource(image.Pixels, image.BufferWidth, image.BufferHeight, true)));
Result data;
if (barcode)
{
var reader = new MultiFormatReader();
data = reader.decode(bitmap);
}
else
{
var reader = new QRCodeReader();
data = reader.decode(bitmap);
}
if (data != null)
{
Loom.QueueOnMainThread(() => {
if (data.BarcodeFormat == BarcodeFormat.QR_CODE)
{
ScannedQRCode(data.Text);
if(target != null)
{
target.SendMessage("ScannedQRCode", data.Text, SendMessageOptions.DontRequireReceiver);
}
}
if (data.BarcodeFormat != BarcodeFormat.QR_CODE)
{
ScannedBarCode(data.Text);
if(target != null)
{
target.SendMessage("ScannedBarCode", data.Text, SendMessageOptions.DontRequireReceiver);
}
}
var parsedResult = ResultParser.parseResult(data);
if(target != null)
{
target.SendMessage("Scanned", parsedResult, SendMessageOptions.DontRequireReceiver);
}
Scanned(parsedResult, data.Text);
decoding = false;
});
}
}
catch (Exception e)
{
decoding = false;
}
});
}
}
}
}
Share this:
Welcome!
All code on this blog is open source. You are free to use all of the code on this blog for any purpose, but it all comes without any warranty whatsoever. You must not use any code on this blog to infringe any patent and I can offer no warranty that the presence of code here implies that it is not patented for some purpose by the author or another party. No code posted by the author has been patented for the basic application demonstrated.
The posts on this blog come as a result of a business application I developed for Silverlight and a game I am writing in Unity. I've tried to write posts that reflect things I've searched for and not been able to find elsewhere.
I decided to publish this work as a quid pro quo for all of the hugely useful articles and examples I've learned from and incorporated.
I'm also really glad to get bug reports - this code is being used professionally by me and I need to keep it bug free :)
I'd appreciate a comment or a rating if you decide to go ahead and use anything, feel free to ask questions or make suggestions for improvements!
Best
Mike
Search
Top Rated
- AllChildren ArrangeOverride Automatic Grid Binary Binary Serializer Bug C# ComboBox Component Control custom panel custom silverlight panel dashboard dashboard layout DataBinding DataContract DataContractSerializer DropShadows Element is already the child of another element Element is already the child of another element silverlight Elements is already the child of another element Extensible Application Markup Language Flip Flip Control FrameworkElement visible Grid Grid layout gui ItemsSource Jonathan Feinberg LoadComponent MeasureOverride MEF Microsoft Silverlight Page Turn Plane Projection POCO serialization Programming proportional panel ProportionalPanel Reflection selection lost on ItemsSource update Serialization shared class libararies Share silverlight class .NET Silverlight Silverlight actually visible Silverlight all children Silverlight Binary Serialization Silverlight Binary Serializer Silverlight flip control Silverlight Serialization Source code Stack Overflow Tag cloud TFS UI Performance unity Unity Serializer Visible visual parent VisualState VisualStateManager VisualState Trigger Visual Tree VisualTree WCF Silverlight Word Cloud Wordle XAML XAML debugging XAML Error XamlReader XML [Line: 0 Position: 0]
Author
Mike Talbot is Chief Visionary of 3radical. He started his career as a game programmer working for UbiSoft and Electronic Arts among others. Currently he is programming mobile applications in Javascript, HTML5 and ASP.NET.
email: mike.talbot@3radical.com
twitter: mike_talbot

WinApse Latest- Replatforming Guide: Pros, Cons, and Impact
- Cypress vs Selenium: Why Cypress is Better!
- In-Depth Review: Apache Spark and Flink Side by Side
- C# Null Coalescing Best Practices
- COPY vs ADD in Docker: A Detailed Comparison
- JSON Web Key Sets (JWKS) Ultimate Guide
- What is a Technical Debt Register Template?
- What is Event-Driven Programming?
- Mailtrap Service Review: Pros and Cons
- PUT vs PATCH with REST APIs
Silverlight Show News- An error has occurred; the feed is probably down. Try again later.
