www.pudn.com > Tesseract.rar > Tesseract.cs, change:2016-12-22,size:7683b


using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Drawing; 
using System.Data; 
using Tesseract; 
using Class; 
namespace OCR 
{ 
    public class Tesseract 
    { 
        static Logger log = new Logger(); 
        public static string GetText(string imgPath) 
        { 
            log.WriteLog("Tesseract:" + imgPath, 1); 
            string ocrtext = string.Empty; 
            using (Bitmap imgsource = new Bitmap(imgPath)) 
            { 
                //灰度化                
                log.WriteLog("灰度化开始", 1); 
                Bitmap tmpBmp = ToGrey(imgsource); 
                log.WriteLog("灰度化完成", 1); 
                
                //Bitmap tmpBmp = imgsource; 
 
                //////二值化 
                //log.WriteLog("二值化开始:", 1); 
                //tmpBmp = Thresholding(tmpBmp); 
                //log.WriteLog("二值化结束:", 1); 
                using (var engine = new TesseractEngine(@"./tessdata", "chi_sim", EngineMode.Default)) 
                { 
                    //using (var img = PixConverter.ToPix(imgsource)) 
                    //{ 
                        using (var page = engine.Process(tmpBmp, pageSegMode: engine.DefaultPageSegMode)) 
                        { 
                            log.WriteLog("Tesseract,获取中文开始", 1); 
                            ocrtext = page.GetText(); 
                            log.WriteLog("Tesseract,获取中文结束\r\n" + ocrtext, 1); 
                        } 
                    //} 
                } 
            } 
            return ocrtext; 
        } 
 
       public static Bitmap ToGrey(Bitmap img1)//灰化 
        { 
            for (int i = 0; i < img1.Width; i++) 
            { 
                for (int j = 0; j < img1.Height; j++) 
                { 
                    Color pixelColor = img1.GetPixel(i, j); 
                    //计算灰度值 
                    int grey = (int)(0.299 * pixelColor.R + 0.587 * pixelColor.G + 0.114 * pixelColor.B); 
                    Color newColor = Color.FromArgb(grey, grey, grey); 
                    img1.SetPixel(i, j, newColor); 
                } 
            } 
            Bitmap newImg = img1; 
            return newImg; 
        } 
 
        static Bitmap Thresholding(Bitmap img1) //二值化 
        { 
            int[] histogram = new int[256]; 
            int minGrayValue = 255, maxGrayValue = 0; 
            //求取直方图 
            for (int i = 0; i < img1.Width; i++) 
            { 
                for (int j = 0; j < img1.Height; j++) 
                { 
                    Color pixelColor = img1.GetPixel(i, j); 
                    histogram[pixelColor.R]++; 
                    if (pixelColor.R > maxGrayValue) maxGrayValue = pixelColor.R; 
                    if (pixelColor.R < minGrayValue) minGrayValue = pixelColor.R; 
                } 
            } 
            //迭代计算阀值 
            int threshold = -1; 
            int newThreshold = (minGrayValue + maxGrayValue) / 2; 
            for (int iterationTimes = 0; threshold != newThreshold && iterationTimes < 100; iterationTimes++) 
            { 
                threshold = newThreshold; 
                int lP1 = 0; 
                int lP2 = 0; 
                int lS1 = 0; 
                int lS2 = 0; 
                //求两个区域的灰度的平均值 
                for (int i = minGrayValue; i < threshold; i++) 
                { 
                    lP1 += histogram[i] * i; 
                    lS1 += histogram[i]; 
                } 
                int mean1GrayValue = (lP1 / lS1); 
                for (int i = threshold + 1; i < maxGrayValue; i++) 
                { 
                    lP2 += histogram[i] * i; 
                    lS2 += histogram[i]; 
                } 
                int mean2GrayValue = (lP2 / lS2); 
                newThreshold = (mean1GrayValue + mean2GrayValue) / 2; 
            } 
            //计算二值化 
            for (int i = 0; i < img1.Width; i++) 
            { 
                for (int j = 0; j < img1.Height; j++) 
                { 
                    Color pixelColor = img1.GetPixel(i, j); 
                    if (pixelColor.R > threshold) img1.SetPixel(i, j, Color.FromArgb(255, 255, 255)); 
                    else img1.SetPixel(i, j, Color.FromArgb(0, 0, 0)); 
                } 
            } 
 
            return img1; 
        } 
 
        public static string CompareImg(string errorInfo, ref bool isResultErr)//图片比较 
        { 
            string errorType = ""; 
            try 
            { 
                if (string.IsNullOrEmpty(errorInfo.Replace("\r", "").Replace("\n", ""))) 
                { 
                    return ""; 
                } 
                for (int i = 0; i < AppUtil.resultErrorTypeDb.Rows.Count; i++)//先匹配结果错误,如果无结果错再匹配过程错误 
                { 
                    errorType = ""; 
                    isResultErr = true;//是否是结果错误 
                    DataRow dr = AppUtil.resultErrorTypeDb.Rows[i]; 
                    string descps = dr["ERROR_DESCP"].ToString(); 
                    if (!string.IsNullOrEmpty(descps)) 
                    { 
                        string[] keys = descps.Split(';'); 
                        for (int k = 0; k < keys.Length; k++) 
                        { 
                            if (!errorInfo.Contains(keys[k]))                             
                            { 
                                isResultErr = false; 
                                break; 
                            } 
                            isResultErr = true; 
                        } 
                        if (isResultErr) 
                        { 
                            errorType = dr["ERROR_NAME"].ToString(); 
                            break; 
                        } 
                    } 
                } 
                if(string.IsNullOrEmpty(errorType)) 
                { 
                    isResultErr = false;//是否是结果错误 
                    for (int i = 0; i < AppUtil.processErrorTypeDb.Rows.Count; i++)//先匹配结果错误,如果无结果错再匹配过程错误 
                    { 
                        errorType = ""; 
                        DataRow dr = AppUtil.processErrorTypeDb.Rows[i]; 
                        string descps = dr["ERROR_DESCP"].ToString();                         
                        if (!string.IsNullOrEmpty(descps)) 
                        { 
                            errorType = dr["ERROR_NAME"].ToString(); 
                            string[] keys = descps.Split(';'); 
                            for (int k = 0; k < keys.Length; k++) 
                            { 
                                if (!errorInfo.Contains(keys[k])) 
                                { 
                                    errorType = ""; 
                                    break; 
                                }                                
                            } 
                            if (!string.IsNullOrEmpty(errorType)) 
                            { 
                                break; 
                            } 
                        } 
                    } 
                } 
            } 
            catch (Exception e) 
            { 
                log.WriteLog("CompareImg异常:" + e.Message, 1); 
                return "-1"; 
            } 
            return errorType; 
        } 
         
    } 
}