/// <summary> /// Ocr识别 /// </summary> /// <param name="bytes"></param> /// <param name="ocrType"></param> /// <returns></returns> public string AIOcrDetect(byte[] bytes, OcrType ocrType = OcrType.GENERAL_BASIC) { string result = string.Empty; if (TcpManager.IsOnLine())//在线检测 { switch (ocrType) { case OcrType.GENERAL_BASIC: result = OcrDetector.Instance.GeneralBasic(bytes).ToString(); break; case OcrType.ACCURATE_BASIC: result = OcrDetector.Instance.AccurateBasic(bytes).ToString(); break; case OcrType.GENERAL_ENHANCED: result = OcrDetector.Instance.GeneralEnhanced(bytes).ToString(); break; case OcrType.HANDWRITING: result = OcrDetector.Instance.Handwriting(bytes).ToString(); break; default: break; } } return(result); }
protected void Page_Load(object sender, EventArgs e) { //IImageToText imageToText = new Tesseract(); IImageToText imageToText = new ImageToText(); string imgpath = Server.MapPath("/upload/10.jpg"); // text = imageToText.Transformation(imgpath, OcrType.Baidu); OcrType ocrType = OcrType.Baidu; if (!string.IsNullOrWhiteSpace(System.Configuration.ConfigurationManager.AppSettings["OCR_TYPE"])) { ocrType = (OcrType)int.Parse(System.Configuration.ConfigurationManager.AppSettings["OCR_TYPE"]); } text = imageToText.Transformation(imgpath, ocrType); }
static IEnumerable <OcrResult> AsOcrPairs(this object obj, string fileId, OcrType ocrType) { foreach (var propertyInfo in obj.GetType().GetProperties()) { if (typeof(IEnumerable).IsAssignableFrom(propertyInfo.PropertyType) == false || propertyInfo.PropertyType == typeof(string)) { yield return(new OcrResult(fileId) { KeyName = propertyInfo.Name, OcrValue = propertyInfo.GetValue(obj).ToString(), OcrType = ocrType }); } } }
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; context.Response.Charset = "utf-8"; HttpPostedFile file = context.Request.Files["file"]; string folder = context.Request["path"], id = context.Request["id"], value = context.Request["id"]; string uploadPath = HttpContext.Current.Server.MapPath(folder) + "\\"; if (file != null) { if (!Directory.Exists(uploadPath)) { Directory.CreateDirectory(uploadPath); } String fileName = file.FileName.Substring(file.FileName.LastIndexOf("\\") + 1, file.FileName.Length - 1 - file.FileName.LastIndexOf("\\")); ///取到当前时间的年、月、日、分、秒和毫秒的值,并使用字符串格式把它们组合成一个字符串 // String fileTime = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() // + DateTime.Now.Second.ToString() + DateTime.Now.Minute.ToString() // + DateTime.Now.Millisecond.ToString(); // String src = file.FileName.Substring(file.FileName.LastIndexOf(".") + 1, file.FileName.Length - file.FileName.LastIndexOf(".") - 1).ToLower(); //string fileName = fileTime + "." + src; file.SaveAs(uploadPath + fileName); string text = ""; try { IImageToText imageToText = new ImageToText(); OcrType ocrType = OcrType.Baidu; if (!string.IsNullOrWhiteSpace(System.Configuration.ConfigurationManager.AppSettings["OCR_TYPE"])) { ocrType = (OcrType)int.Parse(System.Configuration.ConfigurationManager.AppSettings["OCR_TYPE"]); } text = imageToText.Transformation(uploadPath + fileName, ocrType); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } //下面这句代码缺少的话,上传成功后上传队列的显示不会自动消失 context.Response.Write("{ \"id\": \"" + id + "\", \"status\": true, \"message\": \"上传成功!\", \"value\": \"" + fileName + "\", \"text\": \"" + Until.string2Json(text) + "\" }"); } else { context.Response.Write("{ \"id\": null, \"status\": false, \"message\"上传失败!\", \"value\": null, \"text\": null }"); } }
/// <summary> /// 返回转换结果 /// </summary> /// <param name="imgpath">图片路劲</param> /// <param name="ocrType">OCR模式,默认为百度</param> /// <param name="tesseractlanguage">如果是tesseract,注明语言chi_sim是中文,eng是英文,其他的是自定义</param> /// <returns></returns> public string Transformation(string imgpath, OcrType ocrType = OcrType.Baidu, string tesseractlanguage = "chi_sim") { string result = string.Empty; if (ocrType == OcrType.Baidu) { if (CustomCache.Exists("Baidu" + imgpath)) { result = CustomCache.Get <string>("Baidu" + imgpath); } else { result = BaiduOCR.Transformation(imgpath); CustomCache.add("Baidu" + imgpath, result); } } else if (ocrType == OcrType.Google) { result = new Tesseract(tesseractlanguage).Transformation(imgpath); } return(result); }
/// <summary> /// Sets to license plate. /// </summary> public void SetToLicensePlate() { CurrentOcrType = OcrType.LicensePlate; textOcrResult.text = ""; }
/// <summary> /// Sets to idcard. /// </summary> public void SetToIdcard() { CurrentOcrType = OcrType.Idcard; textOcrResult.text = ""; }
/// <summary> /// Sets to driving license. /// </summary> public void SetToDrivingLicense() { CurrentOcrType = OcrType.DrivingLicense; textOcrResult.text = ""; }
/// <summary> /// Sets to business license. /// </summary> public void SetToBusinessLicense() { CurrentOcrType = OcrType.BusinessLicense; textOcrResult.text = ""; }
/// <summary> /// Sets to general basic. /// </summary> public void SetToGeneralBasic() { CurrentOcrType = OcrType.GeneralBasic; textOcrResult.text = ""; }
/// <summary> /// Screens the shot read pixels.带参数的重载函数 /// </summary> /// <returns>The shot read pixels.</returns> /// <param name="ocrType">Ocr type.</param> private IEnumerator ScreenShot_ReadPixels(OcrType ocrType) { textOcrResult.text = ""; buttonTakePhoto.gameObject.SetActive(false); yield return(new WaitForEndOfFrame()); //读取像素 Texture2D tex = new Texture2D(Screen.width, Screen.height); tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0); tex.Apply(); //保存读取的结果 imageBytes = tex.EncodeToPNG(); yield return(new WaitForSeconds(0.01f)); //保存截图 // string path = Application.dataPath + "/ScreenShot_ReadPixels.png"; // System.IO.File.WriteAllBytes(path, tex.EncodeToPNG()); JObject result; switch (ocrType) { case OcrType.Bankcard: Bankcard(); break; case OcrType.BusinessLicense: BusinessLicense(); break; case OcrType.DrivingLicense: DrivingLicense(); break; case OcrType.GeneralBasic: GeneralBasic(); break; case OcrType.Idcard: Idcard(); break; case OcrType.LicensePlate: LicensePlate(); break; default: break; } TextTip.gameObject.SetActive(false); buttonClose.gameObject.SetActive(true); buttonTakePhoto.gameObject.SetActive(true); }