首页 车牌识别 车牌识别示例代码 车牌识别[C#]

车牌识别示例代码C#

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

车牌识别

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

class LicensePlateRecognition
{
    public static async Task RecognizeAsync()
    {
        try
        {
            // 替换为你的实际 appkey
            string appKey = "YOUR_APPKEY_HERE";
            string imagePath = @"C:\Users\Administrator\Desktop\7777.jpg";

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

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

            // 发送 POST 请求
            using (HttpClient client = new HttpClient())
            {
                string url = "https://api.jisuapi.com/licenseplaterecognition/recognize";
                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"]);
                    return;
                }

                JObject result = (JObject)jsonarr["result"];
                foreach (var item in result)
                {
                    Console.WriteLine($"{item.Key}: {item.Value}");
                }
            }
        }
        catch (HttpRequestException ex)
        {
            Console.WriteLine($"请求异常:{ex.Message}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"发生错误:{ex.Message}");
        }
    }

    static async Task Main()
    {
       
       await LicensePlateRecognition.RecognizeAsync();
    }
}