OCREngine.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Imaging;
  5. using System.IO;
  6. using System.Linq;
  7. using Tesseract;
  8. namespace OCRTest
  9. {
  10. /// <summary>
  11. /// OCR配置类
  12. /// </summary>
  13. public class OCRConfig
  14. {
  15. /// <summary>
  16. /// 识别语言(如:"eng", "chi_sim", "chi_sim+eng")
  17. /// </summary>
  18. public string Language { get; set; } = "eng";
  19. /// <summary>
  20. /// 引擎模式
  21. /// </summary>
  22. public EngineMode EngineMode { get; set; } = EngineMode.Default;
  23. /// <summary>
  24. /// 字符白名单(只识别这些字符,提高准确率)
  25. /// </summary>
  26. public string Whitelist { get; set; } = null;
  27. /// <summary>
  28. /// 是否启用图像预处理
  29. /// </summary>
  30. public bool EnablePreprocessing { get; set; } = true;
  31. /// <summary>
  32. /// Tessdata目录路径
  33. /// </summary>
  34. public string TessDataPath { get; set; } = "./tessdata";
  35. /// <summary>
  36. /// PSM页面分割模式(0-13)
  37. /// </summary>
  38. public PageSegMode PageSegMode { get; set; } = PageSegMode.Auto;
  39. }
  40. /// <summary>
  41. /// OCR识别结果
  42. /// </summary>
  43. public class OCRResult
  44. {
  45. /// <summary>
  46. /// 识别的文本
  47. /// </summary>
  48. public string Text { get; set; }
  49. /// <summary>
  50. /// 置信度(0-100)
  51. /// </summary>
  52. public float Confidence { get; set; }
  53. /// <summary>
  54. /// 识别耗时(毫秒)
  55. /// </summary>
  56. public long ElapsedMilliseconds { get; set; }
  57. /// <summary>
  58. /// 是否成功
  59. /// </summary>
  60. public bool Success { get; set; }
  61. /// <summary>
  62. /// 错误信息
  63. /// </summary>
  64. public string Error { get; set; }
  65. }
  66. /// <summary>
  67. /// 高性能OCR识别引擎(线程安全单例)
  68. /// </summary>
  69. public class OCREngine : IDisposable
  70. {
  71. private static readonly object _lock = new object();
  72. private static Dictionary<string, OCREngine> _instances = new Dictionary<string, OCREngine>();
  73. private TesseractEngine _engine;
  74. private OCRConfig _config;
  75. private bool _disposed = false;
  76. /// <summary>
  77. /// 获取默认实例(英文识别)
  78. /// </summary>
  79. public static OCREngine Instance
  80. {
  81. get
  82. {
  83. return GetInstance("eng");
  84. }
  85. }
  86. /// <summary>
  87. /// 获取指定语言的实例
  88. /// </summary>
  89. /// <param name="language">语言代码</param>
  90. /// <returns>OCR引擎实例</returns>
  91. public static OCREngine GetInstance(string language)
  92. {
  93. lock (_lock)
  94. {
  95. if (!_instances.ContainsKey(language))
  96. {
  97. var config = new OCRConfig { Language = language };
  98. _instances[language] = new OCREngine(config);
  99. }
  100. return _instances[language];
  101. }
  102. }
  103. /// <summary>
  104. /// 获取自定义配置的实例
  105. /// </summary>
  106. /// <param name="config">配置对象</param>
  107. /// <returns>OCR引擎实例</returns>
  108. public static OCREngine GetInstance(OCRConfig config)
  109. {
  110. string key = $"{config.Language}_{config.EngineMode}";
  111. lock (_lock)
  112. {
  113. if (!_instances.ContainsKey(key))
  114. {
  115. _instances[key] = new OCREngine(config);
  116. }
  117. return _instances[key];
  118. }
  119. }
  120. /// <summary>
  121. /// 私有构造函数
  122. /// </summary>
  123. private OCREngine(OCRConfig config)
  124. {
  125. _config = config;
  126. InitializeEngine();
  127. }
  128. /// <summary>
  129. /// 初始化Tesseract引擎
  130. /// </summary>
  131. private void InitializeEngine()
  132. {
  133. try
  134. {
  135. // 检查tessdata目录是否存在
  136. if (!Directory.Exists(_config.TessDataPath))
  137. {
  138. throw new DirectoryNotFoundException($"Tessdata目录不存在:{_config.TessDataPath}");
  139. }
  140. // 创建引擎
  141. _engine = new TesseractEngine(_config.TessDataPath, _config.Language, _config.EngineMode);
  142. // 设置PSM模式
  143. _engine.DefaultPageSegMode = _config.PageSegMode;
  144. // 设置字符白名单(如果指定)
  145. if (!string.IsNullOrEmpty(_config.Whitelist))
  146. {
  147. _engine.SetVariable("tessedit_char_whitelist", _config.Whitelist);
  148. }
  149. // 优化性能配置
  150. _engine.SetVariable("tessedit_pageseg_mode", ((int)_config.PageSegMode).ToString());
  151. Console.WriteLine($"OCR引擎初始化成功 - 语言:{_config.Language}, 模式:{_config.EngineMode}");
  152. }
  153. catch (Exception ex)
  154. {
  155. throw new InvalidOperationException($"OCR引擎初始化失败:{ex.Message}", ex);
  156. }
  157. }
  158. /// <summary>
  159. /// 识别图片文件
  160. /// </summary>
  161. /// <param name="imagePath">图片路径</param>
  162. /// <returns>识别结果</returns>
  163. public OCRResult RecognizeText(string imagePath)
  164. {
  165. if (string.IsNullOrEmpty(imagePath))
  166. return new OCRResult { Success = false, Error = "图片路径不能为空" };
  167. if (!File.Exists(imagePath))
  168. return new OCRResult { Success = false, Error = $"文件不存在:{imagePath}" };
  169. try
  170. {
  171. var sw = System.Diagnostics.Stopwatch.StartNew();
  172. // 加载图片
  173. using (var pix = Pix.LoadFromFile(imagePath))
  174. {
  175. // 图像预处理(如果启用)
  176. Pix processedPix = _config.EnablePreprocessing ? PreprocessImage(pix) : pix;
  177. // 执行识别
  178. using (var page = _engine.Process(processedPix))
  179. {
  180. var text = page.GetText();
  181. var confidence = page.GetMeanConfidence();
  182. sw.Stop();
  183. return new OCRResult
  184. {
  185. Text = text.Trim(),
  186. Confidence = confidence * 100,
  187. ElapsedMilliseconds = sw.ElapsedMilliseconds,
  188. Success = true
  189. };
  190. }
  191. }
  192. }
  193. catch (Exception ex)
  194. {
  195. return new OCRResult
  196. {
  197. Success = false,
  198. Error = $"识别失败:{ex.Message}"
  199. };
  200. }
  201. }
  202. /// <summary>
  203. /// 从Bitmap识别文字
  204. /// </summary>
  205. /// <param name="bitmap">Bitmap对象</param>
  206. /// <returns>识别结果</returns>
  207. public OCRResult RecognizeFromBitmap(Bitmap bitmap)
  208. {
  209. if (bitmap == null)
  210. return new OCRResult { Success = false, Error = "Bitmap不能为空" };
  211. try
  212. {
  213. var sw = System.Diagnostics.Stopwatch.StartNew();
  214. // 将Bitmap转换为Pix
  215. using (var pix = BitmapToPix(bitmap))
  216. {
  217. // 图像预处理
  218. Pix processedPix = _config.EnablePreprocessing ? PreprocessImage(pix) : pix;
  219. // 执行识别
  220. using (var page = _engine.Process(processedPix))
  221. {
  222. var text = page.GetText();
  223. var confidence = page.GetMeanConfidence();
  224. sw.Stop();
  225. return new OCRResult
  226. {
  227. Text = text.Trim(),
  228. Confidence = confidence * 100,
  229. ElapsedMilliseconds = sw.ElapsedMilliseconds,
  230. Success = true
  231. };
  232. }
  233. }
  234. }
  235. catch (Exception ex)
  236. {
  237. return new OCRResult
  238. {
  239. Success = false,
  240. Error = $"识别失败:{ex.Message}"
  241. };
  242. }
  243. }
  244. /// <summary>
  245. /// 批量识别图片
  246. /// </summary>
  247. /// <param name="imagePaths">图片路径数组</param>
  248. /// <returns>识别结果列表</returns>
  249. public List<OCRResult> RecognizeBatch(string[] imagePaths)
  250. {
  251. var results = new List<OCRResult>();
  252. foreach (var path in imagePaths)
  253. {
  254. var result = RecognizeText(path);
  255. results.Add(result);
  256. }
  257. return results;
  258. }
  259. /// <summary>
  260. /// 图像预处理(灰度化、二值化、降噪)
  261. /// </summary>
  262. private Pix PreprocessImage(Pix originalPix)
  263. {
  264. try
  265. {
  266. // 转换为灰度图
  267. using (var grayPix = originalPix.ConvertRGBToGray())
  268. {
  269. // 二值化处理(Otsu阈值)
  270. using (var binaryPix = grayPix.BinarizeOtsuAdaptiveThreshold(200, 200, 10, 10, 0.1f))
  271. {
  272. // 返回处理后的图片(需要克隆,因为using会释放)
  273. return binaryPix.Clone();
  274. }
  275. }
  276. }
  277. catch
  278. {
  279. // 预处理失败,返回原图
  280. return originalPix.Clone();
  281. }
  282. }
  283. /// <summary>
  284. /// 将Bitmap转换为Pix
  285. /// </summary>
  286. private Pix BitmapToPix(Bitmap bitmap)
  287. {
  288. // 确保是24位或32位格式
  289. Bitmap tempBitmap = bitmap;
  290. bool needDispose = false;
  291. if (bitmap.PixelFormat != PixelFormat.Format24bppRgb &&
  292. bitmap.PixelFormat != PixelFormat.Format32bppArgb)
  293. {
  294. tempBitmap = new Bitmap(bitmap.Width, bitmap.Height, PixelFormat.Format24bppRgb);
  295. using (var g = Graphics.FromImage(tempBitmap))
  296. {
  297. g.DrawImage(bitmap, 0, 0);
  298. }
  299. needDispose = true;
  300. }
  301. try
  302. {
  303. // 保存为临时PNG文件
  304. string tempFile = Path.GetTempFileName() + ".png";
  305. tempBitmap.Save(tempFile, ImageFormat.Png);
  306. // 加载为Pix
  307. var pix = Pix.LoadFromFile(tempFile);
  308. // 删除临时文件
  309. File.Delete(tempFile);
  310. return pix;
  311. }
  312. finally
  313. {
  314. if (needDispose)
  315. {
  316. tempBitmap.Dispose();
  317. }
  318. }
  319. }
  320. /// <summary>
  321. /// 释放资源
  322. /// </summary>
  323. public void Dispose()
  324. {
  325. if (!_disposed)
  326. {
  327. _engine?.Dispose();
  328. _disposed = true;
  329. }
  330. }
  331. /// <summary>
  332. /// 清理所有实例
  333. /// </summary>
  334. public static void Cleanup()
  335. {
  336. lock (_lock)
  337. {
  338. foreach (var instance in _instances.Values)
  339. {
  340. instance.Dispose();
  341. }
  342. _instances.Clear();
  343. }
  344. }
  345. }
  346. }