/// <summary>
        /// Successful FindMarker request callback function.
        /// </summary>
        /// <param name="response">FindMarker response.</param>
        public void FindMarkerCallbackOK(WSMarkerResponse response)
        {
            DateTime now    = DateTime.Now.ToUniversalTime();
            Marker   marker = new Marker(response.data.markerId,
                                         response.data.markerDescriptor,
                                         response.data.markerCustomData,
                                         DateTime.Parse(response.data.markerUpdateDate),
                                         now, now,
                                         DateTime.Parse(response.data.publishedFrom),
                                         DateTime.Parse(response.data.publishedTo),
                                         response.data.cacheEnabled,
                                         new MarkerDatabase(response.data.markerDatabase.id,
                                                            response.data.markerDatabase.code,
                                                            response.data.markerDatabase.customData,
                                                            response.data.markerDatabase.cloud,
                                                            now, now),
                                         response.data.arLogoEnabled);

            string appdatapath = RecognitionManager.GetAppDataPath();
            Thread thread      = new Thread(() => SaveLocalMarkerThread(marker, true, true, appdatapath, true));

            thread.Start();
            //SaveLocalMarker(marker, true, true);

            _findMarkerRequestRunning = false;
            Thread thread2 = new Thread(() => _dataProviderListener.FindMarkerCallback(marker));

            thread2.Start();
            //_dataProviderListener.FindMarkerCallback(marker);
        }
        public void LoadLocalDiscoverModelsOnStartUp(string[] discoverIds)
        {
            //save all local discover models
            string srcDiscoverPath = Application.streamingAssetsPath + "/markers3D/";

#if UNITY_EDITOR || UNITY_IOS
            if (!Directory.Exists(Application.streamingAssetsPath) || !Directory.Exists(srcDiscoverPath))
            {
                return;
            }
#endif
            string dstDiscoverPath = RecognitionManager.GetAppDataPath() + "markers3D/";

            if (!Directory.Exists(dstDiscoverPath))
            {
                Directory.CreateDirectory(dstDiscoverPath);
            }
            else
            {
                string[] discover_files = Directory.GetFiles(dstDiscoverPath);
                foreach (string ff in discover_files)
                {
                    File.Delete(ff);
                }
            }
#if UNITY_EDITOR || UNITY_IOS
            foreach (string dicoverId in discoverIds)
            {
                if (dicoverId == "")
                {
                    continue;
                }
                string filename        = Path.GetFileName(dicoverId + ".dat");
                string discoverSrcFile = srcDiscoverPath + filename;
                string discoverDstFile = dstDiscoverPath + filename;
                Debug.Log("Saving discover model " + discoverDstFile);
                File.WriteAllBytes(discoverDstFile, File.ReadAllBytes(discoverSrcFile));
            }
#else
            foreach (string dicoverId in discoverIds)
            {
                if (dicoverId == "")
                {
                    continue;
                }
                string filename = Path.GetFileName(dicoverId + ".dat");
                WWW    reader   = new WWW(srcDiscoverPath + filename);
                while (!reader.isDone)
                {
                }
                if (!String.IsNullOrEmpty(reader.error))
                {
                    continue;
                }
                string discoverDstFile = dstDiscoverPath + filename;
                Debug.Log("Saving discover model " + discoverDstFile);
                File.WriteAllBytes(discoverDstFile, reader.bytes);
            }
#endif
        }
        public static void SaveLocalMarkerFile(string markerId, byte[] markerDescriptor)
        {
            string markerFileName = markerId + ".dat";
            string markersFolder  = RecognitionManager.GetAppDataPath() + "markers";

            string markerFilePath = markersFolder + "/" + markerFileName;

            if (!Directory.Exists(markersFolder))
            {
                Directory.CreateDirectory(markersFolder);
            }

            if (!File.Exists(markerFilePath))
            {
                File.WriteAllBytes(markerFilePath, markerDescriptor);
            }
        }
        protected void SyncLocalMarkers(bool isRecognitionManagerRunning, IRecognitionDataProviderListener recognitionDataProviderListener)
        {
#if UNITY_ANDROID && !UNITY_EDITOR
            AndroidJavaClass  jc           = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            AndroidJavaObject context      = jc.GetStatic <AndroidJavaObject>("currentActivity");
            AndroidJavaObject assetManager = context.Call <AndroidJavaObject>("getAssets");
#endif
            PikkartARCore.CheckUnityLicense(
#if UNITY_ANDROID && !UNITY_EDITOR
                assetManager.GetRawObject()
#elif UNITY_EDITOR_WIN
                new StringBuilder(Application.streamingAssetsPath),
    #if UNITY_5_6_OR_NEWER
                new StringBuilder(PlayerSettings.applicationIdentifier)
    #else
                new StringBuilder(PlayerSettings.bundleIdentifier)
    #endif
#endif
                );

            new RecognitionDataProvider(new CloudRecognitionInfo(databases), recognitionDataProviderListener,
                                        RecognitionManager.GetDeviceId()).SyncLocalMarkers(isRecognitionManagerRunning);
        }
        /// <summary>
        /// Instruct the local recognition service to save a marker.
        /// </summary>
        /// <param name="marker">Marker.</param>
        public void SaveLocalMarker(Marker marker, bool isRecognitionManagerRunning, bool replace, bool sync_after_save)
        {
            if (replace || !File.Exists(RecognitionManager.GetAppDataPath() + "markers/" + marker.markerId + ".dat"))
            {
                string dstMarkerPath = RecognitionManager.GetAppDataPath() + "markers/";
                if (!Directory.Exists(dstMarkerPath))
                {
                    Directory.CreateDirectory(dstMarkerPath);
                }

                if (isRecognitionManagerRunning)
                {
#if UNITY_EDITOR_OSX || (UNITY_IOS && !UNITY_EDITOR_WIN)
                    String sbId         = marker.markerId.ToString();
                    String sbDescriptor = marker.markerDescriptor;
#else
                    StringBuilder sbId         = new StringBuilder(marker.markerId.ToString());
                    StringBuilder sbDescriptor = new StringBuilder(marker.markerDescriptor);
#endif
                    Debug.Log("Loading marker PikkartARCore.SaveLocalMarker " + sbId);
                    PikkartARCore.SaveLocalMarker(sbId, sbDescriptor);
                }
                else
                {
                    SaveLocalMarkerFile(marker.markerId, Convert.FromBase64String(marker.markerDescriptor));
                }

                if (sync_after_save)
                {
                    PikkartARCore.SyncMarkersWithFolder();
                }
            }

            marker.markerDescriptor = null;
            _localRecognitionService.SaveMarker(marker, _localRecognitionService.GetMarker(marker.markerId) != null);
        }
        /// <summary>
        /// Deletes obsolete local data.
        /// </summary>
        /// <param name="cacheMilliseconds">Cache milliseconds.</param>
        /// <param name="isRecognitionManagerRunning">is recognition manager running.</param>
        public void DeleteObsoleteLocalData(bool deleteFilesNotInSqlite, bool isRecognitionManagerRunning)
        {
            List <string> markersFileNames = new List <string>();
            List <Marker> dbMarkers        = GetMarkersList();
            string        markersPath      = RecognitionManager.GetAppDataPath() + "markers";

            if (!Directory.Exists(markersPath))
            {
                Directory.CreateDirectory(markersPath);
            }

            if (dbMarkers.Count > 0)
            {
                foreach (Marker marker in dbMarkers)
                {
                    string   markerId       = marker.markerId;
                    string   markerFileName = markerId + ".dat";
                    DateTime?publishedTo    = marker.publishedTo;

                    markersFileNames.Add(markerFileName);

                    if (publishedTo == null)
                    {
                        continue;
                    }

                    if (publishedTo.Value.CompareTo(DateTime.Now.ToUniversalTime()) < 0)
                    {
                        if (isRecognitionManagerRunning)
                        {
#if UNITY_EDITOR_OSX || (UNITY_IOS && !UNITY_EDITOR_WIN)
                            PikkartARCore.DeleteLocalMarker(markerId, true);
#else
                            PikkartARCore.DeleteLocalMarker(new StringBuilder(markerId), true);
#endif
                        }
                        else
                        {
                            string markerFilePath = markersPath + "/" + markerFileName;
                            if (!Directory.Exists(markersPath))
                            {
                                Directory.CreateDirectory(markersPath);
                            }
                            if (File.Exists(markerFilePath))
                            {
                                File.Delete(markerFilePath);
                            }
                        }
                    }
                }
            }

            if (deleteFilesNotInSqlite)
            {
                var        info      = new DirectoryInfo(markersPath);
                FileInfo[] filesInfo = info.GetFiles();

                foreach (FileInfo fileInfo in filesInfo)
                {
                    if (fileInfo.Name.Contains(".dat") && !markersFileNames.Contains(fileInfo.Name))
                    {
                        fileInfo.Delete();
                    }
                }
            }

            dbu.DeleteObsoleteMarkers();
        }
 /// <summary>
 /// Sets the RecognitionManager reference.
 /// </summary>
 /// <param name="recognitionManager">Recognition manager ref</param>
 public void SetRecognitionManager(RecognitionManager recognitionManager)
 {
     this.recognitionManager = recognitionManager;
     this.recognitionManager.SetPikkartCamera(this);
 }
        /// <summary>
        /// Init this instance. Initialize and start RecongnitionManager, PikkartCamera and NatCamManager.
        /// </summary>
        protected IEnumerator InitRecognition()
        {
            foreach (var device in WebCamTexture.devices)
            {
                Debug.Log("Name: " + device.name);
            }
            yield return(Application.RequestUserAuthorization(UserAuthorization.WebCam));

            if (Application.HasUserAuthorization(UserAuthorization.WebCam))
            {
                Debug.Log("webcam found");
            }
            else
            {
#if UNITY_EDITOR
                if (EditorUtility.DisplayDialog("Camera error!",
                                                "You cannot use our SDK without a camera!\n" +
                                                "Please connect a camera and try again.", "Ok"))
                {
                    EditorApplication.isPlaying = false;
                }
#endif
                Debug.Log("webcam error no webcam");
                yield break;
            }

#if UNITY_EDITOR
            if (!useWebcamOnEditor)
            {
                yield break;
            }
#endif
            Screen.sleepTimeout = SleepTimeout.NeverSleep;

            foreach (CloudMarkerObject cloudMarker in _cloudMarkers)
            {
                cloudMarker.Init();
            }
            foreach (MarkerObject marker in _markerObjects)
            {
                marker.Init();
            }

            _camera = FindObjectOfType <PikkartARCamera>();

            _webcam = FindObjectOfType <WebCamManager>();

            if (_recognitionManager == null)
            {
                _recognitionManager = new RecognitionManager(this);
                _recognitionManager.LoadLocalMarkersOnStartup(true, _markerList, _discoverList);
            }
            else
            {
                PikkartARCore.SyncMarkersWithFolder();
            }

            _camera.SetRecognitionManager(_recognitionManager);

            _recognitionOptions = new RecognitionOptions(
                recognitionStorage,
                recognitionMode,
                new CloudRecognitionInfo(databases));

            if (_webcam.HasWebcam())
            {
                _camera.LaunchWebcam(_webcam);
            }
            else
            {
#if UNITY_EDITOR
                if (EditorUtility.DisplayDialog("Camera error!",
                                                "You cannot use our SDK without a camera!\n" +
                                                "Please connect a camera and try again.", "Ok"))
                {
                    EditorApplication.isPlaying = false;
                }
#endif
                Debug.Log("webcam error no webcam");
            }
#if UNITY_EDITOR
            if (_recognitionOptions.getMode() != RecognitionOptions.RecognitionMode.TAP_TO_SCAN)
            {
                if (FindObjectOfType <ScanButton>() != null)
                {
                    FindObjectOfType <ScanButton>().gameObject.SetActive(false);
                }
            }
#else
            if (FindObjectOfType <ScanButton>() != null)
            {
                FindObjectOfType <ScanButton>().gameObject.SetActive(false);
            }
#endif
            _camera.SetMarkers(ref _markerObjects, ref _cloudMarkers);

            _camera.SetSeeThroughDevice(isSeeThroughDevice, seeThroughFramePrediction);

            if (_recognitionListener != null)
            {
                _recognitionListener.OnPikkartARStartupComplete();
            }
        }
示例#9
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PikkartAR.RecognitionManager"/> class.
        /// </summary>
        /// <param name="width">Frame Width.</param>
        /// <param name="height">Frame Height.</param>
        /// <param name="localRecognitionService">Object that implements the communication interface.</param>
        public RecognitionManager(IMarkerObjectListener markerObjectListener)
        {
            _markerObjectListener = markerObjectListener;
            _recognitionRunning   = false;

#if UNITY_ANDROID && !UNITY_EDITOR
            AndroidJavaClass  jc           = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            AndroidJavaObject context      = jc.GetStatic <AndroidJavaObject>("currentActivity");
            AndroidJavaObject assetManager = context.Call <AndroidJavaObject>("getAssets");
#endif
            _deviceId = RecognitionManager.GetDeviceId();

            // Native Recognition Service Initialization
            if (!_nativeWrapperInitialized)
            {
                int res = PikkartARCore.InitRecognition(
#if UNITY_ANDROID && !UNITY_EDITOR
                    assetManager.GetRawObject(),
#endif
                    _localMarkerNotFoundEventMinIntervalMillisecs,
#if UNITY_EDITOR_OSX || (UNITY_IOS && !UNITY_EDITOR_WIN)
                    GetAppDataPath(),
#else
                    new StringBuilder(GetAppDataPath()),
#endif
#if UNITY_EDITOR_WIN
                    new StringBuilder(Application.streamingAssetsPath),
    #if UNITY_5_6_OR_NEWER
                    new StringBuilder(PlayerSettings.applicationIdentifier),
    #else
                    new StringBuilder(PlayerSettings.bundleIdentifier),
    #endif
#elif UNITY_EDITOR_OSX
                    Application.streamingAssetsPath,
    #if UNITY_5_6_OR_NEWER
                    PlayerSettings.applicationIdentifier,
    #else
                    PlayerSettings.bundleIdentifier,
    #endif
#endif
                    Constants.PROCESS_WIDTH,
                    Constants.PROCESS_HEIGHT,
                    ScreenUtilities.GetScreenInches(),
                    Constants.CAMERA_REQUESTED_WIDTH,
                    Constants.CAMERA_REQUESTED_HEIGHT,
                    false);

                if (res > 0)
                {
                    _licenceChecked = true;
                }
                else
                {
                    _licenceChecked = false;
                }
                if (res == 0)
                {
#if (UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR
                    if (MonoBehaviour.FindObjectOfType <LicenseMessage>() != null)
                    {
                        MonoBehaviour.FindObjectOfType <LicenseMessage>().gameObject.SetActive(true);
                    }
                    if (MonoBehaviour.FindObjectOfType <TrialMessage>() != null)
                    {
                        MonoBehaviour.FindObjectOfType <TrialMessage>().gameObject.SetActive(false);
                    }
#elif UNITY_EDITOR
                    if (MonoBehaviour.FindObjectOfType <TrialMessage>() != null)
                    {
                        MonoBehaviour.FindObjectOfType <TrialMessage>().gameObject.SetActive(false);
                    }
                    if (MonoBehaviour.FindObjectOfType <LicenseMessage>() != null)
                    {
                        MonoBehaviour.FindObjectOfType <LicenseMessage>().gameObject.SetActive(false);
                    }
                    if (EditorUtility.DisplayDialog("Licence error!",
                                                    "You cannot use our sdk without a valid licence UNLESS you are in a trial!\n" +
                                                    "Please register to developer.pikkart.com to obtain your own licence or set your application identifier to 'com.pikkart.trial' to evaluate our SDK.", "Ok"))
                    {
                        EditorApplication.isPlaying = false;
                    }
#endif
                }
                else if (res == 1)
                {
#if (UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR
                    if (MonoBehaviour.FindObjectOfType <TrialMessage>())
                    {
                        MonoBehaviour.FindObjectOfType <TrialMessage>().gameObject.SetActive(true);
                    }
                    if (MonoBehaviour.FindObjectOfType <LicenseMessage>())
                    {
                        MonoBehaviour.FindObjectOfType <LicenseMessage>().gameObject.SetActive(false);
                    }
#elif UNITY_EDITOR
                    if (MonoBehaviour.FindObjectOfType <TrialMessage>())
                    {
                        MonoBehaviour.FindObjectOfType <TrialMessage>().gameObject.SetActive(false);
                    }
                    if (MonoBehaviour.FindObjectOfType <LicenseMessage>())
                    {
                        MonoBehaviour.FindObjectOfType <LicenseMessage>().gameObject.SetActive(false);
                    }
                    if (EditorUtility.DisplayDialog("No valid license",
                                                    "No valid license or running as com.pikkart.trial, press Ok to run in trial mode", "Ok"))
                    {
                        EditorApplication.isPlaying = true;
                    }
#endif
                }
                else
                {
                    if (MonoBehaviour.FindObjectOfType <TrialMessage>())
                    {
                        MonoBehaviour.FindObjectOfType <TrialMessage>().gameObject.SetActive(false);
                    }
                    if (MonoBehaviour.FindObjectOfType <LicenseMessage>())
                    {
                        MonoBehaviour.FindObjectOfType <LicenseMessage>().gameObject.SetActive(false);
                    }
                }

                PikkartARCore.SetProjectionMatrix(Constants.CAM_LARGE_FOV, Constants.CAM_SMALL_FOV);

                _nativeWrapperInitialized = true;
                //_nativeArray = Marshal.AllocHGlobal(Constants.BASE_RES_WIDTH * Constants.BASE_RES_HEIGHT * 3);
            }
            else
            {
                if (MonoBehaviour.FindObjectOfType <TrialMessage>())
                {
                    MonoBehaviour.FindObjectOfType <TrialMessage>().gameObject.SetActive(false);
                }
                if (MonoBehaviour.FindObjectOfType <LicenseMessage>())
                {
                    MonoBehaviour.FindObjectOfType <LicenseMessage>().gameObject.SetActive(false);
                }
            }


            _dataProvider = new RecognitionDataProvider(null, this, _deviceId);
        }
        public void LoadLocalMarkersOnStartup(bool isRecognitionManagerRunning, string[] markerIds, string[] discoverIds)
        {
            LoadLocalDiscoverModelsOnStartUp(discoverIds);

            if (markerIds.Length < 1)
            {
                return;
            }

            string srcMarkerPath = Application.streamingAssetsPath + "/markers/";

#if UNITY_EDITOR || UNITY_IOS
            if (!Directory.Exists(Application.streamingAssetsPath) || !Directory.Exists(srcMarkerPath))
            {
                return;
            }
#endif
            string dstMarkerPath = RecognitionManager.GetAppDataPath() + "markers/";

            if (!Directory.Exists(dstMarkerPath))
            {
                Directory.CreateDirectory(dstMarkerPath);
            }
            else
            {
                string[] files = Directory.GetFiles(dstMarkerPath);
                foreach (string ff in files)
                {
                    File.Delete(ff);
                }
            }

            foreach (string markerId in markerIds)
            {
                Debug.Log("Loading marker " + markerId);
                if (markerId == "")
                {
                    continue;
                }

                string markerString = null;

#if UNITY_EDITOR || UNITY_IOS
                markerString = File.ReadAllText(srcMarkerPath + markerId + ".dat");
#else
                WWW reader = new WWW(srcMarkerPath + markerId + ".dat");
                while (!reader.isDone)
                {
                }
                if (!String.IsNullOrEmpty(reader.error))
                {
                    continue;
                }
                markerString = reader.text;
#endif
                if (markerString.Length > 0)
                {
                    Debug.Log("Loading marker save " + markerId);
                    WSMarkerResponse.WSMarker markerData = JsonUtilities.ToObject <WSMarkerResponse.WSMarker>(markerString);
                    DateTime now    = DateTime.Now.ToUniversalTime();
                    Marker   marker = new Marker(markerData.markerId,
                                                 markerData.markerDescriptor, markerData.markerCustomData,
                                                 DateTime.Parse(markerData.markerUpdateDate), now, now,
                                                 markerData.publishedFrom != null ? (DateTime?)DateTime.Parse(markerData.publishedFrom) : null,
                                                 markerData.publishedTo != null ? (DateTime?)DateTime.Parse(markerData.publishedTo) : null,
                                                 markerData.cacheEnabled,
                                                 new MarkerDatabase(markerData.markerDatabase.id, markerData.markerDatabase.code,
                                                                    markerData.markerDatabase.customData, markerData.markerDatabase.cloud, now, now),
                                                 markerData.arLogoEnabled);

                    SaveLocalMarker(marker, isRecognitionManagerRunning, true, false);
                }
            }
            PikkartARCore.SyncMarkersWithFolder();
        }