首页 驾驶证识别 驾驶证识别示例代码 驾驶证识别[C#]

驾驶证识别示例代码C#

作者: 阅读数:1124 上传时间:2025-04-23

驾驶证识别

using System;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

class Driverlicenserecognition
{
    // 替换成你的AppKey
    private const string AppKey = "YOUR_APPKEY_HERE";

    public static async Task RecognizeAsync(string imagePath)
    {
        try
        {
            using (HttpClient client = new HttpClient())
            {
                // 构造请求URL
                string url = $"https://api.jisuapi.com/driverlicenserecognition/recognize?appkey={AppKey}";

                // 读取图片并进行Base64编码
                byte[] imageBytes = File.ReadAllBytes(imagePath);
                string picBase64 = Convert.ToBase64String(imageBytes);

                // 构造POST请求数据
                var postData = new StringContent($"pic={picBase64}", Encoding.UTF8, "application/x-www-form-urlencoded");

                // 发送POST请求
                HttpResponseMessage response = await client.PostAsync(url, postData);

                // 确保成功响应
                response.EnsureSuccessStatusCode();

                // 读取响应内容
                string responseBody = await response.Content.ReadAsStringAsync();
                JObject jsonarr = JObject.Parse(responseBody);

                // 处理API返回状态
                if ((int)jsonarr["status"] != 0)
                {
                    Console.WriteLine(jsonarr["msg"]);
                }
                else
                {
                    JToken result = jsonarr["result"];
                    Console.WriteLine($"{result["licensenumber"]} {result["realname"]} {result["sex"]}");
                    Console.WriteLine($"{result["address"]} {result["birth"]} {result["initialdate"]}");
                    Console.WriteLine($"{result["type"]} {result["startdate"]} {result["enddate"]}");
                }
            }
        }
        catch (HttpRequestException ex)
        {
            Console.WriteLine($"请求异常:{ex.Message}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"发生错误:{ex.Message}");
        }
    }

    static async Task Main(string[] args)
    {
        string imagePath = @"C:\Users\Administrator\Desktop\166.jpg";
        await Driverlicenserecognition.RecognizeAsync(imagePath);
    }
}