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/*_