diff --git a/Libraries.txt b/Libraries.txt index a5b72c9..c850b6a 100644 --- a/Libraries.txt +++ b/Libraries.txt @@ -1,4 +1,4 @@ -Open Source libraries used in the Scanbot SDK for Xamarin and Xamarin.Forms version 4.2.0: +Open Source libraries used in the Scanbot SDK for Xamarin and Xamarin.Forms version 4.2.1: Xamarin.AndroidX: diff --git a/Native.Renderers.Example.Forms/Native.Renderers.Example.Forms.Android/Native.Renderers.Example.Forms.Android.csproj b/Native.Renderers.Example.Forms/Native.Renderers.Example.Forms.Android/Native.Renderers.Example.Forms.Android.csproj index 57b7dbc..167aeba 100644 --- a/Native.Renderers.Example.Forms/Native.Renderers.Example.Forms.Android/Native.Renderers.Example.Forms.Android.csproj +++ b/Native.Renderers.Example.Forms/Native.Renderers.Example.Forms.Android/Native.Renderers.Example.Forms.Android.csproj @@ -68,7 +68,7 @@ - 4.2.0 + 4.2.1-alpha.2 diff --git a/Native.Renderers.Example.Forms/Native.Renderers.Example.Forms.iOS/Native.Renderers.Example.Forms.iOS.csproj b/Native.Renderers.Example.Forms/Native.Renderers.Example.Forms.iOS/Native.Renderers.Example.Forms.iOS.csproj index 66180da..972a228 100644 --- a/Native.Renderers.Example.Forms/Native.Renderers.Example.Forms.iOS/Native.Renderers.Example.Forms.iOS.csproj +++ b/Native.Renderers.Example.Forms/Native.Renderers.Example.Forms.iOS/Native.Renderers.Example.Forms.iOS.csproj @@ -138,7 +138,7 @@ - 4.2.0 + 4.2.1-alpha.2 diff --git a/Native.Renderers.Example.Forms/Native.Renderers.Example.Forms/Native.Renderers.Example.Forms.csproj b/Native.Renderers.Example.Forms/Native.Renderers.Example.Forms/Native.Renderers.Example.Forms.csproj index 06ba3a4..8db0624 100644 --- a/Native.Renderers.Example.Forms/Native.Renderers.Example.Forms/Native.Renderers.Example.Forms.csproj +++ b/Native.Renderers.Example.Forms/Native.Renderers.Example.Forms/Native.Renderers.Example.Forms.csproj @@ -8,7 +8,7 @@ - + diff --git a/Scanbot.SDK.Example.Forms.Android/Scanbot.SDK.Example.Forms.Android.csproj b/Scanbot.SDK.Example.Forms.Android/Scanbot.SDK.Example.Forms.Android.csproj index 5b04616..39dba9c 100644 --- a/Scanbot.SDK.Example.Forms.Android/Scanbot.SDK.Example.Forms.Android.csproj +++ b/Scanbot.SDK.Example.Forms.Android/Scanbot.SDK.Example.Forms.Android.csproj @@ -72,7 +72,7 @@ 2.1.2 - 4.2.0 + 4.2.1-alpha.2 diff --git a/Scanbot.SDK.Example.Forms.iOS/Scanbot.SDK.Example.Forms.iOS.csproj b/Scanbot.SDK.Example.Forms.iOS/Scanbot.SDK.Example.Forms.iOS.csproj index 823563d..4c44482 100644 --- a/Scanbot.SDK.Example.Forms.iOS/Scanbot.SDK.Example.Forms.iOS.csproj +++ b/Scanbot.SDK.Example.Forms.iOS/Scanbot.SDK.Example.Forms.iOS.csproj @@ -150,7 +150,7 @@ 5.0.0.2612 - 4.2.0 + 4.2.1-alpha.2 diff --git a/Scanbot.SDK.Example.Forms/Pages/ImageResultsPage.cs b/Scanbot.SDK.Example.Forms/Pages/ImageResultsPage.cs index 6716cf8..7d5abc9 100644 --- a/Scanbot.SDK.Example.Forms/Pages/ImageResultsPage.cs +++ b/Scanbot.SDK.Example.Forms/Pages/ImageResultsPage.cs @@ -1,5 +1,8 @@ using System; +using System.Collections.Generic; using System.IO; +using System.Linq; +using System.Threading.Tasks; using ScanbotSDK.Xamarin; using ScanbotSDK.Xamarin.Forms; using Xamarin.Essentials; @@ -11,10 +14,12 @@ namespace Scanbot.SDK.Example.Forms { public class ImageResultsPage : ContentPage { + private const string PDF = "PDF", OCR = "Perform OCR", SandwichPDF = "Sandwiched PDF", TIFF = "TIFF (1-bit, B&W)"; + public StackLayout Stack { get; private set; } public ListView List { get; private set; } - + public BottomActionBar BottomBar { get; private set; } public ActivityIndicator Loader { get; set; } @@ -24,7 +29,7 @@ public ImageResultsPage() Title = "Image Results"; List = new ListView(); List.ItemTemplate = new DataTemplate(typeof(ImageResultCell)); - + List.RowHeight = 120; List.BackgroundColor = Color.White; @@ -48,7 +53,7 @@ public ImageResultsPage() Spacing = 0, Children = { List, BottomBar } }; - + Content = new AbsoluteLayout { Children = { Loader, Stack } @@ -123,61 +128,122 @@ async void OnAddButtonClick() async void OnSaveButtonClick() { - var parameters = new string[] {"PDF", "PDF with OCR", "TIFF (1-bit, B&W)" }; - string action = await DisplayActionSheet("Save Image as", "Cancel", null, parameters); + string action = await DisplayActionSheet("Save Image as", "Cancel", null, new[] { PDF, OCR, SandwichPDF, TIFF }); if (action == null || action.Equals("Cancel")) { return; } - (Content as AbsoluteLayout).RaiseChild(Loader); if (!SDKUtils.CheckLicense(this)) { return; } if (!SDKUtils.CheckDocuments(this, Pages.Instance.DocumentSources)) { return; } - if (action.Equals(parameters[0])) + try { - var fileUri = await SBSDK.Operations - .CreatePdfAsync(Pages.Instance.DocumentSources, PDFPageSize.A4); - ViewUtils.Alert(this, "Success: ", "Wrote documents to: " + fileUri.AbsolutePath); - } - else if (action.Equals(parameters[1])) - { - try - { - string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal); - string pdfFilePath = Path.Combine(path, Guid.NewGuid() + ".pdf"); - - var ocrConfig = SBSDK.Operations.OcrConfigs; - // Uncomment below code to use the old OCR approach. Use [OCRMode.Legacy] and set the required [InstalledLanguages] property. - //var languages = new List { "en", "de" }; - //var ocrConfig = new OcrConfigs - //{ - // InstalledLanguages = languages, - // OcrMode = OCRMode.Legacy, - // LanguageDataPath = ocrConfig.LanguageDataPath - //}; - - var result = await SBSDK.Operations.PerformOcrAsync(Pages.Instance.DocumentSources, ocrConfig, pdfFilePath); - // Or do something else with the result: result.Pages... - ViewUtils.Alert(this, "PDF with OCR layer stored: ", pdfFilePath); - } - catch (Exception error) + (Content as AbsoluteLayout).RaiseChild(Loader); + switch (action) { - ViewUtils.Alert(this, "Error", error.Message); + case PDF: + await GeneratePdfAsync(); + break; + case OCR: + await PerformOcrAsync(); + break; + case SandwichPDF: + await GenerateSandwichPdfAsync(); + break; + case TIFF: + await GenerateTiffAsync(); + break; + default: + break; } } - else if (action.Equals(parameters[2])) + catch (Exception ex) { - var fileUri = await SBSDK.Operations.WriteTiffAsync( - Pages.Instance.DocumentSources, - new TiffOptions { OneBitEncoded = true, Dpi = 300, Compression = TiffCompressionOptions.CompressionCcittT6 } - ); - ViewUtils.Alert(this, "Success: ", "Wrote documents to: " + fileUri.AbsolutePath); + // Making the error prettier. + var errorMessage = ex.Message.Substring(ex.Message.LastIndexOf(':')).Trim('{', '}'); + ViewUtils.Alert(this, "Error: ", $"An error occurred while saving the document: {errorMessage}"); + } + finally + { + (Content as AbsoluteLayout).LowerChild(Loader); } + } + + private async Task GeneratePdfAsync() + { + var fileUri = await SBSDK.Operations.CreatePdfAsync(Pages.Instance.DocumentSources.OfType(), + configuration: new PDFConfiguration + { + PageOrientation = PDFPageOrientation.Auto, + PageSize = PDFPageSize.A4, + PdfAttributes = new PDFAttributes + { + Author = "Scanbot User", + Creator = "ScanbotSDK", + Title = "ScanbotSDK PDF", + Subject = "Generating a normal PDF", + Keywords = new[] { "x-platform", "ios", "android" }, + } + }); + ViewUtils.Alert(this, "Success: ", "Wrote documents to: " + fileUri.AbsolutePath); + } - (Content as AbsoluteLayout).LowerChild(Loader); + private async Task PerformOcrAsync() + { + // NOTE: + // The default OCR engine is 'OcrConfig.ScanbotOCR' which is ML based. This mode doesn't expect the Langauges array. + // If you wish to use the previous engine please use 'OcrConfig.Tesseract(...)'. The Languages array is mandatory in this mode. + // Uncomment the below code to use the past legacy 'OcrConfig.Tesseract(...)' engine mode. + // var ocrConfig = OcrConfig.Tesseract(withLanguageString: new List{ "en", "de" }); + + // Using the default OCR option + var ocrConfig = OcrConfig.ScanbotOCR; + + var result = await SBSDK.Operations.PerformOcrAsync(Pages.Instance.DocumentSources.OfType(), configuration: ocrConfig); + + // You can access the results with: result.Pages + ViewUtils.Alert(this, "OCR", result.Text); + } + + private async Task GenerateSandwichPdfAsync() + { + // NOTE: + // The default OCR engine is 'OcrConfig.ScanbotOCR' which is ML based. This mode doesn't expect the Langauges array. + // If you wish to use the previous engine please use 'OcrConfig.Tesseract(...)'. The Languages array is mandatory in this mode. + // Uncomment the below code to use the past legacy 'OcrConfig.Tesseract(...)' engine mode. + // var ocrConfig = OcrConfig.Tesseract(withLanguageString: new List{ "en", "de" }); + + // Using the default OCR option + var ocrConfig = OcrConfig.ScanbotOCR; + + var result = await SBSDK.Operations.CreateSandwichPdfAsync( + Pages.Instance.DocumentSources.OfType(), + new PDFConfiguration + { + PageOrientation = PDFPageOrientation.Auto, + PageSize = PDFPageSize.A4, + PdfAttributes = new PDFAttributes + { + Author = "Scanbot User", + Creator = "ScanbotSDK", + Title = "ScanbotSDK PDF", + Subject = "Generating a sandwiched PDF", + Keywords = new[] { "x-platform", "ios", "android" }, + } + }, ocrConfig); + + ViewUtils.Alert(this, "PDF with OCR layer stored: ", result.AbsolutePath); + } + + private async Task GenerateTiffAsync() + { + var fileUri = await SBSDK.Operations.WriteTiffAsync( + Pages.Instance.DocumentSources, + new TiffOptions { OneBitEncoded = true, Dpi = 300, Compression = TiffCompressionOptions.CompressionCcittT6 }); + ViewUtils.Alert(this, "Success: ", "Wrote documents to: " + fileUri.AbsolutePath); } private async void OnDeleteButtonClick()