/// <summary> /// 调用通用文字识别, 图片参数为远程url图片 /// </summary> /// <param name="url">远程图片路径</param> /// <returns>list集合一行一个值</returns> public List <string> GeneralBasicUrl(string url) { List <string> contents = new List <string>(); // 调用通用文字识别, 图片参数为远程url图片,可能会抛出网络等异常,请使用try/catch捕获 JObject result = client.GeneralBasicUrl(url); // 如果有可选参数 Dictionary <string, object> options = new Dictionary <string, object>(); options.Add("language_type", "CHN_ENG"); options.Add("detect_direction", "true"); options.Add("detect_language", "true"); options.Add("probability", "true"); // 带参数调用通用文字识别, 图片参数为远程url图片 result = client.GeneralBasicUrl(url, options); string content = result["words_result"].ToString(); JArray jarray = JArray.Parse(content); for (int i = 0; i < jarray.Count; i++) { JObject jobject = JObject.Parse(jarray[i].ToString()); string hang = jobject["words"].ToString(); contents.Add(hang); } return(contents); }
public void Test() { //去配置文档读取配置 BaiduConfig baiduConfig = new BaiduConfig() { AppId = "17009825", ApiKey = "HmWuhVwnDaIQ0OWC4mEs24RG", SecretKey = "cZc6TeGQL2eGgpkpgKtmacbQC1WUtLOZ" }; string filePath = @"f:\files\verificationCode_keyword.png"; var client = new Ocr(baiduConfig.ApiKey, baiduConfig.SecretKey); client.Timeout = 60000; var image = File.ReadAllBytes(filePath); var options = new Dictionary <string, object> { { "language_type", "CHN_ENG" }, { "detect_direction", "true" }, { "detect_language", "true" }, { "probability", "true" } }; // 调用通用文字识别, 图片参数为本地图片,可能会抛出网络等异常,请使用try/catch捕获 var result = client.GeneralBasic(image, options); Console.WriteLine(result); var url = "https://ai.bdstatic.com/file/03D0F32FE36C4E3A893D1AD60E797F5B"; // 调用通用文字识别, 图片参数为远程url图片,可能会抛出网络等异常,请使用try/catch捕获 var result2 = client.GeneralBasicUrl(url); Console.WriteLine(result2); // 如果有可选参数 var options2 = new Dictionary <string, object> { { "language_type", "CHN_ENG" }, { "detect_direction", "true" }, { "detect_language", "true" }, { "probability", "true" } }; // 带参数调用通用文字识别, 图片参数为远程url图片 result2 = client.GeneralBasicUrl(url, options2); Console.WriteLine(result2); }
static public void GeneralBasicUrlDemo() { var url = "http://im.zujuan.xkw.com/Parse/7121096/11/700/14/28/7e27af2fa1a1878115058598985874ca"; // 调用通用文字识别, 图片参数为远程url图片,可能会抛出网络等异常,请使用try/catch捕获 var result = client.GeneralBasicUrl(url); Console.WriteLine(result); // 如果有可选参数 var options = new Dictionary <string, object> { { "language_type", "CHN_ENG" }, { "detect_direction", "true" }, { "detect_language", "true" }, { "probability", "true" } }; // 带参数调用通用文字识别, 图片参数为远程url图片 result = client.GeneralBasicUrl(url, options); Console.WriteLine(result); }
public ActionResultVM OcrAPI(string image_base64, string image_url) { var vm = new ActionResultVM(); try { var client = new Ocr(API_KEY, SECRET_KEY) { Timeout = 60000 }; var options = new Dictionary <string, object> { { "language_type", "CHN_ENG" }, { "detect_direction", "true" }, { "detect_language", "true" }, { "probability", "true" } }; if (!string.IsNullOrWhiteSpace(image_url)) { var result = client.GeneralBasicUrl(image_url, options); vm.Set(ARTag.success); vm.data = result; } else { var image = Convert.FromBase64String(image_base64); var result = client.GeneralBasic(image, options); vm.Set(ARTag.success); vm.data = result; } } catch (Exception ex) { vm.Set(ex); } return(vm); }