예제 #1
0
        void SaveDocument(SaveType type)
        {
            if (!SBSDK.IsLicenseValid())
            {
                Alert.ShowLicenseDialog(this);
                return;
            }

            Task.Run(delegate
            {
                var input  = adapter.GetDocumentUris().ToArray();
                var output = GetOutputUri(".pdf");

                if (type == SaveType.TIFF)
                {
                    output = GetOutputUri(".tiff");
                    // Please note that some compression types are only compatible for 1-bit encoded images (binarized black & white images)!
                    var options = new TiffOptions {
                        OneBitEncoded = true, Compression = TiffCompressionOptions.CompressionCcittfax4, Dpi = 250
                    };
                    bool success = SBSDK.WriteTiff(input, output, options);
                }
                else if (type == SaveType.OCR)
                {
                    var languages = SBSDK.GetOcrConfigs().InstalledLanguages.ToArray();

                    if (languages.Length == 0)
                    {
                        RunOnUiThread(delegate
                        {
                            Alert.Toast(this, "OCR languages blobs are not available");
                        });
                        return;
                    }
                    SBSDK.PerformOCR(input, languages, output);
                }
                else
                {
                    SBSDK.CreatePDF(input, output, ScanbotSDK.Xamarin.PDFPageSize.FixedA4);
                }

                Java.IO.File file = Copier.Copy(this, output);

                var intent = new Intent(Intent.ActionView, output);

                var authority = ApplicationContext.PackageName + ".provider";
                var uri       = FileProvider.GetUriForFile(this, authority, file);

                intent.SetDataAndType(uri, MimeUtils.GetMimeByName(file.Name));
                intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask);
                intent.AddFlags(ActivityFlags.GrantReadUriPermission | ActivityFlags.GrantWriteUriPermission);

                RunOnUiThread(delegate
                {
                    StartActivity(Intent.CreateChooser(intent, output.LastPathSegment));
                    Alert.Toast(this, "File saved to: " + output.Path);
                });
            });
        }
예제 #2
0
        void AssignOcrButtonsHandler()
        {
            performOcrButton        = FindViewById <Button>(Resource.Id.performOcrButton);
            performOcrButton.Click += delegate
            {
                if (!CheckScanbotSDKLicense())
                {
                    return;
                }
                if (!CheckDocumentImage())
                {
                    return;
                }

                performOcrButton.Post(() => {
                    performOcrButton.Text    = "Running OCR ... Please wait ...";
                    performOcrButton.Enabled = false;
                });

                Task.Run(() => {
                    try
                    {
                        var pdfOutputUri = GenerateRandomFileUrlInDemoTempStorage(".pdf");
                        var images       = new AndroidNetUri[] { documentImageUri }; // add more images for OCR here

                        // The SDK call is sync!
                        var result = SBSDK.PerformOCR(images, new [] { "en", "de" }, pdfOutputUri);
                        DebugLog("Recognized OCR text: " + result.RecognizedText);
                        DebugLog("Sandwiched PDF file created: " + pdfOutputUri);
                        ShowAlertDialog(result.RecognizedText, "OCR Result", () =>
                        {
                            OpenSharingDialog(pdfOutputUri, "application/pdf");
                        });
                    }
                    catch (Exception e)
                    {
                        ErrorLog("Error performing OCR", e);
                    }
                    finally
                    {
                        performOcrButton.Post(() => {
                            performOcrButton.Text    = "Perform OCR";
                            performOcrButton.Enabled = true;
                        });
                    }
                });
            };
        }
        partial void PerformOCRUpInside(UIButton sender)
        {
            if (!CheckScanbotSDKLicense())
            {
                return;
            }
            if (!CheckDocumentImageUrl())
            {
                return;
            }

            Task.Run(() =>
            {
                DebugLog("Performing OCR ...");
                var images = new NSUrl[] { documentImageUrl };
                var result = SBSDK.PerformOCR(images, new[] { "en", "de" });
                DebugLog("OCR result: " + result.RecognizedText);
                ShowMessage("OCR Text", result.RecognizedText);
            });
        }
예제 #4
0
        private void RecognizeText()
        {
            String status = "reading the recognized text";

            SpeakStatus(status);


            Task.Run(() => {
                try
                {
                    if (!CheckScanbotSDKLicense())
                    {
                        return;
                    }

                    var images = new AndroidNetUri[] { documentImageUri };
                    // SDK call is sync
                    var result = SBSDK.PerformOCR(images, new[] { "en", "de" });
                    DebugLog("Recognized OCR text: " + result.RecognizedText);

                    //switch to readbraille layout
                    Intent nextActivity = new Intent(this, typeof(ReadBraille));
                    nextActivity.PutExtra("text", result.RecognizedText);
                    if (nextActivity == null)
                    {
                        return;
                    }
                    StartActivity(nextActivity);
                }

                catch (Exception e)
                {
                    ErrorLog("Error performing OCR", e);
                }
            });
        }
예제 #5
0
        private void OnSaveButtonClick(object sender, EventArgs e)
        {
            var input = PageRepository.DocumentImageURLs;

            var docs  = NSSearchPathDirectory.DocumentDirectory;
            var nsurl = NSFileManager.DefaultManager.GetUrls(docs, NSSearchPathDomain.User)[0];

            var controller = UIAlertController.Create(Texts.save, Texts.SaveHow, UIAlertControllerStyle.ActionSheet);

            var title = "Oops!";
            var body  = "Something went wrong with saving your file. Please try again";

            if (!SBSDK.IsLicenseValid())
            {
                title = "Oops";
                body  = "Your license has expired";
                Alert.Show(this, title, body);
                return;
            }

            var pdf = CreateButton(Texts.save_without_ocr, delegate
            {
                var output = new NSUrl(nsurl.AbsoluteString + Guid.NewGuid() + ".pdf");
                SBSDK.CreatePDF(input, output, PDFPageSize.FixedA4);
                OpenDocument(output, false);
            });

            var ocr = CreateButton(Texts.save_with_ocr, delegate
            {
                var output    = new NSUrl(nsurl.AbsoluteString + Guid.NewGuid() + ".pdf");
                var languages = SBSDK.GetOcrConfigs().InstalledLanguages;
                try
                {
                    SBSDK.PerformOCR(input, languages.ToArray(), output);
                    OpenDocument(output, true);
                }
                catch (Exception ex)
                {
                    body = ex.Message;
                    Alert.Show(this, title, body);
                }
            });

            var tiff = CreateButton(Texts.Tiff, delegate
            {
                var output = new NSUrl(nsurl.AbsoluteString + Guid.NewGuid() + ".tiff");

                // Please note that some compression types are only compatible for 1-bit encoded images (binarized black & white images)!
                var options = new TiffOptions {
                    OneBitEncoded = true, Compression = TiffCompressionOptions.CompressionCcittfax4, Dpi = 250
                };

                bool success = SBSDK.WriteTiff(input, output, options);

                if (success)
                {
                    title = "Info";
                    body  = "TIFF file saved to: " + output.Path;
                }

                Alert.Show(this, title, body);
            });

            var cancel = CreateButton("Cancel", delegate { }, UIAlertActionStyle.Cancel);

            controller.AddAction(pdf);
            controller.AddAction(ocr);
            controller.AddAction(tiff);

            controller.AddAction(cancel);

            UIPopoverPresentationController presentationPopover = controller.PopoverPresentationController;

            if (presentationPopover != null)
            {
                presentationPopover.SourceView = View;
                presentationPopover.PermittedArrowDirections = UIPopoverArrowDirection.Up;
            }

            PresentViewController(controller, true, null);
        }