// 车牌识别
//
public static string licensePlate(string imgURL) //获取前端传来的网络图片 如果没有在main函数里面用这个函数就行
{
//看看能不能返回token 这些都是切割
string[] res1 = getAccessToken().Split(new[] { "access_token" }, StringSplitOptions.None);
string[] res2 = res1[1].Split(new[] { "scope" }, StringSplitOptions.None);
string res3 = res2[0].Substring(3, 70);
string token = res3;
Console.WriteLine("经过切割后token是:" + res3);
//需要传入文件地址怎么办?
////httppost方法就是异步post方法
string httppicture = HttpPost("https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate",
"access_token=" + token + "&url=" + imgURL);//httppost方法就是异步post方法
//文字是识别的输出结果
Console.WriteLine("得到的结果:" + httppicture);
//进行切割后得到返回值 浙JG25T9 结果进行切割
// string[] res4 = httppicture.Split(new[] { "number" }, StringSplitOptions.None);
//string[] res5 = res4[1].Split(new[] { "vertexes_location" }, StringSplitOptions.None);
// string res6 = res5[0].Substring(3, 7);
// Console.WriteLine("得到的车牌号:" + res6);
//得到颜色
//这个后端写太麻烦了 看看前端string会不会更容易
return httppicture;
}
//实现网络图片的重要函数
public static string HttpPost(string url, string param)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Accept = "*/*";
request.Timeout = 15000;
request.AllowAutoRedirect = false;
StreamWriter requestStream = null;
WebResponse response = null;
string responseStr = null;
try
{
requestStream = new StreamWriter(request.GetRequestStream());
requestStream.Write(param);
requestStream.Close();
response = request.GetResponse();
if (response != null)
{
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
responseStr = reader.ReadToEnd();
reader.Close();
}
}
catch (Exception)
{
throw;
}
finally
{
request = null;
requestStream = null;
response = null;
}
return responseStr;
}
// 调用getAccessToken()获取的 access_token建议根据expires_in 时间 设置缓存
// 返回token示例
public static String TOKEN = "自己写,可以不写";
// 百度云中开通对应服务应用的 API Key 建议开通应用的时候多选服务
private static String clientId = "自己写";
// 百度云中开通对应服务应用的 Secret Key
private static String clientSecret = "自己写";
public static String getAccessToken()
{
String authHost = "https://aip.baidubce.com/oauth/2.0/token";
HttpClient client = new HttpClient();
List
paraList.Add(new KeyValuePair
paraList.Add(new KeyValuePair
paraList.Add(new KeyValuePair
HttpResponseMessage response = client.PostAsync(authHost, new FormUrlEncodedContent(paraList)).Result;
String result = response.Content.ReadAsStringAsync().Result;
Console.WriteLine(result);
return result;
}