public static async Task <OCRData> Detect(IEditableImage Img, Context Ctx, int RectWidth, int RectHeight) { if (TesAPI == null) { TesAPI = new TesseractApi(Ctx, AssetsDeployment.OncePerInitialization); await TesAPI.Init("eng"); TesAPI.SetVariable("tessedit_char_whitelist", "0123456789kmKM"); } OCRData Result = new OCRData(); Result.PngOriginal = Img.ToPng(); // Crop the detection region Img = Img.Crop((Img.Width / 2) - (RectWidth / 2), (Img.Height / 2) - (RectHeight / 2), RectWidth, RectHeight).ToMonochrome(); Result.PngCropped = Img.ToPng(); using (MemoryStream PngImage = new MemoryStream()) { using (Bitmap Pic = ProcessImage((Bitmap)Img.GetNativeImage())) await Pic.CompressAsync(Bitmap.CompressFormat.Png, 100, PngImage); PngImage.Seek(0, SeekOrigin.Begin); await TesAPI.SetImage(PngImage); Result.PngProcessed = PngImage.ToArray(); } Result.Text = TesAPI.Text; return(Result); }
async Task OnPicture(IEditableImage Img) { ShowLabel("Processing..."); int W = Img.Width; int H = Img.Height; OCRData OCRData = await OCR.Detect(Img, ApplicationContext, (int)(W * 0.52f), (int)(H * 0.13f)); OCR.SaveDebug(OCRData); if (OCRData.TryParseKM(out int KM)) { string KMFormat = string.Format("{0} km", KM); ShowLabel(KMFormat); RunOnUiThread(() => { AlertDialog.Builder AlertBuilder = new AlertDialog.Builder(this); AlertBuilder.SetTitle("Success"); AlertBuilder.SetMessage("Use the following? " + KMFormat); AlertBuilder.SetPositiveButton("Yes", (S, E) => { RunOnUiThread(() => { ShowLabel("Fetching location..."); Kilometers = KM; DoBeginLocoVoznja = true; LocationManager LocMgr = (LocationManager)GetSystemService(LocationService); LocMgr.RequestSingleUpdate(LocationManager.GpsProvider, this, Looper.MainLooper); }); }); AlertBuilder.SetNegativeButton("No", (S, E) => { ShowLabel("User cancelled"); }); AlertBuilder.Show(); }); } else { ShowLabel(null); ShowInfoDialog("Info", string.Format("Failed to parse km, got '{0}'", OCRData.Text ?? "none")); } }
public static void SaveDebug(OCRData OCRData) { string FolderPath = AEnv.ExternalStorageDirectory.AbsolutePath; FolderPath = FilePath.Combine(FolderPath, "LocoVoznja_OCR"); if (!Directory.Exists(FolderPath)) { Directory.CreateDirectory(FolderPath); } string FName = FilePath.Combine(FolderPath, Utils.GetCurrentTimeName() + ".zip"); using (FileStream FStream = new FileStream(FName, FileMode.Create)) { ZipEntry Orig = new ZipEntry("orig.png", OCRData.PngOriginal); ZipEntry Crop = new ZipEntry("crop.png", OCRData.PngCropped); ZipEntry Proc = new ZipEntry("proc.png", OCRData.PngProcessed); ZipEntry Info = new TextZipEntry("info.txt", OCRData.CreateInfoString()); ZipUtils.CreateZip(FStream, Orig, Crop, Proc, Info); } }