恢复应用程序并扫描数据矩阵时,BarcodeReader Decode返回null。

时间:2019-02-15 11:15:05

标签: c# unity3d zxing vuforia gs1-datamatrix

我一直在Unity引擎中使用Vuforia和Zxing开发AR应用程序。

如果仅首次启动并扫描应用程序,则Zxing BarcodeReader Decode方法将返回条形码的文本。但是,当应用程序暂停并恢复或转移到另一个应用程序(浏览器)并移回该应用程序时,解码将不会返回值(空)。

我的代码如下,我已经在 OnApplicationPause 方法中重新创建了一个新的BarcodeReader对象。但这没用。

[AddComponentMenu("System/ArCamCapture")]
public class ArCamCapture : MonoBehaviour
{

    private readonly Image.PIXEL_FORMAT m_PixelFormat = Image.PIXEL_FORMAT.RGB888;
    private bool _CameraInitialized;

    private BarcodeReader _BarCodeReader;


    void Start()
    {
        try
        {
            _BarCodeReader = new BarcodeReader();

            StartCoroutine(InitializeCamera());                

            VuforiaARController.Instance.RegisterTrackablesUpdatedCallback(OnTrackablesUpdated);
            VuforiaARController.Instance.RegisterVuforiaStartedCallback(OnVuforiaStarted);
        }
        catch (Exception ex)
        {
            SSTools.ShowMessage("Error : " + ex.Message, SSTools.Position.bottom, SSTools.Time.twoSecond);
        }

    }

    /// <summary>
    /// Initialize AR camera to detect barcode
    /// </summary>
    /// <returns></returns>
    private IEnumerator InitializeCamera()
    {
        // Waiting a little seem to avoid the Vuforia's crashes.
        yield return new WaitForSeconds(1.25f);

        CameraDevice.Instance.SetFrameFormat(Image.PIXEL_FORMAT.RGB888, true);
        // Force autofocus.
        var isAutoFocus = CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
        if (!isAutoFocus)
        {
            CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_NORMAL);
        }
        _CameraInitialized = true;
        _BarCodeReader.Options.PossibleFormats.Add(BarcodeFormat.DATA_MATRIX);           
    }

    /// <summary>
    /// AR camera tracking change
    /// </summary>
    public void OnTrackablesUpdated()
    {
        try
        {
            KeyInputHanlde();
            if (Application.internetReachability == NetworkReachability.NotReachable)
            {
                SSTools.ShowMessage("check your internet connection", SSTools.Position.bottom, SSTools.Time.threeSecond);
                return;
            }

            if (CameraDevice.Instance.SetFrameFormat(m_PixelFormat, true) && _CameraInitialized)
            {
                Image image = CameraDevice.Instance.GetCameraImage(m_PixelFormat);

                if (image == null)
                {
                    SSTools.ShowMessage("image is not available yet", SSTools.Position.bottom, SSTools.Time.twoSecond);
                }

                else
                {
                    var data = _BarCodeReader.Decode(image.Pixels, image.BufferWidth, image.BufferHeight, RGBLuminanceSource.BitmapFormat.RGB24);
                    if (data != null)
                    {
                        //user code
                    }
                    else
                    {
                            SSTools.ShowMessage("Scanning ...", SSTools.Position.bottom, SSTools.Time.twoSecond);                            

                    }
                }

            }
        }
        catch (Exception ex)
        {
            SSTools.ShowMessage("Error : " + ex.Message, SSTools.Position.bottom, SSTools.Time.twoSecond);
            _CameraInitialized = true;
        }

    }

    /// <summary>
    /// When Vuforia camera is on
    /// </summary>
    private void OnVuforiaStarted()
    {
        CameraDevice.Instance.SetFocusMode(
            CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
    }

    /// <summary>
    /// When android application is OnResume and OnPause state
    /// </summary>
    /// <param name="pauseStatus"></param>
    private void OnApplicationPause(bool isPaused)
    {
        if (!isPaused)
        {                
            SSTools.ShowMessage($"is paused: {isPaused}", SSTools.Position.bottom, SSTools.Time.oneSecond);
            CameraDevice.Instance.SetFocusMode(
            CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
            CameraDevice.Instance.SetFrameFormat(Image.PIXEL_FORMAT.RGB888, true);
            _BarCodeReader = new BarcodeReader();
            _BarCodeReader.Options.PossibleFormats.Add(BarcodeFormat.DATA_MATRIX);                
        }

    }

}

我在这里错过了什么配置? 谢谢

0 个答案:

没有答案