Barcode Scanner in Unity

This guide describes the process of adding the Vuforia Barcode Scanner to your app with just a few simple steps.

Please note that Barcode Scanner requires a Premium license but is free to try with a Basic license.  

QuickStart Guide

The best way to try out and start with barcode detection is with the Vuforia Core Samples for Unity. The scene can be used freely as a basis of your own barcode-detecting application.

  1. Open an empty Unity Project and add the samples from the Assets Store.
  2. Open the Barcodes Scene and test any of the supported code types in Play Mode webcam or recording.

The barcode is controlled by the BarcodeScanner GameObject and its BarcodeScanner.cs component with user interface elements to select the closest barcode or QR-code.

Setup Barcode in Unity

Set up a scene with the Vuforia Barcode Scanning feature. See Supported Versions for Unity Editor version and device support.

  1. Set up Unity with Vuforia Engine as described in the Getting Started with Vuforia Engine in Unity.
  2. Add the Barcode from GameObject -> Vuforia Engine -> Barcode.

  3. You can print the attached QR-code that is in Packages/com.ptc.vuforia.engine/Vuforia/Textures/QRCode.png but it can detect any of the supported code types should you want to use your own.
  4. When a barcode is detected in Play Mode or at runtime, the BarcodeBehaviour is cloned as a new instance with a unique id.
  5. To better visualize that a barcode or QR-code is tracked, you can add the Barcode Outline Behaviour component to the Barcode GameObject.

Barcode Behaviour

With this component, you set the parameter to Detect Multiple Barcodes.

The Barcode Behaviour component also defines which barcode types to detect and track. Note that the Barcode transform will appear to have 3D coordinates, but it is being provided on the video background plane with no relative depth. As a result, it is only possible to align 2D content such as a box outline to the barcode and it is not possible to align 3D content to the Barcode GameObject. To add content in the world space, use VuMarks instead, or a combination of Barcode, Ground PlaneArea Targets, or Instant Image Targets.

Barcode Outline Behaviour

The Barcode Outline Behaviour component renders a rectangular outline over the tracked barcode. You can change its Material and Outline Thickness.

Configuration Options

We can configure the scene to select a barcode and display the data of the detected in the camera view.

With a simple script attached to the Barcode GameObject, we can display the instanceData.Text of a tracked barcode via a UI Text object in the scene.

  1. Add to your scene a UI -> Text (TMP) GameObject.
  2. Add to the Barcode GameObject the cs from the snippet below.
  3. Set the Text (TMP) as the object in Barcode As Text.
  4. Finally, uncheck the Detect Multiple Barcodes in the Barcode Behaviour
    using UnityEngine;
    using Vuforia;
    
    public class SimpleBarcodeScanner : MonoBehaviour
    {
        public TMPro.TextMeshProUGUI barcodeAsText;
        BarcodeBehaviour mBarcodeBehaviour;
        void Start()
        {
            mBarcodeBehaviour = GetComponent<BarcodeBehaviour>();
        }
    
        // Update is called once per frame
        void Update()
        {
            if (mBarcodeBehaviour != null && mBarcodeBehaviour.InstanceData != null)
            {
                barcodeAsText.text = mBarcodeBehaviour.InstanceData.Text;
            }
            else
            {
                barcodeAsText.text = "";
            }
        }
    }
  5. In Play Mode, the barcode that is currently tracked or was last detected has its data load printed on the screen.

Select what barcode to read

We can further extend this use case to select which barcode to read with a mouse click or screen tap rather than what is tracked. In the following example, we test our interaction with a webcam in Play Mode.

  1. In an empty scene, add an ARCameraBarcode, a UI -> Text (TMP), and a new empty GameObject and name it BarcodeScanner.
  2. Attach the BarcodeScanner.cs script to the BarcodeScanner. It listens for a primary mouse click and sends a Raycast to interact with a collider. The instanceData.Text is then displayed in the Text object.
    using UnityEngine;
    using Vuforia;
    
    public class BarcodeScanner : MonoBehaviour
    {
        public TMPro.TextMeshProUGUI barcodeAsText;
        void Update()
        {
            if (Input.GetMouseButtonDown(0))
            {
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    
                if (Physics.Raycast(ray.origin, ray.direction, out RaycastHit hit))
                {
    
                    barcodeAsText.text = hit.collider.GetComponent<BarcodeBehaviour>().InstanceData.Text;
                }
            }
        }
    }
  3. Add the BarcodeCollider.cs and the BarcodeOutlineBehaviour.cs to the Barcode GameObject. It is needed for making the mesh from the barcode’s outline vertices. BarcodeCollider then uses that mesh to create a collider for the Raycast to interact with.
using UnityEngine;
using Vuforia;

public class BarcodeCollider : MonoBehaviour
{
    BarcodeBehaviour mBarcodeBehaviour;
    MeshCollider mMeshCollider;

    void Start()
    {

        mBarcodeBehaviour = GetComponent<BarcodeBehaviour>();
        if (mBarcodeBehaviour != null)
        {
            mBarcodeBehaviour.OnBarcodeOutlineChanged += OnBarcodeOutlineChanged;
        }
    }

    void OnBarcodeOutlineChanged(Vector3[] vertices)
    {
        UpdateMeshCollider(vertices);
    }

    void UpdateMeshCollider(Vector3[] vertices)
    {        
        if (!mMeshCollider)
        {
            mMeshCollider = gameObject.AddComponent<MeshCollider>();
            mMeshCollider.cookingOptions = MeshColliderCookingOptions.None;
        }

        Mesh mesh = new Mesh();
        mesh.vertices = vertices;
        mesh.triangles = new int []{ 0, 1, 2, 0, 2, 3 }; // Creates 2 triangles

        mMeshCollider.sharedMesh = mesh;
    }
}
  1. In Play Mode, press the left mouse button while hovering over a barcode to have the information printed on the screen.

Additional Configurations

The above example and the Vuforia Core Sample Barcode scene demonstrates the most common use case for barcode detection and tracking. But, you can continue to develop your logic and for example subscribe to the Action event OnBarcodeOutlineChanged() that fires whenever an instance’s outline has changed.

Furthermore, the Barcode.InstanceData can also be used to report BarcodeType, unique Id, and OutlineVertices of the Barcode instance which can be used for more sophisticated reporting.