コード例 #1
0
        /// <summary>
        /// 通用文字识别
        /// </summary>
        /// <param name="path"></param>
        /// <param name="img"></param>
        /// <returns>解析后的数据</returns>
        public static string GeneralBasic(Image img, string appKey = null, string appSecret = null)
        {
            string ak      = appKey ?? YoudaoKey.AppKey;
            string secret  = appSecret ?? YoudaoKey.AppSecret;
            string url     = "https://openapi.youdao.com/ocrapi";
            string base64  = WebExt.ImageToBase64(img);
            string salt    = DateTime.Now.Millisecond.ToString();
            string curTime = WebExt.GetTimeSpan();
            string sign    = ComputeHash(ak + Truncate(base64) + salt + curTime + secret);
            string str     = string.Format($"img={HttpUtility.UrlEncode(base64)}" +
                                           $"&langType=auto&detectType=10012&imageType=1&appKey={ak}" +
                                           $"&salt={salt}&sign={sign}&docType=json&signType=v3&curtime={curTime}");
            string result = WebExt.Request(url, null, str);
            // 解析json数据
            JavaScriptSerializer js   = new JavaScriptSerializer();
            string        json_result = GetDictResult(result);
            JsonOCR       list        = js.Deserialize <JsonOCR>(json_result);
            StringBuilder sb          = new StringBuilder();

            foreach (var region in list.regions)
            {
                foreach (var line in region.lines)
                {
                    sb.Append(line.text + Environment.NewLine);
                }
            }
            // 删除最后一个换行符
            return(sb.ToString().TrimEnd('\r', '\n'));
        }
コード例 #2
0
        public static string Translate(string text, string from, string to, out string speakUrl)
        {
            speakUrl = null;
            string url     = "https://openapi.youdao.com/api";
            string salt    = DateTime.Now.Millisecond.ToString();
            string curTime = WebExt.GetTimeSpan();
            string sign    = ComputeHash(YoudaoKey.AppKey + Truncate(text) + salt + curTime + YoudaoKey.AppSecret);
            string str     = string.Format($"q={HttpUtility.UrlEncode(text)}" +
                                           $"&from={GetTranLang(from)}&to={GetTranLang(to)}&appKey={YoudaoKey.AppKey}" +
                                           $"&salt={salt}&sign={sign}&signType=v3&curtime={curTime}");
            string result = WebExt.Request(url, null, str);
            // 解析json数据
            JavaScriptSerializer js   = new JavaScriptSerializer();
            YoudaoTranslate      list = js.Deserialize <YoudaoTranslate>(result);

            if (list.errorCode != "0")
            {
                ErrorTip(list.errorCode,
                         "https://ai.youdao.com/DOCSIRMA/html/%E8%87%AA%E7%84%B6%E8%AF%AD%E8%A8%80%E7%BF%BB%E8%AF%91/API%E6%96%87%E6%A1%A3/%E6%96%87%E6%9C%AC%E7%BF%BB%E8%AF%91%E6%9C%8D%E5%8A%A1/%E6%96%87%E6%9C%AC%E7%BF%BB%E8%AF%91%E6%9C%8D%E5%8A%A1-API%E6%96%87%E6%A1%A3.html");
                return("");
            }
            speakUrl = list.tSpeakUrl;
            // 接收序列化后的数据
            return(string.Join("", list.translation));
        }
コード例 #3
0
        /// <summary>
        /// 表格文字识别
        /// </summary>
        /// <param name="path"></param>
        /// <param name="img"></param>
        /// <returns></returns>
        public static string FormOcrRequest(string downloadDir, Image img)
        {
            string url     = "https://openapi.youdao.com/ocr_table";
            string base64  = WebExt.ImageToBase64(img);
            string salt    = DateTime.Now.Millisecond.ToString();
            string curTime = WebExt.GetTimeSpan();
            string sign    = ComputeHash(YoudaoKey.AppKey + Truncate(base64) + salt + curTime + YoudaoKey.AppSecret);
            string str     = string.Format($"q={HttpUtility.UrlEncode(base64)}" +
                                           $"&type=1&appKey={YoudaoKey.AppKey}" +
                                           $"&salt={salt}&sign={sign}&docType=excel&signType=v3&curtime={curTime}");
            string result = WebExt.Request(url, null, str);

            // 解析json数据
            JavaScriptSerializer js = new JavaScriptSerializer();
            string   json_result    = GetDictResult(result);
            JsonForm list           = js.Deserialize <JsonForm>(json_result);

            if (list.tables.Count == 0)
            {
                throw new Exception("识别失败!");
            }

            // 将接收到的base64数据写入到xlsx文件
            if (downloadDir == null)
            {
                return(string.Format("识别成功。\r\n返回的Base64编码:\r\n{0}", string.Join("", list.tables.ToArray())));
            }

            string downloadPath = new SavePath().FormPath + DateTime.Now.ToString("yyyy-MM-dd_HH_mm_ss") + ".xlsx";

            if (!Directory.Exists(Path.GetDirectoryName(downloadPath)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(downloadPath));
            }
            using (FileStream fs = new FileStream(downloadPath, FileMode.Create))
            {
                foreach (var cont in list.tables)
                {
                    byte[] contents = Convert.FromBase64String(cont);
                    fs.Write(contents, 0, contents.Length);
                }
            }
            return(string.Format("识别成功,已下载到当前程序目录。\r\n返回的Base64编码:\r\n{0}", string.Join("", list.tables.ToArray())));
        }
コード例 #4
0
        /// <summary>
        /// 身份证识别
        /// </summary>
        /// <param name="path"></param>
        /// <param name="img"></param>
        /// <returns></returns>
        public static string Idcard(Image img)
        {
            string url     = "https://openapi.youdao.com/ocr_structure";
            string base64  = WebExt.ImageToBase64(img);
            string salt    = DateTime.Now.Millisecond.ToString();
            string curTime = WebExt.GetTimeSpan();
            string sign    = ComputeHash(YoudaoKey.AppKey + Truncate(base64) + salt + curTime + YoudaoKey.AppSecret);
            string str     = string.Format($"q={HttpUtility.UrlEncode(base64)}" +
                                           $"&structureType=idcard&appKey={YoudaoKey.AppKey}" +
                                           $"&salt={salt}&sign={sign}&docType=json&signType=v3&curtime={curTime}");
            string result = WebExt.Request(url, null, str);
            // 解析json数据
            JavaScriptSerializer js = new JavaScriptSerializer();
            string json_result      = GetDictResult(result);
            JsonId list             = js.Deserialize <JsonId>(json_result);

            return(string.Format($"姓名:{list.Name}\r\n性别:{list.Gender}\r\n" +
                                 $"民族:{list.Nation}\r\n出生:{list.Birthday}\r\n" +
                                 $"住址:{list.Address}\r\n身份证号码:{list.IDNumber}"));
        }
コード例 #5
0
        /// <summary>
        /// 短语音识别
        /// </summary>
        /// <param name="audioPath">音频文件路径</param>
        /// <param name="rate">采样率</param>
        /// <param name="lang">要识别的语言</param>
        /// <returns></returns>
        public static string Recognition(string audioPath, string rate, string lang)
        {
            string url     = "https://openapi.youdao.com/asrapi";
            string base64  = WebExt.FileToBase64(audioPath);
            string salt    = DateTime.Now.Millisecond.ToString();
            string curTime = WebExt.GetTimeSpan();
            string sign    = ComputeHash(YoudaoKey.AppKey + Truncate(base64) + salt + curTime + YoudaoKey.AppSecret);
            string str     = string.Format($"q={HttpUtility.UrlEncode(base64)}" +
                                           $"&langType={GetRecLang(lang)}&appKey={YoudaoKey.AppKey}" +
                                           $"&salt={salt}&sign={sign}&signType=v3&curtime={curTime}" +
                                           $"&format=wav&rate={rate}&channel=1&type=1");
            string result            = WebExt.Request(url, null, str);
            JavaScriptSerializer js  = new JavaScriptSerializer();
            JsonRecognition      lst = js.Deserialize <JsonRecognition>(result);

            if (lst.errorCode != "0")
            {
                ErrorTip(lst.errorCode, "https://ai.youdao.com/DOCSIRMA/html/%E8%AF%AD%E9%9F%B3%E8%AF%86%E5%88%ABASR/API%E6%96%87%E6%A1%A3/%E7%9F%AD%E8%AF%AD%E9%9F%B3%E8%AF%86%E5%88%AB%E6%9C%8D%E5%8A%A1/%E7%9F%AD%E8%AF%AD%E9%9F%B3%E8%AF%86%E5%88%AB%E6%9C%8D%E5%8A%A1-API%E6%96%87%E6%A1%A3.html");
                return("");
            }
            return(string.Join("", lst.result));
        }