文档:
https://ai.baidu.com/ai-doc/FACE/7k37c1ucj
身份证文档 https://ai.baidu.com/tech/ocr_cards/idcard
活体检测 https://ai.baidu.com/ai-doc/FACE/lk37c1tag
// 调用getAccessToken()获取的 access_token建议根据expires_in 时间 设置缓存 // 返回token示例 public static String TOKEN = "24.adda70c11b9786206253ddb70affdc46.2592000.1493524354.282335-1234567"; // 百度云中开通对应服务应用的 API Key 建议开通应用的时候多选服务 private static String clientId = "百度云应用的AK"; // 百度云中开通对应服务应用的 Secret Key private static String clientSecret = "百度云应用的SK"; 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; } public static string personVerify() { string token = "[调用鉴权接口获取的token]"; string host = "https://aip.baidubce.com/rest/2.0/face/v3/person/verify?access_token=" + token; Encoding encoding = Encoding.Default; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host); request.Method = "post"; request.KeepAlive = true; String str = $"{"image":"sfasq35sadvsvqwr5q...","image_type":"BASE64","id_card_number":"110...","name":"张三","quality_control":"LOW","liveness_control":"HIGH"}"; 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.Default); string result = reader.ReadToEnd(); Console.WriteLine("身份验证:"); Console.WriteLine(result); return result; } public static string idcard() { string token = "[调用鉴权接口获取的token]"; string host = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=" + token; Encoding encoding = Encoding.Default; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host); request.Method = "post"; request.KeepAlive = true; // 图片的base64编码 string base64 = getFileBase64("[本地图片文件]"); String str = "id_card_side=" + "front" + "&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.Default); string result = reader.ReadToEnd(); Console.WriteLine("身份证识别:"); Console.WriteLine(result); return result; } 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; }