Nasir Khan
Nasir Khan

Reputation: 881

QR Code Scanner in Unity?

I am trying to get QRCode reader in unity that works on ios and Android.

Unity Zxing QR code scanner integration

Using above answer I Have added Vuforia (Working perfectly alone). then i also have added Zxing.unity.dll in plugins folder, then added this script to ARCamera in a scene.

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);
        }
    }
}    
}

But it is still not detecting any QRCode. Is there any other way to do QRcode reading and writing except Zxing? or any working sample project you have?

Upvotes: 1

Views: 14834

Answers (1)

Yleisnero
Yleisnero

Reputation: 11

I also tried to implement a QRCode Reader with Vuforia and XZing using almost the same code you used. For me it worked, but it took very very long to detect the QRCode. When I used a Color32 array instead of cameraFeed.pixels it was much quicker:

GUI.DrawTexture(screenRect, webCamTexture, ScaleMode.ScaleToFit);
        try
        {
            IBarcodeReader barcodeReader = new BarcodeReader();
            var result = barcodeReader.Decode(webCamTexture.GetPixels32(),
                webCamTexture.width, webCamTexture.height);

            if (result != null)
            {
                Debug.Log("DECODED TEXT FROM QR: " + result.Text);
                loadNewPoi(Convert.ToInt32(result.Text));

                PlayerPrefs.SetInt("camera_enabled", Convert.ToInt32(false));
                webCamTexture.Stop();
            }
        }

But in this example I was using a WebCamTexture instead of Vuforia. Unluckily it isn't possible to get a Color32 array with GetPixels32() from the Vuforia camera.

Another option is to use the QRCodes as Image-Targets, but I have a lot of wrong detections doing this.

For me there is no fitting solution for XZing together with Vuforia at the moment.

Upvotes: 1

Related Questions