通过百度AI识别图片上的文字

一、首先要自己去百度AI注册,然后获取一个APIKey和SecretKey通过;

二、开始识别图片上的文字,一下为部分代码

if (comboBox1.Text.Equals("文字识别通用含位置版"))
                {
                    url = TysbLdUrl;
                }
                if (comboBox1.Text.Equals("文字识别高精度版"))
                {
                    url = TysbGjdUrl;
                }
                if (comboBox1.Text.Equals("文字识别高精度位置版"))
                {
                    url = TysbGjdLdUrl;
                }
                if (comboBox1.Text.Equals("网络图片文字识别"))
                {
                    url = NetsbUrl;
                }
                var img = System.Windows.Forms.Clipboard.GetImage().ImgToBase64String();
               pictureBox1.Image = System.Windows.Forms.Clipboard.GetImage();
                var token = AccessToken.getAccessToken().ToModel<Rootobject>().access_token;
               string strbaser64 = img; // 图片的base64编码
               string host = $"{url}?access_token=" + token;
               Encoding encoding = Encoding.Default;
               HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host);
               request.Method = "post";
               request.ContentType = "application/x-www-form-urlencoded";
               request.KeepAlive = true;
               String str = "image=" + HttpUtility.UrlEncode(strbaser64);
               byte[] buffer = encoding.GetBytes(str);
               request.ContentLength = buffer.Length;
              
               request.GetRequestStream().Write(buffer, 0, buffer.Length);
               HttpWebResponse response = (HttpWebResponse)request.GetResponse();
              
               StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
               string result = reader.ReadToEnd();
               Console.WriteLine("通用文字识别:");
               Console.WriteLine(result);
               richTextBox1.Text = result;

其中access_token是通过方法AccessToken获取,其中apikey和secretkey是第一步需要获取的

  public static class AccessToken
    {
        // 调用getAccessToken()获取的 access_token建议根据expires_in 时间 设置缓存
        // 返回token示例
        public static String TOKEN = "24.adda70c11b9786206253ddb70affdc46.2592000.1493524354.282335-1234567";
        // 百度云中开通对应服务应用的 API Key 建议开通应用的时候多选服务
        private static String clientId = ConfigurationManager.AppSettings["APIKey"];
        // 百度云中开通对应服务应用的 Secret Key
        private static String clientSecret = ConfigurationManager.AppSettings["SecretKey"];

        public static String getAccessToken()
        {
            String authHost = "https://aip.baidubce.com/oauth/2.0/token";
            HttpClient client = new HttpClient();
            List<KeyValuePair<String, String>> paraList = new List<KeyValuePair<string, string>>();
            paraList.Add(new KeyValuePair<string, string>("grant_type", "client_credentials"));
            paraList.Add(new KeyValuePair<string, string>("client_id", clientId));
            paraList.Add(new KeyValuePair<string, string>("client_secret", clientSecret));
            HttpResponseMessage response = client.PostAsync(authHost, new FormUrlEncodedContent(paraList)).Result;
            String result = response.Content.ReadAsStringAsync().Result;
            Console.WriteLine(result);
            return result;
        }
    }
 string host = $"{url}?access_token=" + token;

url是不同的种类区分开来,比如有网络图文识别,普识别等等,看看我的截面图

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值