Ejemplo n.º 1
0
        /// <summary>
        /// 文字识别
        /// </summary>
        /// <param name="recognitionMode">识别方式</param>
        /// <returns>文字识别的协程</returns>
        public static Coroutine Recognition(CharacterRecognitionModeBase recognitionMode)
        {
            if (recognitionMode == null || recognitionMode.ImageSource == null)
            {
                GlobalTools.LogError("文字识别失败:识别方式和识别图像源均不能为空!");
                return(null);
            }

            return(Main.Current.StartCoroutine(RecognitionCoroutine(recognitionMode)));
        }
Ejemplo n.º 2
0
        private static IEnumerator RecognitionCoroutine(CharacterRecognitionModeBase recognitionMode)
        {
            string url = string.Format("{0}?access_token={1}", recognitionMode.API, TOKEN);

            WWWForm form = new WWWForm();

            form.AddField("image", recognitionMode.GetImageSourceByBase64());
            form.AddField("recognize_granularity", recognitionMode.Granularity == CharacterRecognitionModeBase.RecognizeGranularity.Big ? "big" : "small");
            form.AddField("language_type", recognitionMode.Language.ToString());
            form.AddField("detect_direction", recognitionMode.IsDetectDirection ? "true" : "false");
            form.AddField("detect_language", recognitionMode.IsDetectLanguage ? "true" : "false");

            using (UnityWebRequest request = UnityWebRequest.Post(url, form))
            {
                request.SetRequestHeader("Content-Type", recognitionMode.ContentType);
                yield return(request.SendWebRequest());

                if (!request.isNetworkError && !request.isHttpError)
                {
                    JsonData jsonData = GlobalTools.StringToJson(request.downloadHandler.text);
                    if (jsonData.Keys.Contains("error_code"))
                    {
                        GlobalTools.LogError("文字识别失败:" + jsonData["error_code"].ToString() + " " + jsonData["error_msg"].ToString());

                        recognitionMode.FailHandler?.Invoke();
                    }
                    else
                    {
                        recognitionMode.SuccessHandler?.Invoke(new OCRResponse(jsonData));
                    }
                }
                else
                {
                    GlobalTools.LogError("文字识别失败:" + request.responseCode + " " + request.error);

                    recognitionMode.FailHandler?.Invoke();
                }
            }
        }