I need to integrate Zxing with vuforia to make a QR code scanning app in Unity? I have no idea how to integrate Zxing with Vuforia in unity.Can someone guide me how to do this?I have Zxing .dll files and Vuforia unity package.Thanks in Advance.
I was looking for integrating Zxing with vuforia in Unity today.
The first thing to do is to download the dll from : https://zxingnet.codeplex.com/ and copy the unity dll into your Plugins folder (which should be in the Assets folder)
Then, I managed to found some examples (some of theses is outdated) :
http://ydaira.blogspot.fr/2012/09/how-to-decode-qr-codes-using-unity3d.html
https://github.com/Redth/ZXing.Net/blob/master/Clients/VuforiaDemo/Assets/VuforiaScanner.cs
After merging theses examples and simplify them, I got something like this (which is placed of the ARCamera prefab) :
using UnityEngine;
using System;
using System.Collections;
using Vuforia;
using System.Threading;
using ZXing;
using ZXing.QrCode;
using ZXing.Common;
[AddComponentMenu("System/VuforiaScanner")]
public class VuforiaScanner : MonoBehaviour
{
private bool cameraInitialized;
private BarcodeReader barCodeReader;
void Start()
{
barCodeReader = new BarcodeReader();
StartCoroutine(InitializeCamera());
}
private IEnumerator InitializeCamera()
{
// Waiting a little seem to avoid the Vuforia's crashes.
yield return new WaitForSeconds(1.25f);
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);
}
else
{
Debug.Log("No QR code detected !");
}
}
catch (Exception e)
{
Debug.LogError(e.Message);
}
}
}
}
I manage to make it works in an AVD (Android Virtual Device), so it will works on a real device.
If you use Unity 5.x and 64-bit Windows you may get an error
Failed to load Assets/Plugins/QCARWrapper.dll
Solution is simple as stated in the question Unity3d - Failed to load 'Assets/Plugins/QCARWrapper.dll'
To use Vuforia with Unity 64 bit, just move the QCARWrapper DLLs from
/Plugins
to/Plugins/x86.
These are the DLLs:Select the
QCARWrapper.bundle
in the Unity Project view (located under Assets > Plugins ), so that its settings are shown in the Unity Inspector Change the settings ofQCARWrapper.bundle
in the Unity inspector from Any Platform to Standalone + Editor.
Than it works like a charm.
If you have lags during scanning, this code should help you. I used KDelli's answer and used another thread for the code of decoding qr.
using UnityEngine;
using System;
using System.Collections;
using Vuforia;
using System.Threading;
using ZXing;
using ZXing.QrCode;
using ZXing.Common;
[AddComponentMenu("System/VuforiaCamera")]
public class VuforiaCamera : MonoBehaviour
{
private bool cameraInitialized;
private BarcodeReader barCodeReader;
private bool isDecoding = false;
void Start()
{
barCodeReader = new BarcodeReader();
StartCoroutine(InitializeCamera());
}
private IEnumerator InitializeCamera()
{
// Waiting a little seem to avoid the Vuforia's crashes.
yield return new WaitForSeconds(1.25f);
// 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 && !isDecoding)
{
try
{
var cameraFeed = CameraDevice.Instance.GetCameraImage(Image.PIXEL_FORMAT.RGB888);
if (cameraFeed == null)
{
return;
}
ThreadPool.QueueUserWorkItem(new WaitCallback(DecodeQr), cameraFeed);
}
catch (Exception e)
{
Debug.LogError(e.Message);
}
}
}
private void DecodeQr(object state){
isDecoding = true;
var cameraFeed = (Image)state;
var data = barCodeReader.Decode(cameraFeed.Pixels, cameraFeed.BufferWidth, cameraFeed.BufferHeight, RGBLuminanceSource.BitmapFormat.RGB24);
if (data != null)
{
// QRCode detected.
isDecoding = false;
}
else
{
isDecoding = false;
Debug.Log("No QR code detected !");
}
}
}
来源:https://stackoverflow.com/questions/30546548/unity-zxing-qr-code-scanner-integration