-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImageListController.cs
194 lines (164 loc) · 7.25 KB
/
ImageListController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using ReadyToUseUI.iOS.Repository;
using ReadyToUseUI.iOS.Utils;
using ReadyToUseUI.iOS.View;
using ReadyToUseUI.iOS.View.Collection;
using ReadyToUseUI.iOS.Models;
using ScanbotSDK.iOS;
namespace ReadyToUseUI.iOS.Controller
{
public class ImageListController : UIViewController
{
public ImageCollectionView ContentView { get; private set; }
public override void ViewDidLoad()
{
base.ViewDidLoad();
ContentView = new ImageCollectionView();
View = ContentView;
Title = "Scanned documents";
LoadPages();
var saveButton = new UIBarButtonItem(Texts.save, UIBarButtonItemStyle.Done, OnSaveButtonClick);
NavigationItem.SetRightBarButtonItem(saveButton, false);
}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
LoadPages();
ContentView.Collection.Selected += OnImageSelected;
}
public override void ViewWillDisappear(bool animated)
{
base.ViewWillDisappear(animated);
ContentView.Collection.Selected -= OnImageSelected;
}
void LoadPages()
{
ContentView.Collection.Pages.Clear();
ContentView.Collection.Pages.AddRange(PageRepository.Items);
ContentView.Collection.ReloadData();
}
void OnImageSelected(object sender, CollectionEventArgs e)
{
PageRepository.Current = e.Page;
var controller = new ProcessingController();
NavigationController.PushViewController(controller, true);
}
private void OnSaveButtonClick(object sender, EventArgs e)
{
if (PageRepository.DocumentImageURLs.Length == 0)
return;
var input = PageRepository.DocumentImageURLs;
var docs = NSSearchPathDirectory.DocumentDirectory;
var url = NSFileManager.DefaultManager.GetUrls(docs, NSSearchPathDomain.User)[0];
var alertController = UIAlertController.Create(Texts.save, Texts.SaveHow, UIAlertControllerStyle.ActionSheet);
if (!ScanbotSDKGlobal.IsLicenseValid)
{
var title = "Oops";
var body = "Your license has expired";
Alert.Show(this, title, body);
return;
}
var pdf = CreateButton(Texts.save_without_ocr, (action) => _ = CreatePDFAsync(input, url));
var ocr = CreateButton(Texts.save_with_ocr, (action) => _ = PerformOCRAndCreatePDFAsync(input, url));
var tiff = CreateButton(Texts.Tiff, (action) => WriteTIFF(input, url));
var cancel = CreateButton("Cancel", delegate { }, UIAlertActionStyle.Cancel);
alertController.AddAction(pdf);
alertController.AddAction(ocr);
alertController.AddAction(tiff);
alertController.AddAction(cancel);
UIPopoverPresentationController presentationPopover = alertController.PopoverPresentationController;
if (presentationPopover != null)
{
presentationPopover.SourceView = View;
presentationPopover.PermittedArrowDirections = UIPopoverArrowDirection.Up;
}
PresentViewController(alertController, true, null);
}
private async Task CreatePDFAsync(NSUrl[] inputUrls, NSUrl outputUrl)
{
try
{
var outputPdfUrl = await DocumentUtilities.CreatePDFAsync(inputUrls, outputUrl, SBSDKPDFRendererPageSize.A4, SBSDKPDFRendererPageOrientation.Auto, ScanbotSDKUI.DefaultImageStoreEncrypter);
if (outputPdfUrl != null)
{
OpenDocument(outputPdfUrl, false);
}
else
{
ShowErrorAlert();
}
}
catch (Exception ex)
{
Alert.Show(this, "Create PDF", ex.Message);
}
}
private async Task PerformOCRAndCreatePDFAsync(NSUrl[] inputUrls, NSUrl outputUrl)
{
var recognitionMode = SBSDKOpticalCharacterRecognitionMode.Ml;
// This is the new OCR configuration with ML which doesn't require the languages.
SBSDKOpticalCharacterRecognizerConfiguration ocrConfiguration = SBSDKOpticalCharacterRecognizerConfiguration.MlConfiguration;
// to use legacy configuration we have to pass the installed languages.
if (recognitionMode == SBSDKOpticalCharacterRecognitionMode.Legacy)
{
var installedLanguages = SBSDKResourcesManager.InstalledLanguages;
ocrConfiguration = SBSDKOpticalCharacterRecognizerConfiguration.LegacyConfigurationWithLanguages(installedLanguages);
}
SBSDKOpticalCharacterRecognizer recognizer = new SBSDKOpticalCharacterRecognizer(ocrConfiguration);
try
{
// Please check the default parameters
var (ocrResult, outputPdfUrl) = await DocumentUtilities.PerformOCRAsync(ocrRecognizer: recognizer, inputUrls: inputUrls, outputUrl: outputUrl, shouldGeneratePdf: true, encrypter: ScanbotSDKUI.DefaultImageStoreEncrypter);
if (ocrResult != null)
{
OpenDocument(outputPdfUrl, true, ocrResult.RecognizedText);
}
else
{
ShowErrorAlert();
}
}
catch (Exception ex)
{
Alert.Show(this, "Perform OCR", ex.Message);
}
}
private void WriteTIFF(NSUrl[] inputUrls, NSUrl outputUrl)
{
// Please note that some compression types are only compatible for 1-bit encoded images (binarized black & white images)!
var options = SBSDKTIFFImageWriterParameters.DefaultParametersForBinaryImages();
options.Binarize = true;
options.Compression = SBSDKTIFFImageWriterCompressionOptions.Ccittfax4;
options.Dpi = 250;
var (success, outputTiffUrl) = DocumentUtilities.CreateTIFF(options, inputUrls, outputUrl, ScanbotSDKUI.DefaultImageStoreEncrypter);
if (success)
{
var title = "Write TIFF";
var body = "TIFF file saved to: " + outputTiffUrl;
Alert.Show(this, title, body);
}
else
{
ShowErrorAlert();
}
}
void OpenDocument(NSUrl uri, bool ocr, string ocrResult = null)
{
InvokeOnMainThread(() =>
{
var controller = new PdfViewController(uri, ocr, ocrResult);
NavigationController.PushViewController(controller, true);
});
}
UIAlertAction CreateButton(string text, Action<UIAlertAction> action,
UIAlertActionStyle style = UIAlertActionStyle.Default)
{
return UIAlertAction.Create(text, style, action);
}
private void ShowErrorAlert()
{
var title = "Oops!";
var body = "Something went wrong with saving your file. Please try again";
Alert.Show(this, title, body);
}
}
}