c#调用百度AI银行卡识别
电脑版发表于:2020/2/15 12:15
code:
public class BankCard { // 银行卡识别 public static BankCardModel bankCard(string token) { string host = "https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard?access_token=" + token; Encoding encoding = Encoding.Default; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host); request.Method = "post"; request.KeepAlive = true; // 图片的base64编码 string base64 = getFileBase64("c://card//bb.jpg"); String str = "image=" + HttpUtility.UrlEncode(base64); 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); BankCardModel bankCardModel = JsonConvert.DeserializeObject<BankCardModel>(result); return bankCardModel; } public static String getFileBase64(String fileName) { FileStream filestream = new FileStream(fileName, FileMode.Open); byte[] arr = new byte[filestream.Length]; filestream.Read(arr, 0, (int)filestream.Length); string baser64 = Convert.ToBase64String(arr); filestream.Close(); return baser64; } }
实体:
public class BankCardModel { public BankCardModelItem result { get; set; } } public class BankCardModelItem { public string bank_card_number { get; set; } public string valid_date { get; set; } }