-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathViewController.cs
204 lines (170 loc) · 9.3 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using CoreFoundation;
using UIKit;
using Scandit.DataCapture.Barcode.Capture;
using Scandit.DataCapture.Barcode.Data;
using Scandit.DataCapture.Barcode.UI.Overlay;
using Scandit.DataCapture.Core.Capture;
using Scandit.DataCapture.Core.Common.Feedback;
using Scandit.DataCapture.Core.Data;
using Scandit.DataCapture.Core.Source;
using Scandit.DataCapture.Core.UI;
using Scandit.DataCapture.Core.UI.Style;
using Scandit.DataCapture.Core.UI.Viewfinder;
namespace BarcodeCaptureRejectSample
{
public partial class ViewController : UIViewController, IBarcodeCaptureListener
{
// Enter your Scandit License key here.
// Your Scandit License key is available via your Scandit SDK web account.
public const string SCANDIT_LICENSE_KEY = "-- ENTER YOUR SCANDIT LICENSE KEY HERE --";
private DataCaptureContext dataCaptureContext;
private Camera camera;
private DataCaptureView dataCaptureView;
private BarcodeCapture barcodeCapture;
private BarcodeCaptureOverlay overlay;
private readonly Feedback feedback = Feedback.DefaultFeedback;
private readonly BarcodeCaptureOverlayStyle overlayStyle = BarcodeCaptureOverlayStyle.Frame;
public ViewController (IntPtr handle) : base (handle)
{
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
this.InitializeAndStartBarcodeScanning();
}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
this.barcodeCapture.Enabled = true;
// Switch camera on to start streaming frames. The camera is started asynchronously and will
// take some time to completely turn on.
this.camera?.SwitchToDesiredStateAsync(FrameSourceState.On);
}
public override void ViewWillDisappear(bool animated)
{
base.ViewWillDisappear(animated);
// Switch camera off to stop streaming frames. The camera is stopped asynchronously and will
// take some time to completely turn off. Until it is completely stopped, it is still possible
// to receive further results, hence it's a good idea to first disable barcode capture as well.
this.camera?.SwitchToDesiredStateAsync(FrameSourceState.Off);
}
public override void DidReceiveMemoryWarning ()
{
base.DidReceiveMemoryWarning ();
}
protected void InitializeAndStartBarcodeScanning()
{
// Create data capture context using your license key.
this.dataCaptureContext = DataCaptureContext.ForLicenseKey(SCANDIT_LICENSE_KEY);
// Use the default camera and set it as the frame source of the context.
// The camera is off by default and must be turned on to start streaming frames to the data
// capture context for recognition.
// See resumeFrameSource and pauseFrameSource below.
this.camera = Camera.GetDefaultCamera();
if (this.camera != null)
{
// Use the settings recommended by barcode capture.
this.camera.ApplySettingsAsync(BarcodeCapture.RecommendedCameraSettings);
this.dataCaptureContext.SetFrameSourceAsync(this.camera);
}
BarcodeCaptureSettings barcodeCaptureSettings = BarcodeCaptureSettings.Create();
// The settings instance initially has all types of barcodes (symbologies) disabled.
// For the purpose of this sample we enable the QR symbology. In your own app ensure
// that you only enable the symbologies that your app requires as every additional
// enabled symbology has an impact on processing times.
barcodeCaptureSettings.EnableSymbology(Symbology.Qr, true);
// Create new barcode capture mode with the settings from above.
this.barcodeCapture = BarcodeCapture.Create(this.dataCaptureContext, barcodeCaptureSettings);
// By default, every time a barcode is scanned, a sound (if not in silent mode) and a
// vibration are played. In the following we are setting a success feedback without sound
// and vibration.
this.barcodeCapture.Feedback.Success = new Feedback(vibration: null, sound: null);
// Register self as a listener to get informed whenever a new barcode got recognized.
this.barcodeCapture.AddListener(this);
// To visualize the on-going barcode capturing process on screen, setup a data capture view
// that renders the camera preview. The view must be connected to the data capture context.
this.dataCaptureView = DataCaptureView.Create(this.dataCaptureContext, this.View.Bounds);
this.dataCaptureView.AutoresizingMask = UIViewAutoresizing.FlexibleHeight |
UIViewAutoresizing.FlexibleWidth;
this.View.AddSubview(this.dataCaptureView);
this.overlay = BarcodeCaptureOverlay.Create(this.barcodeCapture, this.dataCaptureView, this.overlayStyle);
this.overlay.Viewfinder = RectangularViewfinder.Create(RectangularViewfinderStyle.Square, RectangularViewfinderLineStyle.Light);
}
public void ShowResult(string result)
{
DispatchQueue.MainQueue.DispatchAsync(() => {
UIAlertController alert = UIAlertController.Create(result, message: null, preferredStyle: UIAlertControllerStyle.Alert);
var action = UIAlertAction.Create("OK", UIAlertActionStyle.Default, (UIAlertAction) => {
this.barcodeCapture.Enabled = true;
});
alert.AddAction(action);
this.PresentViewController(alert, animated: true, completionHandler: () => { });
});
}
#region IBarcodeCaptureListener
public void OnBarcodeScanned(BarcodeCapture barcodeCapture, BarcodeCaptureSession session, IFrameData frameData)
{
if (session.NewlyRecognizedBarcode == null)
{
return;
}
var barcode = session.NewlyRecognizedBarcode;
// If the code scanned doesn't start with "09:", we will just ignore it and continue
// scanning.
if (barcode.Data?.StartsWith("09:") == false)
{
// We temporarily change the brush, used to highlight recognized barcodes, to a
// transparent brush.
this.overlay.Brush = Brush.TransparentBrush;
return;
}
// If the code is accepted, we want to make sure to use
// a brush to highlight the code.
this.overlay.Brush = BarcodeCaptureOverlay.DefaultBrushForStyle(this.overlayStyle);
// We also want to emit a feedback (vibration and, if enabled, sound).
this.feedback.Emit();
// Stop recognizing barcodes for as long as we are displaying the result. There won't be any new
// results until the capture mode is enabled again. Note that disabling the capture mode does
// not stop the camera, the camera continues to stream frames until it is turned off.
this.barcodeCapture.Enabled = false;
// If you don't want the codes to be scanned more than once, consider setting the codeDuplicateFilter when
// creating the barcode capture settings to -1.
// You can set any other value (e.g. 500) to set a fixed timeout and override the smart behaviour enabled
// by default.
// Get the human readable name of the symbology and assemble the result to be shown.
string symbology = SymbologyDescription.Create(barcode.Symbology).ReadableName;
string result = string.Format("Scanned {0} ({1})", barcode.Data, symbology);
this.ShowResult(result);
// Dispose the frame when you have finished processing it. If the frame is not properly disposed,
// different issues could arise, e.g. a frozen, non-responsive, or "severely stuttering" video feed.
frameData.Dispose();
}
public void OnSessionUpdated(BarcodeCapture barcodeCapture, BarcodeCaptureSession session, IFrameData frameData)
{
// Dispose the frame when you have finished processing it. If the frame is not properly disposed,
// different issues could arise, e.g. a frozen, non-responsive, or "severely stuttering" video feed.
frameData.Dispose();
}
public void OnObservationStarted(BarcodeCapture barcodeCapture)
{
}
public void OnObservationStopped(BarcodeCapture barcodeCapture)
{
}
#endregion
}
}