Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EPIC-4628] Xamarin DS 4.2.1 #127

Draft
wants to merge 6 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Libraries.txt
Original file line number Diff line number Diff line change
@@ -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:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
<ItemGroup>
<PackageReference Include="Xamarin.Forms" Version="5.0.0.2612" />
<PackageReference Include="ScanbotSDK.Xamarin.Forms">
<Version>4.2.0</Version>
<Version>4.2.1-alpha.2</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
<ItemGroup>
<PackageReference Include="Xamarin.Forms" Version="5.0.0.2612" />
<PackageReference Include="ScanbotSDK.Xamarin.Forms">
<Version>4.2.0</Version>
<Version>4.2.1-alpha.2</Version>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\iOS\Xamarin.iOS.CSharp.targets" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Xamarin.Forms" Version="5.0.0.2612" />
<PackageReference Include="ScanbotSDK.Xamarin.Forms" Version="4.2.0" />
<PackageReference Include="ScanbotSDK.Xamarin.Forms" Version="4.2.1-alpha.2" />
</ItemGroup>
<ItemGroup>
<Folder Include="Views\" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
<Version>2.1.2</Version>
</PackageReference>
<PackageReference Include="ScanbotSDK.Xamarin.Forms">
<Version>4.2.0</Version>
<Version>4.2.1-alpha.2</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
<Version>5.0.0.2612</Version>
</PackageReference>
<PackageReference Include="ScanbotSDK.Xamarin.Forms">
<Version>4.2.0</Version>
<Version>4.2.1-alpha.2</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
Expand Down
148 changes: 107 additions & 41 deletions Scanbot.SDK.Example.Forms/Pages/ImageResultsPage.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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; }
Expand All @@ -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;

Expand All @@ -48,7 +53,7 @@ public ImageResultsPage()
Spacing = 0,
Children = { List, BottomBar }
};

Content = new AbsoluteLayout
{
Children = { Loader, Stack }
Expand Down Expand Up @@ -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<string> { "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<FileImageSource>(),
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<string>{ "en", "de" });

// Using the default OCR option
var ocrConfig = OcrConfig.ScanbotOCR;

var result = await SBSDK.Operations.PerformOcrAsync(Pages.Instance.DocumentSources.OfType<FileImageSource>(), 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<string>{ "en", "de" });

// Using the default OCR option
var ocrConfig = OcrConfig.ScanbotOCR;

var result = await SBSDK.Operations.CreateSandwichPdfAsync(
Pages.Instance.DocumentSources.OfType<FileImageSource>(),
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()
Expand Down