百度有直接的Nuget包引用非常的简单调用:
具体怎么用看说明文档就够了:
static void Main(string[] args) { GeneralBasicDemo(); } public static void GeneralBasicDemo() { var API_KEY = "********"; var SECRET_KEY = "*************"; var client = new Baidu.Aip.Ocr.Ocr(API_KEY, SECRET_KEY); client.Timeout = 60000; // 修改超时时间 string img = @"E:imgFile15.jpg"; var image = File.ReadAllBytes(img); // 调用通用文字识别(高精度版),可能会抛出网络等异常,请使用try/catch捕获 // 如果有可选参数 var options = new Dictionary<string, object>{ {"detect_direction", "true"}, {"probability", "true"} }; // 带参数调用通用文字识别(高精度版) var result = client.AccurateBasic(image, options); string str = string.Empty; var txts = (from obj in (JArray)result.Root["words_result"] select (string)obj["words"]); //foreach (var r in txts) //{ // str += r+" "; //} str = string.Join(" ", txts); Console.WriteLine(str); Console.ReadKey(); }
需要注意的是返回的数据是JObject格式,搞了大半天后才拿到数据,类似键值对的方式,直接用linq获取即可;