Example #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'));
        }
Example #2
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())));
        }
Example #3
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}"));
        }