For a current project i needed a qr-code scanner that runs in continuous mode in Unity3D.For decoding barcodes, the zxing.net library offers a possibility to integrate quite easily in Unity3D. That beeing done we need the image stream to be processed.
After searching quite a long time i came with two possible solutions.
Unity provides the possibility to get a camera and show it’s input on a webcamtexture. You can find more documentation about it here.
- Directly integrated in Unity
- The webcam can be attached to a unity-UI element, which allows us to show the webcam stream where we want it in the size that we want.
- No focus-options for the camera. The camera will have no autofocus whatsoever. (Perhaps this could be added with a native plugin?)
- Ratio, Resolution and all the settings have to be done by yourself.
Vuforia is an augmented reality library which does all the heavy lifting stuff for tracking markers, images and 3d-objects. It’s quite a bit of overkill to use it, since we only need the camera-stream, but it offers some possibilites the webCamTexture does not. It’s also a lot faster…
You can get it after signing up here.
- There is already an explanation for how to integrate Vuforia, on which i based my own solution:
- Vuforia does all the platform stuff and you only have to handle the Image Stream, which makes it quite fast
- A Texture is provided along with the ar-camera that shows the videostream
- Continuous Autofocus or repeated Autofocus possible
- Vuforia is not free (but i can live with the watermarked version for now
- Another 3rd Party plugin…
I decided to go for the Vuforia option because
- I needed fullscreen camera feed anyways
- Faster and easier to implemented
- Watermark doesn’t matter in my case
So building on the solution golB s’ydairA the steps to get all integrated is:
- Get the vuforia unity package (you can download it after signing up) and import it into your Unity3D package.
- Download zxing.net for Unity3D or get the unitypackage from golB s’ydairA blog post (you will need to do some adaptions here)
- Drag the ARCamera prefab from the vuforia folder into the project and enter the license key (you can get one from the vuforia portal)
- Use the CameraImageAccess.cs file you can find attached here and drag it on the ARCamera
- Optional: Create a UI-Text element and assign it the CameraImageAccess ScannedText field to show a scanned text when running the app
On to explain the ImageAccess file, which is essentiall quite simple:
- The above code scans for multiple barcodes, if you only need one you can just use a QRCodeReader directly, which makes processing a bit faster
- I’ve only tested the code on my android device, but theoretically it should work on iphone and windows too, because vuforia is cross-platform compatible and zxing.net too.
- The vuforia camera with the current (2015-12-18) unitypackage will not work under Unity3D64bit in the editor, because it was compiled for the 32bit version. To work around that i had to install the 32bit version of Unity3D. Perhaps they add a 64bit version in the future…