using PaddleOCRSharp;
using System;
using System.Drawing;

public class OcrHelper
{

    //private static TesseractEngine engine = new TesseractEngine("./ocr_models", "chi_sim");
    //public static OCRResult GetOcrResult(Bitmap bitmap)
    //{
    //    try
    //    {
    //        using var tempBitmap = new Bitmap(bitmap);
    //        var ocrResult = paddleOCREngine.DetectText(tempBitmap);
    //        return ocrResult;
    //    }
    //    catch (Exception ex)
    //    {
    //        ExceptionNotice.SendAsync(ex, "Ocr文字识别异常！");
    //        throw;
    //    }
    //}

    private static string RootDirectory = EngineBase.GetRootDirectory().TrimEnd('\\') + "\\inference";

    //建议程序全局初始化一次即可，不必每次识别都初始化，容易报错。 
    private static PaddleOCREngine paddleOCREngine = paddleOCREngine = new PaddleOCREngine(new OCRModelConfig()
    {
        det_infer = RootDirectory + "\\ch_PP-OCRv3_det_infer",
        cls_infer= RootDirectory + "\\ch_ppocr_mobile_v2.0_cls_infer",
        rec_infer = RootDirectory + "\\ch_PP-OCRv3_rec_infer",
        keys = RootDirectory + "\\ppocr_keys.txt",
    }, new OCRParameter());
     
    public static OCRResult GetOcrResult(Bitmap bitmap)
    {
        try
        {
            using (var tempBitmap = new Bitmap(bitmap))
            {
                return paddleOCREngine.DetectText(tempBitmap);
            }
        }
        catch (Exception ex)
        {
            throw;
        }
    }

    public static OCRResult GetOcrResult(byte[] bytes)
    {
        try
        {
            var ocrResult = paddleOCREngine.DetectText(bytes);
            return ocrResult;
        }
        catch (Exception ex)
        {

            throw;
        }
    }

}

