/
ViewController.cs
188 lines (150 loc) · 4.38 KB
/
ViewController.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
using System;
using System.Text;
using System.Threading.Tasks;
using AVFoundation;
using CoreGraphics;
using Tesseract.iOS;
using UIKit;
using Xamarin.Essentials;
using static System.Math;
namespace MoveOCR
{
public partial class ViewController : UIViewController
{
public ViewController(IntPtr handle) : base(handle) { }
public override void ViewDidLoad()
{
base.ViewDidLoad();
OCR = new TesseractApi();
CaptureSession = new AVCaptureSession();
ImageOutput = new AVCapturePhotoOutput();
var cameraDevice = AVCaptureDevice.GetDefaultDevice(AVMediaType.Video);
var cameraInput = AVCaptureDeviceInput.FromDevice(cameraDevice);
CaptureSession.AddInput(cameraInput);
CaptureSession.AddOutput(ImageOutput);
SetupUI();
CaptureSession.StartRunning();
Camera = new CameraHandler();
Camera.FinishedProcessing += async delegate
{
PictureView.Image = new UIImage(Camera.Picture, 1f, UIImageOrientation.Right);
Capture = PictureView.Capture();
await InitReader();
};
OCRButton.TouchUpInside += async delegate
{
HandleButtonClick();
};
AlphaNumericSwitch.ValueChanged += async delegate
{
await SetOcrTextLabel();
};
// Selection slider Setup
SelectionBarSlider.TouchUpInside += async delegate
{
await InitReader();
};
SelectionBarSlider.TouchUpOutside += async delegate
{
await InitReader();
};
SelectionBarSlider.ValueChanged += delegate
{
var tempFrame = SelectionBarView.Frame;
tempFrame.Y = (SelectionBarSlider.Value * 92) + 22;
SelectionBarView.Frame = tempFrame;
};
}
public void SetupUI()
{
// Live Camera Feed
var layer = new AVCaptureVideoPreviewLayer(CaptureSession);
layer.VideoGravity = AVLayerVideoGravity.ResizeAspectFill;
layer.Frame = CameraView.Bounds;
CameraView.Layer.AddSublayer(layer);
// Selection Bar Options
SelectionBarView.Layer.BorderWidth = 3.5f;
SelectionBarView.Layer.BorderColor = new CGColor(246, 137, 27, 0.35f);
SelectionBarSlider.Transform = CGAffineTransform.MakeRotation((float)PI / 2);
}
void HandleButtonClick()
{
ToggleBusy(true);
if (OCRButton.TitleLabel.Text == "Take Picture")
{
using (var settings = AVCapturePhotoSettings.Create())
{
ImageOutput.CapturePhoto(settings, Camera);
}
OCRButton.SetTitle("Retake Picture", UIControlState.Normal);
}
else
{
PictureView.Image = null;
Capture = null;
OCRButton.SetTitle("Take Picture", UIControlState.Normal);
ToggleBusy(false);
}
}
async Task InitReader()
{
ToggleBusy(true);
if (!OCR.Initialized)
{
await OCR.Init("eng");
}
if (Capture != null)
{
var cropped = CropCapture();
await OCR.SetImage(cropped.AsJPEG().ToArray());
Result = OCR.Text;
FilteredResult = FilterAlphaNumeric(OCR.Text);
await SetOcrTextLabel();
}
ToggleBusy(false);
}
UIImage CropCapture()
{
var rect = new CGRect(new CGPoint(0, 0), SelectionBarView.Frame.Size);
var location = new CGPoint(0, 22 - SelectionBarView.Frame.Y);
UIGraphics.BeginImageContextWithOptions(rect.Size, false, Capture.CurrentScale);
Capture.Draw(location);
var cropped = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
return cropped;
}
string FilterAlphaNumeric(string text)
{
var result = new StringBuilder();
foreach (var ch in text)
{
if (char.IsLetterOrDigit(ch))
{
result.Append(ch);
}
}
return result.ToString();
}
async Task SetOcrTextLabel()
{
var result = AlphaNumericSwitch.On ? FilteredResult : Result;
if (!string.IsNullOrEmpty(result))
{
OCRTextField.Text = result;
await Clipboard.SetTextAsync(result);
}
}
void ToggleBusy(bool busy)
{
OCRButton.Enabled = !busy;
ActivitySpinner.Hidden = !busy;
}
string Result;
string FilteredResult;
UIImage Capture;
TesseractApi OCR;
CameraHandler Camera;
AVCaptureSession CaptureSession;
AVCapturePhotoOutput ImageOutput;
}
}