Log in or register to post comments

Camera very laggy

April 30, 2018 - 2:11am #1

First of all i have combined vuforia with ZXing QR library.



What i want to achieve:

Before showing the object I want to get the DATA from the QR code.

 

Problem:

Camera is laggy even after QR scan

 

This is my script: (Added it as attachment aswell)

using ZXing;
using UnityEngine;
using Vuforia;
using System;
using UnityEngine.UI;

public class Personizer : MonoBehaviour, ITrackableEventHandler
{

#if UNITY_EDITOR
    private Vuforia.Image.PIXEL_FORMAT mPixelFormat = Vuforia.Image.PIXEL_FORMAT.GRAYSCALE;
#elif UNITY_ANDROID
   private Vuforia.Image.PIXEL_FORMAT mPixelFormat =  Vuforia.Image.PIXEL_FORMAT.RGB888;
#elif UNITY_IOS
    private Vuforia.Image.PIXEL_FORMAT mPixelFormat =  Vuforia.Image.PIXEL_FORMAT.RGB888;
#endif

    private bool mFormatRegistered = false;

    private bool scannedQR;
    private bool canSeeTarget;
    private string qrData;

    public Text debugText;
    public TrackableBehaviour mTrackableBehaviour;

    void Start () {
        Application.targetFrameRate = 60;

        scannedQR = false;
        canSeeTarget = false;

        VuforiaARController.Instance.RegisterTrackablesUpdatedCallback(OnTrackablesUpdated);
        VuforiaARController.Instance.RegisterOnPauseCallback(OnPause);
        VuforiaARController.Instance.RegisterVuforiaStartedCallback(OnVuforiaStarted);

        var isAutoFocus = CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
        if (!isAutoFocus)
        {
            CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_NORMAL);
        }
    }

    void OnVuforiaStarted()
    {
        StartSearchingQR();

        mTrackableBehaviour.RegisterTrackableEventHandler(this);
    }

    void StartSearchingQR()
    {
        RegisterFormat();
    }

    private void OnTrackablesUpdated()
    {
        if (!scannedQR)
        {
            if (mFormatRegistered)
            {
                Vuforia.Image image = CameraDevice.Instance.GetCameraImage(mPixelFormat);

                if (image != null)
                {
                    try
                    {
                        IBarcodeReader barcodeReader = new BarcodeReader();
                        // decode the current frame
                        var result = barcodeReader.Decode(image.Pixels, image.BufferWidth, image.BufferHeight, RGBLuminanceSource.BitmapFormat.RGB24);

                        if (result != null)
                        {
                            scannedQR = true;
                            UnregisterFormat();
                            qrData = result.Text;
                            debugText.text = result.Text;

                            if (canSeeTarget)
                                mTrackableBehaviour.gameObject.SetActive(true);
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.LogWarning(ex.Message);
                    }
                }
            }
        }
    }

    public void OnTrackableStateChanged(TrackableBehaviour.Status previousStatus, TrackableBehaviour.Status newStatus)
    {
        if (newStatus == TrackableBehaviour.Status.DETECTED ||
        newStatus == TrackableBehaviour.Status.TRACKED ||
        newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
        {
            OnTrackingFound();
        }
        else if (previousStatus == TrackableBehaviour.Status.TRACKED &&
                 newStatus == TrackableBehaviour.Status.NOT_FOUND)
        {
            OnTrackingLost();
        }
        else
        {
            OnTrackingLost();
        }
    }

    private void OnTrackingFound()
    {
        //PlayAnimations();
        canSeeTarget = true;
        if (!scannedQR)
        {
            mTrackableBehaviour.gameObject.SetActive(false);
        }
    }

    private void OnTrackingLost()
    {
        canSeeTarget = false;
        //StopAnimations();
    }

    #region Vuforia
    void OnPause(bool paused)
    {
        if (!scannedQR)
        {
            if (paused)
            {
                UnregisterFormat();
            }
            else
            {
                RegisterFormat();
            }
        }
    }

    void RegisterFormat()
    {
        if (CameraDevice.Instance.SetFrameFormat(mPixelFormat, true))
        {
            mFormatRegistered = true;
        }
        else
        {
            mFormatRegistered = false;
        }
    }

    void UnregisterFormat()
    {
        CameraDevice.Instance.SetFrameFormat(mPixelFormat, false);
        mFormatRegistered = false;
    }
    #endregion
}

 

But my camera is a bit laggy now?

AttachmentSize
Plain text icon Personizer.cs4.42 KB

Camera very laggy

April 30, 2018 - 6:23am #4

The weird think is if i press the home button and go back to the app again the problem is solved.

Camera very laggy

April 30, 2018 - 6:02am #3

I do unregister after the QR scan is succesfull that's why it is weird that the camera stays slow after a succesfull scan

Camera very laggy

April 30, 2018 - 6:00am #2

You're passing around of a ton of image data per second.  You gotta be smart with the way you handle that data.  Some things to try:

Unless your qr code is in color and needs it, change to grayscale on all platforms.

Reduce to 30 frames per second.

Resize the byte array image smaller before passing to your qr reader.  

Change to speed mode on your ar camera,  i think on some devices this will reduce the size of the image you get from the camera.



I did not look too closely at the code to see if you're turning off QR reading after scan correctly.  Consider unregistering trackable update callback after it's successful.

 

Log in or register to post comments