示例#1
0
 static string path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "tessdata\\";//语言包位置
 /// <summary> 实例化识别引擎对象
 /// 实例化识别引擎对象
 /// </summary>
 /// <param name="lang"></param>
 /// <param name="mode"></param>
 /// <returns></returns>
 //static bool InitOcr(string lang)
 //{
 //    try
 //    {
 //        if (_ocr != null)
 //        {
 //            _ocr.Dispose();
 //            _ocr = null;
 //        }
 //        _ocr = new Tesseract(path, lang, OcrEngineMode.Default);
 //        return true;
 //    }
 //    catch (Exception e)
 //    {
 //        _ocr = null;
 //        NLogerHelper.Error(e.Message, "Failed to initialize tesseract OCR engine");
 //        return false;
 //    }
 //}
 /// <summary>识别MAT对象文本
 /// 识别MAT对象文本
 /// </summary>
 /// <param name="source"></param>
 /// <returns></returns>
 static public string OcrImage(Mat source, string lang)
 {
     try {
         var    _ocr      = new Tesseract(path, lang, OcrEngineMode.Default);
         Mat    result    = new Mat();
         string ocredText = OcrImage(_ocr, source, result);
         NLogerHelper.Info("ocredText:" + ocredText);//识别出来的文本
         //NLogerHelper.Info("hocrTextBox:" + _ocr.GetHOCRText());//xml
         return(TxtUtil.ReadClearContent(ocredText));
     } catch (Exception ex)
     {
         NLogerHelper.Error(ex.Message, "Failed to initialize tesseract OCR engine");
         return(string.Empty);
     }
 }
        public async Task <IHttpActionResult> CheckCard()
        {
            // 文件的临时处理文件夹
            var temp = Guid.NewGuid().ToString("N");

            if (!Directory.Exists(folder + temp))
            {
                Directory.CreateDirectory(folder + temp);
            }
            // 拿到接口传进来的文件,数据库记录的字段信息
            var FrontPath      = string.Empty;
            var Flag           = 0;
            var frontFlag      = 0;
            var reserveFlag    = 0;
            var CreateDate     = DateTime.Now;
            var IdentityNumber = string.Empty;
            var Name           = string.Empty;
            var Sex            = string.Empty;
            var Nation         = string.Empty;
            var BirthDay       = string.Empty;
            var Address        = string.Empty;
            var Institution    = string.Empty;
            var ValidityDate   = string.Empty;
            var ReversePath    = string.Empty;

            // 取出照片信息,并保存
            #region 取出照片信息,并保存
            var frontFile = HttpContext.Current.Request.Files["front"];
            var fronttask = Task.Run(() =>
            {
                if (frontFile != null)
                {
                    var frontinfo = Front(frontFile, temp);
                    if (frontinfo != null)
                    {
                        FrontPath      = frontinfo.FrontPath;
                        IdentityNumber = frontinfo.IdentityNumber;
                        Name           = frontinfo.Name;
                        Sex            = frontinfo.Sex;
                        Nation         = frontinfo.Nation;
                        BirthDay       = frontinfo.BirthDay;
                        Address        = frontinfo.Address;
                        frontFlag      = 1;
                    }
                }
            });
            //反面
            var reverseFile = HttpContext.Current.Request.Files["reverse"];
            var reversetask = Task.Run(() =>
            {
                if (reverseFile != null)
                {
                    var reverseinfo = Reverse(reverseFile, temp);
                    if (reverseinfo != null)
                    {
                        Institution  = reverseinfo.Institution;
                        ValidityDate = reverseinfo.ValidityDate;
                        ReversePath  = reverseinfo.ReversePath;
                        reserveFlag  = 1;
                    }
                }
            });
            try
            {
                await Task.WhenAll(fronttask, reversetask);
            }
            catch (Exception ex)
            {
                NLogerHelper.Error(ex.Message);
            }

            if (frontFlag > 0 && reserveFlag > 0)
            {
                Flag = 3;
            }
            else if (frontFlag > 0 && reserveFlag == 0)
            {
                Flag = 1;
            }
            else if (frontFlag == 0 && reserveFlag > 0)
            {
                Flag = 2;
            }
            // 保存到数据库
            try
            {
                if (Flag > 0)
                {
                    var sql = @"insert into identity (FrontPath,Flag,CreateDate,IdentityNumber,Name,Sex,Nation,BirthDay,Address,Institution,ValidityDate,ReversePath) values (@FrontPath,@Flag,@CreateDate,@IdentityNumber,@Name,@Sex,@Nation,@BirthDay,@Address,@Institution,@ValidityDate,@ReversePath)";
                    MySqlParameter[] parameters = new MySqlParameter[] {
                        new MySqlParameter("@FrontPath", FrontPath),
                        new MySqlParameter("@Flag", Flag),
                        new MySqlParameter("@CreateDate", CreateDate),
                        new MySqlParameter("@IdentityNumber", IdentityNumber),
                        new MySqlParameter("@Name", Name),
                        new MySqlParameter("@Sex", Sex),
                        new MySqlParameter("@Nation", Nation),
                        new MySqlParameter("@BirthDay", BirthDay),
                        new MySqlParameter("@Address", Address),
                        new MySqlParameter("@Institution", Institution),
                        new MySqlParameter("@ValidityDate", ValidityDate),
                        new MySqlParameter("@ReversePath", ReversePath)
                    };
                    var r = Common.MySqlHelper.ExecuteNonQuery(sql, parameters, CommandType.Text);
                    if (r)
                    {
                        List <Detail> data = new List <Detail>();
                        data.Add(new Detail {
                            Key = "姓名", Value = Name
                        });
                        data.Add(new Detail {
                            Key = "性别", Value = Sex
                        });
                        //data.Add(new Detail { Key = "民族", Value = Nation });
                        data.Add(new Detail {
                            Key = "生日", Value = BirthDay
                        });
                        data.Add(new Detail {
                            Key = "住址", Value = Address
                        });
                        data.Add(new Detail {
                            Key = "号码", Value = IdentityNumber
                        });
                        data.Add(new Detail {
                            Key = "签发机关", Value = Institution
                        });
                        data.Add(new Detail {
                            Key = "有效期限", Value = ValidityDate
                        });
                        return(Json(new
                        {
                            Code = 200,
                            Msg = "解析完成!(识别效果不理想的影响因素有很多,比如:光线,拍摄角度,有遮挡物,图片尺寸等,可调整图片质量重新识别提高识别率!)",
                            Data = data
                        }));
                    }
                    else
                    {
                        return(Json(new
                        {
                            Code = 10001,
                            Msg = "识别信息存储失败,可稍后重试!"
                        }));
                    }
                }
                else
                {
                    return(Json(new
                    {
                        Code = 10000,
                        Msg = "请至少上传一张符合图片格式的身份证照片!"
                    }));
                }
            }
            catch (Exception ex)
            {
                NLogerHelper.Info(ex.Message);
                return(Json(new
                {
                    Code = 10001,
                    Msg = "上传异常!"
                }));
            }
            #endregion
        }