Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by ina · Jun 22, 2012 at 09:28 AM · cameracolorvuforiabytearrayzxing

Getting Vuforia to work with zxing

I am trying to use the camera input from Vuforia to read QR codes using zxing. It seems there are two main problems:

1) How do you get a non-null bytestream from Vuforia? (I've tried using WaitForEndOfFrame and LateUpdate -- still get null bytes)

2) Once I get this byte stream, do I have to convert it to get zxing to be able to parse it? (if so, does this work: here)


Edit: Note that the zxing c# port via @WhyDoIDoIt and the VuforiaScanner script works if you comment out all the Loom references. The Loom of @WhyDoIDoIt no longer works. Unity 4.6.1, Vuforia 3.0.9

Comment
Add comment · Show 9
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Russel · Jun 29, 2012 at 09:04 AM 0
Share

Have you already a solution? Have you done it already with Unity Android or iOS and Zxing without Vuforia?

avatar image flamy · Jun 29, 2012 at 09:08 AM 1
Share

i hope you should be asking this in vuforia forums, since not much people might have worked with it here...

avatar image whydoidoit · Jul 14, 2012 at 04:47 PM 1
Share

I've got this working now - did you get there?

avatar image Russel · Jul 15, 2012 at 09:29 AM 0
Share

Can I get your solution? Oliver-Ebert@web.de Thank you. :)

avatar image whydoidoit · Jul 18, 2012 at 07:26 AM 1
Share

Will post the solution on my blog later: my mods to ZXing and multithreaded decoder. Performance is excellent, amazingly better than the Antares decoder I was using before and frame rate remains good.

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by whydoidoit · Jul 18, 2012 at 04:39 PM

So I've successfully integrated Vuforia/ZXing and Unity to do barcode and QR code scanning. I've created a version of ZXing that is Unity compatible and produced an example decoder that are available from here. The answer was to use grayscale images and decode them on a second thread.

Comment
Add comment · Show 13 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image elixir-bash · Oct 10, 2012 at 10:10 AM 0
Share

hey mike, that sounds great!! Im planning to make use of QR scanner in my unity game. Can you give me a heads up on how this is possible .Is there a tutorial for this..Thank you

avatar image whydoidoit · Oct 10, 2012 at 04:35 PM 0
Share

Sure - what you need to do is download the package from the link. Attach the VuforiaScanner script to something (like the camera). Set the target to a GameObject which has a script that should get the QR code or Barcode. Write a script on with a method:

  function Scanned(data : String)
  {
       //Your code here
  }

Attach this script to the target game object.

Your other choice is to add an event handler to the static VuforiaScanner.ScannedQRCode event which will be fired every time a QR code is recognised.

avatar image nafizace · Jan 15, 2014 at 02:45 PM 1
Share

its showing this error when detecting the qrcode

CompareBaseObjectsInternal can only be called from the main thread.Constructors and field initializers will be executed from the loading thread when loading a scene.Don't use this function in the constructor or field initializers, ins$$anonymous$$d move initialization code to the Awake or Start function

and also no output in my guitext....i m using unity 4.3 and vuforia 2.8...

avatar image ina · Feb 08, 2015 at 10:15 AM 1
Share

try making sure everything inherits from $$anonymous$$onoBehaviour

avatar image ina · Feb 08, 2015 at 10:32 AM 1
Share

Or just don't use the Loom class

Show more comments
avatar image
0

Answer by zORg_alex · Aug 24, 2015 at 02:37 PM

Here's what I've found out:

Grab ZXing plugin here

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[i].r = r[i].g = r[i].b = a.Pixels[i];
     }
     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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Shaukeen · Jun 22, 2016 at 04:00 PM

We also have implemented the QR detection functionality using ZXing.dll in Unity 5.3.4f1 with Vuforia Unity SDK 5.5.9. We have a QR detection script on GameObject which remains active throughout the app and using below mentioned (QRScanner.cs) code ( as mentioned on http://stackoverflow.com/questions/30546548/unity-zxing-qr-code-scanner-integration ).

We are also using Vuforia for image detection (50 image targets) in same scene where QR detection is expected. The Vuforia plugin is getting enabled / disabled multiple times as per our requirement. Both the image and QR detection is working perfectly for us on Android and iOS devices until app is in focus. Whenever VuforiaBehaviour gets disabled and enabled, QR detection stops working thereafter ( as mentioned by @zORg_alex ). QRScanner script always receives null data after app is resumed or AR camera is reloaded. We have tried keeping our QR detection script on AR camera prefab and also tried

  qcarBehaviour.RegisterTrackablesUpdatedCallback(OnTrackablesUpdated);
  qcarBehaviour.RegisterQCARStartedCallback(OnTrackablesUpdated);

callbacks every time AR camera starts but with no success. The QR detection stops working completely after pausing Vuforia plugin for any reason.

Does anybody have any idea how to fix this issue ?

QRScanner.cs

 using UnityEngine;
 using System;
 using System.Collections;
 using Vuforia;
 using System.Threading;
 using ZXing;
 using ZXing.QrCode;
 using ZXing.Common;
 
 /*        /////////////////    QR detection does not work in editor    ////////////////    */
 
 [AddComponentMenu("System/QRScanner")]
 public class QRScanner : MonoBehaviour
 {    
     private bool cameraInitialized;
     private BarcodeReader barCodeReader;
     public AppManager camScript;
 
     void Start()
     {        
         barCodeReader = new BarcodeReader();
         StartCoroutine(InitializeCamera());
     }
 
     private IEnumerator InitializeCamera()
     {
         // Waiting a little seem to avoid the Vuforia's crashes.
         yield return new WaitForSeconds(3f);
 
         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);
                     Application.OpenURL (data.Text);      // our function to call and pass url as text
                     data = null;        // clear data
                 }
                 else
                 {
                     Debug.Log("No QR code detected !");
                 }
             }
             catch (Exception e)
             {
                 Debug.LogError(e.Message);
             }
         }
     }
 }




Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why does camera not register color until objects are close up? 0 Answers

rgb value detail 0 Answers

How to Switch/Change Front and back Vuforia AR Camera using a button? 0 Answers

Material doesn't have a color property '_Color' 4 Answers

Is there a way to limit the range of colors that can render in the scene? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges