internal static CloudRecognizerCache GetInstance()
 {
     if (instance == null)
     {
         instance = new CloudRecognizerCache();
     }
     return(instance);
 }
Exemple #2
0
        private void Update()
        {
            if (cloudState == CloudState.CLOUDSTATE_CONNECT)
            {
                cloudState = CloudState.CLOUDSTATE_CONNECTING;

                CloudRecognitionAPIController.Instance.Recognize(this.secretId, this.secretKey, featureBase64, (recognitionResult) =>
                {
                    if (this.restart == true && recognitionResult == null)
                    {
                        this.cloudState = CloudState.CLOUDSTATE_FEATURE_COLLECT_READY;

                        return;
                    }

                    CloudRecognitionData cloudRecognitionData = null;
                    try
                    {
                        cloudRecognitionData = JsonReader.Deserialize <CloudRecognitionData>(recognitionResult);
                    }
                    catch (Exception ex)
                    {
                        Debug.Log("error");
                        this.cloudState = CloudState.CLOUDSTATE_FEATURE_COLLECT_READY;
                        return;
                    }

                    if (recognitionResult != null && cloudRecognitionData.ImgId != "" && cloudRecognitionData.ImgGSUrl != "")
                    {
                        Debug.Log(recognitionResult);
                        string fileName = Path.GetFileName(cloudRecognitionData.ImgGSUrl);
                        CloudRecognitionAPIController.Instance.DownloadCloudDataAndSave(cloudRecognitionData.ImgGSUrl, fileName, (localPath) =>
                        {
                            if (this.restart == true)
                            {
                                this.cloudState = CloudState.CLOUDSTATE_FEATURE_COLLECT_READY;
                                return;
                            }

                            if (localPath == null)
                            {
                                this.cloudState = CloudState.CLOUDSTATE_FEATURE_COLLECT_READY;
                                return;
                            }

                            TrackingState trackingState   = TrackerManager.GetInstance().GetTrackingState();
                            TrackingResult trackingResult = trackingState.GetTrackingResult();
                            if (trackingResult.GetCount() > 0)
                            {
                                cloudState = CloudState.CLOUDSTATE_TRACKING;
                            }
                            else
                            {
                                string mapDirectory  = Path.GetDirectoryName(localPath);
                                string mapFilePath   = mapDirectory + "/" + Path.GetFileNameWithoutExtension(localPath) + ".2dmap";
                                string imageFilePath = localPath;

                                string customToBase64 = "";
                                if (cloudRecognitionData.Custom != null)
                                {
                                    byte[] customToByteArray = Encoding.UTF8.GetBytes(cloudRecognitionData.Custom);
                                    customToBase64           = Convert.ToBase64String(customToByteArray);
                                }

                                string command = "";
                                if (File.Exists(mapFilePath))
                                {
                                    command = "{\"cloud\":\"add_image\",\"cloud_2dmap_path\":\"" + mapFilePath + "\",\"image_width\":" + cloudRecognitionData.RealWidth + ",\"cloud_name\":\"" + cloudRecognitionData.Name + "\",\"cloud_meta\":\"" + customToBase64 + "\"}";
                                }
                                else
                                {
                                    command = "{\"cloud\":\"add_image\",\"cloud_image_path\":\"" + imageFilePath + "\",\"output_path\":\"" + mapFilePath + "\",\"image_width\":" + cloudRecognitionData.RealWidth + ",\"cloud_name\":\"" + cloudRecognitionData.Name + "\",\"cloud_meta\":\"" + customToBase64 + "\"}";
                                }

                                if (this.restart == true || command == "")
                                {
                                    this.cloudState = CloudState.CLOUDSTATE_FEATURE_COLLECT_READY;
                                    return;
                                }

                                TrackerManager.GetInstance().StartTracker(TrackerManager.TRACKER_TYPE_IMAGE);
                                CloudRecognizerCache.GetInstance().ADD(fileName, command);
                                CloudRecognizerCache.GetInstance().LOAD();

                                cloudState = CloudState.CLOUDSTATE_TRACKING;
                            }
                        });
                    }
                    else
                    {
                        this.cloudState = CloudState.CLOUDSTATE_FEATURE_COLLECT_READY;
                    }
                });
            }
        }