public async void Sample3AllModes()
        {
            foreach (var engineMode in Enum.GetValues(typeof(OcrEngineMode)))
            {
                Console.WriteLine("Engine mode: {0}", engineMode);
                await _api.Init("eng", (OcrEngineMode)engineMode);

                foreach (var segmentationMode in Enum.GetValues(typeof(PageSegmentationMode)))
                {
                    _api.SetPageSegmentationMode((PageSegmentationMode)segmentationMode);
                    using (var stream = LoadSample("sample3.png")) {
                        var result = await _api.SetImage(stream);

                        Assert.IsTrue(result);
                        Console.WriteLine("Segmentation mode: {0}, result: \"{1}\"", segmentationMode,
                                          _api.Text.Replace("\n", " "));
                    }
                }
            }
        }
Ejemplo n.º 2
0
        protected override async void OnActivityResult(int requestCode, Android.App.Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);
            ProgressDialog progress = new ProgressDialog(this);

            progress.Indeterminate = true;
            progress.SetProgressStyle(ProgressDialogStyle.Spinner);
            progress.SetMessage("Kraunama...");
            progress.SetCancelable(false);
            progress.Show();
            try
            {
                System.Console.WriteLine("OnActivityLoad eventas");
                Intent mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
                Uri    contentUri      = Uri.FromFile(Failosas._file);
                mediaScanIntent.SetData(contentUri);
                // SendBroadcast(mediaScanIntent);

                int height = Android.Content.Res.Resources.System.DisplayMetrics.HeightPixels;
                int width  = Android.Content.Res.Resources.System.DisplayMetrics.WidthPixels;
                Failosas.bitmap = Failosas._file.Path.LoadAndResizeBitmap(width, height);
                if (Failosas.bitmap != null)
                {
                    System.Console.WriteLine("Got bitmap, doing OCR...");

                    TesseractApi api = new TesseractApi(mContext, AssetsDeployment.OncePerInitialization);

                    await api.Init("eng");

                    api.SetPageSegmentationMode(Tesseract.PageSegmentationMode.SparseText);
                    await api.SetImage(Failosas._file.Path);

                    RunOnUiThread(() => {
                        string text = api.Text;
                        System.Console.Error.WriteLine("Gautas text: " + text);
                        Android.Widget.Toast.MakeText(this, "Pagaliau! :D", Android.Widget.ToastLength.Long).Show();
                        progress.Hide();
                        Android.Support.V7.App.AlertDialog.Builder alert = new Android.Support.V7.App.AlertDialog.Builder(this);
                        alert.SetTitle("Gavom teksta");
                        alert.SetMessage(text);
                        alert.SetPositiveButton("nieko gero! :D", (senderAlert, args) =>
                        {
                            Android.Widget.Toast.MakeText(this, "Gerai kad supratai!", Android.Widget.ToastLength.Short).Show();
                        });
                        Dialog dialog = alert.Create();
                        dialog.Show();
                    });
                }
                else
                {
                    Android.Widget.Toast.MakeText(this, "Blogas image!", Android.Widget.ToastLength.Short).Show();
                }
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("Klaida darant OCR: " + ex.ToString());
                progress.Hide();
                Android.Widget.Toast.MakeText(this, "Klaida darant OCR!", Android.Widget.ToastLength.Long).Show();
            }
            finally
            {
                GC.Collect();
                progress.Hide();
            }
        }