Ejemplo n.º 1
0
        /// <summary>
        /// 表格文字识别
        /// </summary>
        /// <param name="path">图片路径</param>
        /// <param name="img"></param>
        /// <returns></returns>
        public static string FormOcrRequest(string downloadDir, Image img)
        {
            string host   = "https://aip.baidubce.com/rest/2.0/solution/v1/form_ocr/request?access_token=";
            string base64 = WebExt.ImageToBase64(img);
            string str    = "image=" + HttpUtility.UrlEncode(base64) + "&is_sync=true&request_type=excel";
            string json   = WebExt.Request(host, GetAccessToken(), str);
            // 接收序列化后的数据
            string result = FormOcrParseJson(json, out string url);

            if (downloadDir == null)
            {
                return(result);
            }
            string downloadPath = downloadDir + DateTime.Now.ToString("yyyy-MM-dd_HH_mm_ss") + ".xls";

            if (!Directory.Exists(Path.GetDirectoryName(downloadPath)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(downloadPath));
            }
            try
            {
                WebExt.DownloadFile(url, downloadPath);
                result += "\r\n是否下载:下载表格成功。\r\n保存路径:" + downloadPath;
            }
            catch (Exception ex)
            {
                result += "\r\n是否下载:下载表格失败!\r\n" + "原因:" + ex.Message;
            }
            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 百度翻译
        /// </summary>
        /// <param name="srcText">源文本</param>
        /// <param name="from">源语言</param>
        /// <param name="to">目标语言</param>
        public static string Translate(string srcText, string from, string to, string appid = null, string password = null)
        {
            string salt = DateTime.Now.Millisecond.ToString();
            string id   = appid ?? BaiduKey.AppId;
            string psd  = password ?? BaiduKey.Password;
            // 签名
            string sign = GetTranslateMD5(id + srcText + salt + psd);
            string url  = "https://fanyi-api.baidu.com/api/trans/vip/translate";
            string str  = string.Format($"q={HttpUtility.UrlEncode(srcText)}&from={GetTranLang(from)}&to={GetTranLang(to)}" +
                                        $"&appid={id}&salt={salt}&sign={sign}");
            string result             = WebExt.Request(url, null, str);
            JavaScriptSerializer js   = new JavaScriptSerializer();              // 实例化一个能够序列化数据的类
            BaiduTranslate       list = js.Deserialize <BaiduTranslate>(result); // 将json数据转化为对象类型并赋值给list

            if (list.Error_code != null)                                         // 如果调用Api出现错误
            {
                throw new Exception("调用百度翻译失败" + "\n原因:" + list.Error_msg);
            }

            // 接收序列化后的数据
            StringBuilder dst = new StringBuilder();

            foreach (var item in list.Trans_result)
            {
                dst.Append(item.Dst + "\r\n");
            }
            return(dst.ToString().TrimEnd('\r', '\n'));
        }
Ejemplo n.º 3
0
        public static void ErrorCorrection(string text, out string srcText, out string correctQuery)
        {
            string host   = "https://aip.baidubce.com/rpc/2.0/nlp/v1/ecnet?charset=UTF-8&&access_token=" + GetAccessToken(BaiduKey.CorrectionAK, BaiduKey.CorrectionSK);
            string str    = "{\"text\":\"" + text + "\"}";
            string result = WebExt.Request(host, null, str, "application/json");

            CorrectionParseJSON(result, out srcText, out correctQuery);
        }
Ejemplo n.º 4
0
        public static string Idcard(Image img)
        {
            string url = "https://aiapi.jd.com/jdai/ocr_idcard";

            byte[] bArr   = WebExt.ImageToBytes(img);
            string result = Post(url, bArr);

            return(IdcardParseJSON(result));
        }
Ejemplo n.º 5
0
        public static string GeneralBasic(Image img, string appkey = null, string secretkey = null)
        {
            string url = "https://aiapi.jd.com/jdai/ocr_universal";

            byte[] bArr   = WebExt.ImageToBytes(img);
            string result = Post(url: url, buffer: bArr, contentType: null, headers: null, appkey: appkey, secretkey: secretkey);

            return(GeneralParseJSON(result));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 公式识别
        /// </summary>
        /// <param name="path"></param>
        /// <param name="img"></param>
        /// <returns></returns>
        public static string Formula(Image img)
        {
            string host   = "https://aip.baidubce.com/rest/2.0/ocr/v1/formula?access_token=";
            string base64 = WebExt.ImageToBase64(img);
            string str    = "image=" + HttpUtility.UrlEncode(base64) + "&detect_direction=true";
            string result = WebExt.Request(host, GetAccessToken(), str);

            return(GeneralParseJSON(result));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 银行卡识别
        /// </summary>
        /// <param name="path"></param>
        /// <param name="img"></param>
        /// <returns></returns>
        public static string BankCard(Image img)
        {
            string host   = "https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard?access_token=";
            string base64 = WebExt.ImageToBase64(img);
            string str    = "image=" + HttpUtility.UrlEncode(base64) + "&detect_direction=true";
            string result = WebExt.Request(host, GetAccessToken(), str);

            result = BankCardParseJson(result);
            return(result);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 身份证识别
        /// </summary>
        /// <param name="cardSide">"front"含照片面,"back"含国徽面</param>
        /// <param name="path">图片路径</param>
        /// <param name="img"></param>
        /// <returns></returns>
        public static string Idcard(string cardSide, Image img)
        {
            string host   = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=";
            string base64 = WebExt.ImageToBase64(img);
            string str    = "id_card_side=" + cardSide + "&detect_direction=true&detect_risk=true&detect_photo=true&image=" + HttpUtility.UrlEncode(base64);
            string result = WebExt.Request(host, GetAccessToken(), str);

            if (cardSide.ToLower() == "back") // 含国徽面
            {
                result = IdcardBackParseJson(result);
            }
            else                              // 含照片面
            {
                result = IdcardFrontParseJson(result);
            }
            return(result);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 获取AccessToken,失败时抛出异常
        /// </summary>
        /// <returns></returns>
        public static string GetAccessToken(string apikey = null, string secretkey = null)
        {
            string host             = "https://aip.baidubce.com/oauth/2.0/token";
            string ak               = apikey ?? BaiduKey.ApiKey;
            string sk               = secretkey ?? BaiduKey.SecretKey;
            string str              = string.Format($"grant_type=client_credentials&client_id={ak}&client_secret={sk}");
            string result           = WebExt.Request(host, "", str);
            JavaScriptSerializer js = new JavaScriptSerializer();     // 实例化一个能够序列化数据的类
            Token list              = js.Deserialize <Token>(result); // 将json数据转化为对象并赋值给list

            if (list.error != null)
            {
                throw new Exception("获取AccessToken失败!" + "\n原因:" + list.error_description);
            }

            return(list.access_token);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// 二维码识别
        /// </summary>
        /// <param name="path"></param>
        /// <param name="img"></param>
        /// <returns></returns>
        public static string QRCode(Image img)
        {
            string host   = "https://aip.baidubce.com/rest/2.0/ocr/v1/qrcode?access_token=";
            string base64 = WebExt.ImageToBase64(img);
            string str    = "image=" + HttpUtility.UrlEncode(base64);
            string result = WebExt.Request(host, GetAccessToken(), str);
            // 解析json数据
            JavaScriptSerializer js   = new JavaScriptSerializer();
            BaiduOcrJson         list = js.Deserialize <BaiduOcrJson>(result);

            if (list.error_code != null) // 如果调用Api出现错误
            {
                throw new Exception("OCR识别错误!" + "\n原因:" + list.error_msg);
            }
            if (list.codes_result.Count == 0)
            {
                return("未检测到二维码");
            }

            // 接收序列化后的数据
            StringBuilder builder = new StringBuilder();

            foreach (var item in list.codes_result)
            {
                builder.Append("二维码类型:" + item.type + "\r\n");
                builder.Append("二维码内容:");
                foreach (var text in (IEnumerable <object>)item.text)
                {
                    builder.Append(text + "\r\n");
                }
            }

            // 查找最后一个换行符的位置
            int len = builder.ToString().LastIndexOf('\r');

            return(len < 0 ? "" : builder.ToString().Remove(len));
        }
Ejemplo n.º 11
0
        /// <summary>
        /// 百度文字识别(通用、通用(高精度)、手写、数字)
        /// </summary>
        /// <param name="mode">模式(通用、通用(高精度)、手写、数字)</param>
        /// <param name="path">图片路径</param>
        /// <param name="img">image</param>
        /// <param name="langType">识别语言</param>
        /// <returns></returns>
        public static string GeneralBasic(string mode, Image img, string langType = null)
        {
            string general_basic_host  = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=";
            string accurate_basic_host = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token=";
            string handwriting_host    = "https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting?access_token=";
            string numbers_host        = "https://aip.baidubce.com/rest/2.0/ocr/v1/numbers?access_token=";

            // 进行base64编码
            string base64 = WebExt.ImageToBase64(img);
            string host = null, str = null;

            if (mode.IndexOf("通用") != -1)
            {
                host = general_basic_host;
                str  = "image=" + HttpUtility.UrlEncode(base64, Encoding.UTF8) + "&language_type=" + GetLangType(langType) + "&detect_direction=true";
            }
            else if (mode.IndexOf("高精度") != -1)
            {
                host = accurate_basic_host;
                str  = "image=" + HttpUtility.UrlEncode(base64, Encoding.UTF8) + "&language_type=" + GetLangType(langType) + "&detect_direction=true";
            }
            else if (mode.IndexOf("手写") != -1)
            {
                host = handwriting_host;
                str  = "image=" + HttpUtility.UrlEncode(base64, Encoding.UTF8);
            }
            else if (mode.IndexOf("数字") != -1)
            {
                host = numbers_host;
                str  = "image=" + HttpUtility.UrlEncode(base64, Encoding.UTF8) + "&detect_direction=true";
            }
            // 发送请求,接收数据
            string result = WebExt.Request(host, GetAccessToken(), str);

            return(GeneralParseJSON(result));
        }
Ejemplo n.º 12
0
 private static string GetSign(string sk, string ts) => WebExt.GetMD5(sk + ts);
Ejemplo n.º 13
0
 private static string GetSign(string sk, string ts)
 {
     return(WebExt.GetMD5(sk + ts));
 }