|

分享源码
界面截图: |
- |
是否带模块: |
纯源码 |
备注说明: |
- |
using System;
using System.Drawing;
using System.IO;
using System.Threading.Tasks;
using PaddleOCRSharp;
public static class OCR
{
// 全局 OCR 引擎实例(建议全局初始化一次)
private static PaddleOCREngine engine;
// 初始化 OCR 引擎(可以在应用程序启动时调用)
public static void InitializeEngine(OCRModelConfig config, OCRParameter oCRParameter)
{
try
{
if (engine == null)
{
engine = new PaddleOCREngine(config, oCRParameter);
Console.WriteLine("OCR 引擎已全局初始化");
}
}
catch (Exception ex)
{
Console.WriteLine($"OCR 引擎初始化失败: {ex.Message}\n堆栈跟踪: {ex.StackTrace}");
throw;
}
}
/// <summary>
/// 自动加载默认配置并初始化 OCR 引擎。
/// </summary>
public static void LoadDefaultConfig()
{
// 这里应该有逻辑来创建默认的 OCRModelConfig 和 OCRParameter 对象
var defaultConfig = new OCRModelConfig(); // 假设这是默认配置
var defaultParams = new OCRParameter(); // 假设这是默认参数
InitializeEngine(null, defaultParams);
}
/// <summary>
/// 异步地通过传递 Base64 编码的图像数据进行 OCR 识别,并返回识别结果。
/// </summary>
/// <param name="base64Image">Base64 编码的图像数据</param>
/// <returns>识别到的文本</returns>
public static async Task<string> RecognizeFromBase64Async(string base64Image)
{
try
{
// 检查 OCR 引擎是否已初始化
if (engine == null)
{
throw new InvalidOperationException("OCR 引擎未初始化,请先调用 InitializeEngine 方法。");
}
// 去除 Base64 字符串中的 "data:image/*;base64," 前缀(如果存在)
if (base64Image.StartsWith("data:image/"))
{
base64Image = base64Image.Substring(base64Image.IndexOf(',') + 1);
}
// 解码 Base64 字符串为字节数组
byte[] imageBytes = Convert.FromBase64String(base64Image);
// 将字节数组转换为 Bitmap 对象
using (var ms = new MemoryStream(imageBytes))
using (var bitmap = new Bitmap(ms))
{
// 使用 OCR 引擎进行文本识别
OCRResult ocrResult = await Task.Run(() => engine.DetectText(bitmap));
// 返回识别到的文本
return ocrResult?.Text ?? "未识别到任何文本";
}
}
catch (Exception ex)
{
// 捕获并记录异常
Console.WriteLine($"发生错误: {ex.Message}\n堆栈跟踪: {ex.StackTrace}");
return $"识别失败: {ex.Message}";
}
}
}
上面的源码单独用一个文件 private async void button6_Click(object sender, EventArgs e)
{
string code = await OCR.RecognizeFromBase64Async("/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAAoAHgDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwDs9U1W00ez+03jsqZ2qFUks2CQB+R68VyT/EdBIwj0tmTJ2lp8EjtkbTj86k+IccEltaObuJZ4ScQE/M6tjkfTb34988HD/wCEu/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M/De806C7vzdalHpcxRPLkyg3rk7hukDAc7eARn3xxR8Zavbah4imMVybu3gjEK3MhDiRwckgBQAozt+UYOM8k8+l614Q8NzST38+krPezsdsaTPH50hHAwpwCcZJx6se5rzqC0W8+LEFrZiKNLe9X5REsSDyvmcKq5wMo2O/Iyc5NenD2VWbmk9tb9PQ1523oerWGiaWthB9hN1HasgeJY7uZF2nngbhjrXM/EfT4LHwusun2EELG4RZZIbdQVTB/iAyo3bRnjrjvXXj/QL0g8WtyxbJ4EcpwNv0bk9vmzyS4FXZI45onilRZI3UqyMMhgeoI7ivO5Y32HCrKMk7nl/g7xN4Y0fQEe9GNViaRmYwl5HPONjcgArhcZHOfXJzPAto2seOJr6G2EFkhlmkjXIWMNkIgIAGQSCOn3CR0r0GTwD4YlleRtKUM7FiFmkUZPoA2APYVu2llaWERis7WG3jLbikMYQE+uB34FXdLY2nXhaXLe77ke25tfuBrqL+6zASL7AnAYfUg9eTmuD+KOo+bp2nW0WDHJM8j7lKurIAAMHGOHJ5Hp+Po9Z+q6Jp2tQGK+tkk4wHxhl4PQ/iaItJ3ZzQfLK5keC7rTrfw5pmnR6nazXRiLmJZV3gtlyu3OcjJH4dq6eua03wzpXh68F1Bp25wpVbhN7sgPHKknk/3lHc8AdeiiljnjEkUiyIejIcg/jRLe6CTTd0PoooqSQooooAKKKKACiiigCOaGO4haKVdyH3wQRyCCOQQeQRyDTbVZ0to0uXV5lG1nXjfj+LHbPXHbOOetFFAE1FFFABRRRQAVXltAZDNA3kzn7zqoO/0DDuPyPoRk0UUAEU04kEVxDyeksfKH6jqp4PqOgyTRRRQB//2Q==");
textBox1.Text = code;
} 这个是调用案例 传递参数是bs64 这个可以随便修改 传递字节集
|
|