@xbone-3/cordova-plugin-mlkit-barcodescanner 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,183 @@
1
+ @import MLKitBarcodeScanning;
2
+
3
+ #import "CDViOSScanner.h"
4
+
5
+ @class UIViewController;
6
+
7
+ @interface CDViOSScanner ()
8
+ {
9
+ NSInteger _previousStatusBarStyle;
10
+ UIInterfaceOrientation _previousOrientation;
11
+ }
12
+ @end
13
+
14
+
15
+ @implementation CDViOSScanner
16
+
17
+ - (void)pluginInitialize
18
+ {
19
+ _previousStatusBarStyle = -1;
20
+ _previousOrientation = UIInterfaceOrientationUnknown;
21
+ NSString *beepSoundPath = [[NSBundle mainBundle] pathForResource:@"beep" ofType:@"caf"];
22
+ NSURL *beepSoundUrl = [NSURL fileURLWithPath:beepSoundPath];
23
+ self->_player = [[AVAudioPlayer alloc] initWithContentsOfURL:beepSoundUrl
24
+ error:nil];
25
+ }
26
+
27
+ - (void)startScan:(CDVInvokedUrlCommand *)command
28
+ {
29
+ _previousOrientation = [[UIApplication sharedApplication] statusBarOrientation];
30
+
31
+ BOOL hasCamera = [UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera];
32
+
33
+ if (hasCamera)
34
+ {
35
+ //Force portrait orientation.
36
+ [[UIDevice currentDevice] setValue: [NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:@"orientation"];
37
+ dispatch_async(dispatch_get_main_queue(), ^{
38
+ NSLog(@"Arguments %@", command.arguments);
39
+ if (self->_scannerOpen == YES)
40
+ {
41
+ //Scanner is currently open, throw error.
42
+ NSArray *response = @[@"SCANNER_OPEN", @"", @""];
43
+ CDVPluginResult *pluginResult=[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsArray:response];
44
+
45
+ [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
46
+ }
47
+ else
48
+ {
49
+ //Open scanner.
50
+ self->_scannerOpen = YES;
51
+ self.cameraViewController = [[CameraViewController alloc] init];
52
+ self.cameraViewController.delegate = self;
53
+
54
+ //Provide settings to the camera view.
55
+ NSNumberFormatter* f = [[NSNumberFormatter alloc] init];
56
+ f.numberStyle = NSNumberFormatterDecimalStyle;
57
+ NSDictionary* config = [command.arguments objectAtIndex:0];
58
+ self->_beepOnSuccess = [[config valueForKey:@"beepOnSuccess"] boolValue] ?: NO;
59
+ self->_vibrateOnSuccess = [[config valueForKey:@"vibrateOnSuccess"] boolValue] ?: NO;
60
+ NSNumber* barcodeFormats = [config valueForKey:@"barcodeFormats"] ?: @1234;
61
+ self.cameraViewController.barcodeFormats = barcodeFormats;
62
+ self.cameraViewController.detectorSize = (CGFloat)[[config valueForKey:@"detectorSize"] ?: @0.5 floatValue];
63
+ self.cameraViewController.modalPresentationStyle = UIModalPresentationFullScreen;
64
+
65
+ NSLog(@"scanAreaSize: %f, barcodeFormats: %@", self.cameraViewController.detectorSize, self.cameraViewController.barcodeFormats);
66
+
67
+ [self.viewController presentViewController:self.cameraViewController animated: NO completion:nil];
68
+ self->_callback = command.callbackId;
69
+ }
70
+ });
71
+ }
72
+ else
73
+ {
74
+ UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:NSLocalizedString(@"The device has no camera.", @"Message to the user if the device has no camera.") preferredStyle:UIAlertControllerStyleAlert];
75
+ UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
76
+ [alert addAction:defaultAction];
77
+
78
+ [self.viewController presentViewController:alert animated:YES completion:nil];
79
+ }
80
+ }
81
+
82
+ - (void)sendResult:(MLKBarcode *)barcode
83
+ {
84
+ [self.cameraViewController dismissViewControllerAnimated:NO completion:nil];
85
+ _scannerOpen = NO;
86
+
87
+ NSString* value = barcode.rawValue;
88
+
89
+ // rawValue returns null if string is not UTF-8 encoded.
90
+ // If that's the case, we will decode it as ASCII,
91
+ // because it's the most common encoding for barcodes.
92
+ // e.g. https://www.barcodefaq.com/1d/code-128/
93
+ if(barcode.rawValue == nil)
94
+ {
95
+ value = [[NSString alloc] initWithData:barcode.rawData encoding:NSASCIIStringEncoding];
96
+ }
97
+
98
+ NSArray* response = @[value, @(barcode.format), @(barcode.valueType)];
99
+ CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:response];
100
+
101
+ [self playBeep];
102
+
103
+ [self resetOrientation];
104
+ [self.commandDelegate sendPluginResult:pluginResult callbackId:_callback];
105
+ }
106
+
107
+ - (void)playBeep
108
+ {
109
+ if (self->_beepOnSuccess)
110
+ {
111
+ [self->_player prepareToPlay];
112
+ [self->_player play];
113
+ }
114
+
115
+ if (self->_vibrateOnSuccess)
116
+ {
117
+ AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
118
+ }
119
+ }
120
+
121
+ - (void)closeScanner
122
+ {
123
+ [self.cameraViewController dismissViewControllerAnimated:NO completion:nil];
124
+ _scannerOpen = NO;
125
+
126
+ NSArray *response = @[@"USER_CANCELLED", @"", @""];
127
+ CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsArray:response];
128
+
129
+ [self resetOrientation];
130
+ [self.commandDelegate sendPluginResult:pluginResult callbackId:_callback];
131
+ }
132
+
133
+ - (void)resetOrientation
134
+ {
135
+ if (_previousOrientation != UIInterfaceOrientationUnknown && _previousOrientation != UIInterfaceOrientationPortrait)
136
+ {
137
+ [[UIDevice currentDevice] setValue: [NSNumber numberWithInteger: _previousOrientation] forKey:@"orientation"];
138
+ NSLog(@"Changing device orientation to previous orientation");
139
+ }
140
+ }
141
+
142
+
143
+ - (void)show:(CDVInvokedUrlCommand*)command
144
+ {
145
+ if (self.cameraViewController == nil)
146
+ {
147
+ NSLog(@"Tried to show scanner after it was closed.");
148
+ return;
149
+ }
150
+
151
+ if (_previousStatusBarStyle != -1)
152
+ {
153
+ NSLog(@"Tried to show scanner while already shown");
154
+ return;
155
+ }
156
+
157
+ _previousStatusBarStyle = [UIApplication sharedApplication].statusBarStyle;
158
+ _previousOrientation = [[UIApplication sharedApplication] statusBarOrientation];
159
+
160
+ __block UINavigationController* nav = [[UINavigationController alloc]
161
+ initWithRootViewController:self.cameraViewController];
162
+
163
+ nav.navigationBarHidden = YES;
164
+ nav.modalPresentationStyle = UIModalPresentationFullScreen;
165
+
166
+ __weak CDViOSScanner* weakSelf = self;
167
+
168
+ // Run later to avoid the "took a long time" log message.
169
+ dispatch_async(dispatch_get_main_queue(), ^{
170
+ if (weakSelf.cameraViewController != nil)
171
+ {
172
+ CGRect frame = [[UIScreen mainScreen] bounds];
173
+ UIWindow* tmpWindow = [[UIWindow alloc] initWithFrame:frame];
174
+ UIViewController* tmpController = [[UIViewController alloc] init];
175
+ [tmpWindow setRootViewController:tmpController];
176
+ [tmpWindow setWindowLevel:UIWindowLevelNormal];
177
+ [tmpWindow makeKeyAndVisible];
178
+ [tmpController presentViewController:nav animated:NO completion:nil];
179
+ }
180
+ });
181
+ }
182
+
183
+ @end
@@ -0,0 +1,36 @@
1
+ /*
2
+ Copyright 2016-present Google Inc. All Rights Reserved.
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ @import MLKitBarcodeScanning;
18
+ @import UIKit;
19
+
20
+ // View controller demonstraing how to use the barcode detector with the AVFoundation
21
+ // video pipeline.
22
+ @protocol senddataProtocol <NSObject>
23
+
24
+ - (void)closeScanner;
25
+ - (void)sendResult:(MLKBarcode *)result;
26
+
27
+ @end
28
+
29
+ @interface CameraViewController : UIViewController
30
+
31
+ @property(nonatomic,assign)id delegate;
32
+ @property(nonatomic,assign) NSNumber *barcodeFormats;
33
+ @property(nonatomic,assign) CGFloat detectorSize;
34
+
35
+ @end
36
+