10

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.

Hussey123
  • 479
  • 1
  • 5
  • 21

3 Answers3

11

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) :

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.

KDelli
  • 188
  • 1
  • 12
  • hey bro...nothing happening just a black screen is appearing in android device.I think camer isn't initializing. – Hussey123 Jun 01 '15 at 14:54
  • It's Vuforia which take the default Webcam for your device. Have you drag&drop the "AR Camera" into your scene ? If you have a Webcam, you can test all the Vuforia features directly in Unity – KDelli Jun 02 '15 at 07:51
  • Fixed the issues with black screen.The value of iFrameFormatSet is also true now.But qr code is not decoding.Always it's value is null.It prints "No QR code is detected !" – Hussey123 Jun 03 '15 at 01:06
  • 1
    I edit my code with a functional one. I change the pixel format to RGB888 and use a BarcodeReader instead of a QRCodeReader. I managed to make this code works on a virtual device. – KDelli Jun 03 '15 at 12:20
  • thanks that really helped! btw it's a bit laggy on my smartphone, is there a way to get it smoother? maybe i miss-setted some impostations? – Sebastiano franceschin May 25 '16 at 21:06
  • @KDelli: can u please share the updated code over here that worked for u – Parth Doshi Jun 07 '16 at 16:31
  • @KDelli: I am trying to use same thing for Hololens with beta-vuforia-extension. I am getting below errors in when I have tried to run solution: Failed to fix references for type ZXing.Common.EncodingOptions – chikka.anddev Nov 02 '16 at 07:21
  • I tried integrating this into Microsoft HoloLens by building the above in Unity. But strangely the application only functions as it's supposed to in Unity and no scans are taken on Hololens. The whole thing crashes when it hits BarcodeReader. Any advice? – Max Feb 17 '17 at 02:59
  • I made the previous code work by replacing RGB888 by GREYSCALE (thanks for the sample by the way!) but the result is still laggy, any idea to improve it? – J'hack le lezard Apr 16 '17 at 21:47
  • Any updates regarding the camera lagging issue? @KDelli – Kanagalingam May 14 '18 at 07:05
  • @J'hacklelezard If you have lags, please see my answer below. – Ruslan Leshchenko Aug 07 '18 at 05:52
  • I have an issue when the app is paused and resumed, Decode returns null. How can I overcome this issue.? – dush88c Feb 15 '19 at 10:42
1

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'

  1. To use Vuforia with Unity 64 bit, just move the QCARWrapper DLLs from /Plugins to /Plugins/x86. These are the DLLs:

  2. 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 of QCARWrapper.bundle in the Unity inspector from Any Platform to Standalone + Editor.

Than it works like a charm.

Zizouz212
  • 4,908
  • 5
  • 42
  • 66
Mashiska
  • 11
  • 1
1

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 !");
        }
}    
}
Ruslan Leshchenko
  • 4,043
  • 4
  • 24
  • 36
  • After testing I noticed that using ThreadPool.QueueUserWorkItem on iOS keeps on stacking worker threads that are not closed after quitting QR scene and consume CPU power. After entering and quitting QR scene a few times this eventually leads to app hang. Is there a a way to close/abort these threads manually? – Saico Oct 03 '18 at 15:35
  • @Ruslan Leshchenko I'm getting cameraFeed=null,Also i have tried with different Image formats,Unfortunately not working – Nandha Feb 15 '19 at 10:13
  • @Nandha Guess you have trouble with camera. Unfortunately can't help with it. – Ruslan Leshchenko Feb 15 '19 at 10:43
  • Here i need to mention that I have changed Image Format to gray scale,Also I have tested with making build for android but not working. – Nandha Feb 15 '19 at 10:50