Getting Vuforia to work with zxing

I am trying to use the camera input from Vuforia to read QR codes using zxing. It seems there are two main problems:

  1. How do you get a non-null bytestream from Vuforia? (I’ve tried using WaitForEndOfFrame and LateUpdate – still get null bytes)

  2. Once I get this byte stream, do I have to convert it to get zxing to be able to parse it? (if so, does this work: here)

Edit: Note that the zxing c# port via @WhyDoIDoIt and the VuforiaScanner script works if you comment out all the Loom references. The Loom of @WhyDoIDoIt no longer works. Unity 4.6.1, Vuforia 3.0.9

So I’ve successfully integrated Vuforia/ZXing and Unity to do barcode and QR code scanning. I’ve created a version of ZXing that is Unity compatible and produced an example decoder that are available from here. The answer was to use grayscale images and decode them on a second thread.

Here’s what I’ve found out:

Grab ZXing plugin [here][1]

I’ve used to get Vuforias last version, no told one.

In your project (when you import Vuforia and this ZXing plugins) add AR camera from Vuforias Prefabs, and do not delete original MainCamera, you’ll need it for UI (just set UI’s canvas to be in Overlay mode, without Main Camera I always got UI stretched weirdly on devices). Never fiddle with AR Camera, only if you want to put something as child. It messed my project, whenever I did something to it.

About scripts:

Dont hesitate to look at their original tutorials, because I won’t post all the script, I’ll need to dig through it a lot to remove my stuff and to comment.

Here goes QRReaderScript main parts

	public bool reading;
	public string QRMessage;
	Thread qrThread;
	bool isQuit;
	private Color32[] c;
	private int W, H;
	private Vuforia.Image.PIXEL_FORMAT m_PixelFormat = Vuforia.Image.PIXEL_FORMAT.GRAYSCALE;
	private bool m_RegisteredFormat = false;

	void Start () {
		qrThread = new Thread(DecodeQR);
		qrThread.Start();
		Vuforia.QCARBehaviour qcarBehaviour = (Vuforia.QCARBehaviour) FindObjectOfType(typeof(Vuforia.QCARBehaviour));
		if (qcarBehaviour)
		{
		//Here goes, this one works fine, but whenever you disable
		//the Vuforia.QCARBehaviour to pause AR to preserve battery when
		//app looses focus, QCARoutput is null all the time further on
		qcarBehaviour.RegisterTrackablesUpdatedCallback(OnTrackablesUpdated);
		//This line is what Vuforias guys told me to fix this
		//Still did not test it,
		//for now just terminate app on lost focus more than a minute
		qcarBehaviour.RegisterQCARStartedCallback(OnTrackablesUpdated);
		//TODO is pausing still wrecked?
		}
	}

Update function will need:

		if (reading) {
			if (QCARoutput!=null) {
				if (updC) {
					updC = false;
					Invoke("ForceUpdateC",1f);//here we'll postpone next c update, this function literally just switches updC to true
					if (QCARoutput==null) {
						return;
					}
					c = null;
					c = ImageToColor32(QCARoutput);
					if (W==0 | H==0) {
						W = QCARoutput.BufferWidth;
						H = QCARoutput.BufferHeight;
					}
					QCARoutput = null;
				}
			}
		}

Here if we are reading and QCARoutput is OK and we got c updated, we set next update pending after a sec, and convert QCARoutput to pixel32

Here is the function to get image from Vuforia:

	public void OnTrackablesUpdated()
	{
		if (!reading) return;
		if (!m_RegisteredFormat)
		{
			Vuforia.CameraDevice.Instance.SetFrameFormat(m_PixelFormat, true);
			m_RegisteredFormat = true;
		}

		Vuforia.CameraDevice cam = Vuforia.CameraDevice.Instance;
		QCARoutput = cam.GetCameraImage(m_PixelFormat);
	}

This one is for ZXing:

void DecodeQR() {
		// create a reader with a custom luminance source
		var barcodeReader = new BarcodeReader {AutoRotate = false, TryHarder = false};
		while (true)
		{
			if (isQuit)//This one is used to be true in OnApplicationQuit() See ZXing's tutorial for more info
				break;
			if (reading && c!=null) {
				try
				{
					// decode the current frame
					ZXing.Result result = barcodeReader.Decode(c, W, H);
					c = null;
					if (result != null)
					{
						QRMessage = result.Text;
						//download = true;
						//reading = false;
					}
					
					// Sleep a little bit and set the signal to get the next frame
					Thread.Sleep(200);
				}
				catch
				{
				}
			}
			else {

			}
		}
	}

and last thing:

	void ForceUpdateC() { //To set it to update later
		updC = true;
	}

And the magic converter:

Color32[] ImageToColor32(Vuforia.Image a) {
	if (!a.IsValid()) return null;
	Color32[] r = new Color32[a.BufferWidth * a.BufferHeight];
	for (int i = 0; i< r.Length; i++) {
		r<em>.r = r<em>.g = r_.b = a.Pixels*;*_</em></em>

* }*
* return r;*
* }*
Bump me if I did forget something important. This thing gave me a pain, so I’ll share the crucial things from my script with everyone, but not whole script, so you’ll need to combine it all yourself. Plus I still need to figure out how to reset GetCameraImage results to normal after disabling and re-enabling QCARBehaviour.
And good luck.
_*[1]: http://zxingnet.codeplex.com/*_

We also have implemented the QR detection functionality using ZXing.dll in Unity 5.3.4f1 with Vuforia Unity SDK 5.5.9. We have a QR detection script on GameObject which remains active throughout the app and using below mentioned (QRScanner.cs) code ( as mentioned on Unity Zxing QR code scanner integration - Stack Overflow ).

We are also using Vuforia for image detection (50 image targets) in same scene where QR detection is expected. The Vuforia plugin is getting enabled / disabled multiple times as per our requirement. Both the image and QR detection is working perfectly for us on Android and iOS devices until app is in focus. Whenever VuforiaBehaviour gets disabled and enabled, QR detection stops working thereafter ( as mentioned by @zORg_alex ). QRScanner script always receives null data after app is resumed or AR camera is reloaded. We have tried keeping our QR detection script on AR camera prefab and also tried

 qcarBehaviour.RegisterTrackablesUpdatedCallback(OnTrackablesUpdated);
 qcarBehaviour.RegisterQCARStartedCallback(OnTrackablesUpdated);

callbacks every time AR camera starts but with no success. The QR detection stops working completely after pausing Vuforia plugin for any reason.

Does anybody have any idea how to fix this issue ?

QRScanner.cs

using UnityEngine;
using System;
using System.Collections;
using Vuforia;
using System.Threading;
using ZXing;
using ZXing.QrCode;
using ZXing.Common;

/*		/////////////////	QR detection does not work in editor	////////////////	*/

[AddComponentMenu("System/QRScanner")]
public class QRScanner : MonoBehaviour
{    
	private bool cameraInitialized;
	private BarcodeReader barCodeReader;
	public AppManager camScript;

	void Start()
	{        
		barCodeReader = new BarcodeReader();
		StartCoroutine(InitializeCamera());
	}

	private IEnumerator InitializeCamera()
	{
		// Waiting a little seem to avoid the Vuforia's crashes.
		yield return new WaitForSeconds(3f);

		var isFrameFormatSet = CameraDevice.Instance.SetFrameFormat(Image.PIXEL_FORMAT.RGB888, true);
		Debug.Log(String.Format("FormatSet : {0}", isFrameFormatSet));

		// Force autofocus.
//		var isAutoFocus = CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
//		if (!isAutoFocus)
//		{
//			CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_NORMAL);
//		}
//		Debug.Log(String.Format("AutoFocus : {0}", isAutoFocus));
		cameraInitialized = true;
	}

	private void Update()
	{
		if (cameraInitialized)
		{
			try
			{
				var cameraFeed = CameraDevice.Instance.GetCameraImage(Image.PIXEL_FORMAT.RGB888);
				if (cameraFeed == null)
				{
					return;
				}
				var data = barCodeReader.Decode(cameraFeed.Pixels, cameraFeed.BufferWidth, cameraFeed.BufferHeight, RGBLuminanceSource.BitmapFormat.RGB24);
				if (data != null)
				{
					// QRCode detected.
					Debug.Log(data.Text);
					Application.OpenURL (data.Text);      // our function to call and pass url as text
					data = null;		// clear data
				}
				else
				{
					Debug.Log("No QR code detected !");
				}
			}
			catch (Exception e)
			{
				Debug.LogError(e.Message);
			}
		}
	}
}