예제 #1
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())));
        }
        public ActionResult ViewById(int Id)
        {
            JsonForm form = this.FormRepository.Find(Id) ?? throw new NullReferenceException($"Form not found with Id {Id}");

            return(this.View("ViewJsonForm", form));
        }