-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewControllerTextResult.cs
84 lines (76 loc) · 3.54 KB
/
ViewControllerTextResult.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
using System;
using Foundation;
using UIKit;
//using DocutainSdk;
using CoreFoundation;
using static AVFoundation.AVMetadataIdentifiers;
using Docutain.SDK.Xamarin.iOS;
namespace Docutain_SDK_Example_Xamarin_iOS
{
public class ViewControllerTextResult : UIViewController
{
private UIActivityIndicatorView loadingIndicator;
private UITextView textView;
private System.nfloat padding = 16.0f;
private string filePath;
public ViewControllerTextResult(string filePath)
{
this.filePath = filePath;
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
NavigationItem.BackButtonTitle = "";
Title = "Docutain SDK Example";
if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
View.BackgroundColor = UIColor.SystemBackground;
else
View.BackgroundColor = UIColor.White;
loadingIndicator = new UIActivityIndicatorView();
textView = new UITextView();
textView.Editable = false;
textView.Font = UIFont.SystemFontOfSize(UIFont.SystemFontSize);
View.AddSubview(textView);
textView.TranslatesAutoresizingMaskIntoConstraints = false;
textView.TopAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.TopAnchor, padding).Active = true;
textView.BottomAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.BottomAnchor, -padding).Active = true;
textView.LeadingAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.LeadingAnchor, padding).Active = true;
textView.TrailingAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.TrailingAnchor, -padding).Active = true;
loadingIndicator.HidesWhenStopped = true;
loadingIndicator.StartAnimating();
View.AddSubview(loadingIndicator);
loadingIndicator.TranslatesAutoresizingMaskIntoConstraints = false;
loadingIndicator.CenterXAnchor.ConstraintEqualTo(View.CenterXAnchor).Active = true;
loadingIndicator.CenterYAnchor.ConstraintEqualTo(View.CenterYAnchor).Active = true;
if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
LoadText();
else
Console.WriteLine("Text Recognition is only available for iOS 13+");
}
private void LoadText()
{
DispatchQueue.GetGlobalQueue(DispatchQueuePriority.Default).DispatchAsync(() =>
{
if (!string.IsNullOrEmpty(filePath))
{
//if a filePath is available it means we have imported a file. If so, we need to load it into the SDK first
if (!DocumentDataReader.LoadFile(NSUrl.FromFilename(filePath)))
{
//an error occurred, get the latest error message
Console.WriteLine($"DocumentDataReader.LoadFile failed with error: {DocutainSDK.LastError}");
return;
}
}
//get the text of all currently loaded pages
//if you want text of just one specific page, define the page number
//see https://docs.docutain.com/docs/Xamarin/textDetection for more details
string text = DocumentDataReader.GetText();
DispatchQueue.MainQueue.DispatchAsync(() =>
{
loadingIndicator.StopAnimating();
textView.Text = text;
});
});
}
}
}