- Home /
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
Have you already a solution? Have you done it already with Unity Android or iOS and Zxing without Vuforia?
i hope you should be asking this in vuforia forums, since not much people might have worked with it here...
Can I get your solution? Oliver-Ebert@web.de Thank you. :)
Will post the solution on my blog later: my mods to ZXing and multithreaded decoder. Performance is excellent, amazingly better than the Antares decoder I was using before and frame rate remains good.
Answer by whydoidoit · Jul 18, 2012 at 04:39 PM
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.
hey mike, that sounds great!! Im planning to make use of QR scanner in my unity game. Can you give me a heads up on how this is possible .Is there a tutorial for this..Thank you
Sure - what you need to do is download the package from the link. Attach the VuforiaScanner script to something (like the camera). Set the target to a GameObject which has a script that should get the QR code or Barcode. Write a script on with a method:
function Scanned(data : String)
{
//Your code here
}
Attach this script to the target game object.
Your other choice is to add an event handler to the static VuforiaScanner.ScannedQRCode event which will be fired every time a QR code is recognised.
its showing this error when detecting the qrcode
CompareBaseObjectsInternal can only be called from the main thread.Constructors and field initializers will be executed from the loading thread when loading a scene.Don't use this function in the constructor or field initializers, ins$$anonymous$$d move initialization code to the Awake or Start function
and also no output in my guitext....i m using unity 4.3 and vuforia 2.8...
try making sure everything inherits from $$anonymous$$onoBehaviour
Answer by zORg_alex · Aug 24, 2015 at 02:37 PM
Here's what I've found out:
Grab ZXing plugin here
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[i].r = r[i].g = r[i].b = a.Pixels[i];
}
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.
Answer by Shaukeen · Jun 22, 2016 at 04:00 PM
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 http://stackoverflow.com/questions/30546548/unity-zxing-qr-code-scanner-integration ).
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);
}
}
}
}