@habibov77/cordova-plugin-mcc-mlkit-barcode-scanner-fixed 1.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,554 @@
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 AVFoundation;
18
+ @import MLKitBarcodeScanning;
19
+ @import MLKitVision;
20
+
21
+ #import "CameraViewController.h"
22
+
23
+ @interface CameraViewController ()<AVCaptureVideoDataOutputSampleBufferDelegate>
24
+
25
+ @property(nonatomic, weak) IBOutlet UIView *placeHolderView;
26
+ @property(nonatomic, weak) IBOutlet UIView *overlayView;
27
+ @property(nonatomic, strong) UIImageView *imageView;
28
+
29
+ @property(nonatomic, strong) AVCaptureSession *session;
30
+ @property(nonatomic, strong) AVCaptureVideoDataOutput *videoDataOutput;
31
+ @property(nonatomic, strong) dispatch_queue_t videoDataOutputQueue;
32
+ @property(nonatomic, strong) AVCaptureVideoPreviewLayer *previewLayer;
33
+
34
+ @property(nonatomic, strong) MLKBarcodeScanner *barcodeDetector;
35
+ @property(nonatomic, strong) UIButton *torchButton;
36
+ @property(nonatomic, strong) NSTimer *scanTimeoutTimer;
37
+ @property(nonatomic, assign) BOOL resultSent;
38
+
39
+ @end
40
+
41
+ @implementation CameraViewController
42
+ @synthesize delegate;
43
+
44
+ - (BOOL)prefersStatusBarHidden
45
+ {
46
+ return YES;
47
+ }
48
+
49
+ - (BOOL)prefersHomeIndicatorAutoHidden
50
+ {
51
+ return YES;
52
+ }
53
+
54
+ - (BOOL) shouldAutorotate
55
+ {
56
+ return NO;
57
+ }
58
+
59
+ - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
60
+ return UIInterfaceOrientationMaskPortrait;
61
+ }
62
+
63
+ - (id)initWithCoder:(NSCoder *)aDecoder {
64
+ self = [super initWithCoder:aDecoder];
65
+ if (self) {
66
+ _videoDataOutputQueue = dispatch_queue_create("VideoDataOutputQueue",
67
+ DISPATCH_QUEUE_SERIAL);
68
+ }
69
+ return self;
70
+ }
71
+
72
+ - (void)viewDidLoad {
73
+ [super viewDidLoad];
74
+
75
+ // Set up camera.
76
+ self.session = [[AVCaptureSession alloc] init];
77
+ self.session.sessionPreset = AVCaptureSessionPresetHigh;
78
+
79
+ _videoDataOutputQueue = dispatch_queue_create("VideoDataOutputQueue",
80
+ DISPATCH_QUEUE_SERIAL);
81
+
82
+ [self updateCameraSelection];
83
+
84
+ // Set up video processing pipeline.
85
+ [self setUpVideoProcessing];
86
+
87
+ // Set up camera preview.
88
+ [self setUpCameraPreview];
89
+
90
+ //Parse Cordova settings.
91
+ NSNumber *formats = 0;
92
+ //If barcodeFormats == 0 then process as a VIN with VIN verifications.
93
+ if([_barcodeFormats isEqual: @0]) {
94
+ NSLog(@"Running VIN style");
95
+ formats = @(MLKBarcodeFormatCode39|MLKBarcodeFormatDataMatrix);
96
+ } else if([_barcodeFormats isEqual: @1234]) {
97
+ // @todo: investigating what should be done here
98
+ } else {
99
+ formats = _barcodeFormats;
100
+ }
101
+ NSLog(@"_barcodeFormats %@, %@", _barcodeFormats, formats);
102
+
103
+ // Initialize barcode detector.
104
+ MLKBarcodeScannerOptions *options = [[MLKBarcodeScannerOptions alloc] initWithFormats: [formats intValue]];
105
+ self.barcodeDetector = [MLKBarcodeScanner barcodeScannerWithOptions:options];
106
+ }
107
+
108
+ - (void)viewDidLayoutSubviews {
109
+ [super viewDidLayoutSubviews];
110
+
111
+ self.previewLayer.frame = self.view.layer.bounds;
112
+ self.previewLayer.position = CGPointMake(CGRectGetMidX(self.previewLayer.frame),
113
+ CGRectGetMidY(self.previewLayer.frame));
114
+ }
115
+
116
+ - (void)dealloc {
117
+ [self.scanTimeoutTimer invalidate];
118
+ self.scanTimeoutTimer = nil;
119
+ [self cleanupCaptureSession];
120
+ }
121
+
122
+ - (void)viewDidAppear:(BOOL)animated {
123
+ [super viewDidAppear:animated];
124
+
125
+ AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
126
+ if (authStatus == AVAuthorizationStatusDenied) {
127
+ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Camera permission required" message:@"Access to the camera has been prohibited, please enable it in the settings to continue." preferredStyle:UIAlertControllerStyleAlert];
128
+
129
+ UIAlertAction *primaryAction = [UIAlertAction actionWithTitle:@"Settings" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
130
+ [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
131
+ [self.delegate closeScanner];
132
+ }];
133
+ UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
134
+ [self.delegate closeScanner];
135
+ }];
136
+ [alert addAction:cancelAction];
137
+ [alert addAction:primaryAction];
138
+ alert.preferredAction = primaryAction;
139
+ [self presentViewController:alert animated:YES completion:nil];
140
+ } else {
141
+ //Force portrait orientation.
142
+ [[UIDevice currentDevice] setValue:
143
+ [NSNumber numberWithInteger: UIInterfaceOrientationPortrait]
144
+ forKey:@"orientation"];
145
+
146
+ [self.session startRunning];
147
+ [self startScanTimeoutIfNeeded];
148
+ }
149
+ }
150
+
151
+ - (void)viewDidDisappear:(BOOL)animated {
152
+ [super viewDidDisappear:animated];
153
+ [self.scanTimeoutTimer invalidate];
154
+ self.scanTimeoutTimer = nil;
155
+ [self.session stopRunning];
156
+ }
157
+
158
+ - (BOOL)tryMarkResultSent {
159
+ @synchronized (self) {
160
+ if (self.resultSent) {
161
+ return NO;
162
+ }
163
+ self.resultSent = YES;
164
+ return YES;
165
+ }
166
+ }
167
+
168
+ - (void)startScanTimeoutIfNeeded {
169
+ [self.scanTimeoutTimer invalidate];
170
+ self.scanTimeoutTimer = nil;
171
+ if (self.timeoutMs <= 0) {
172
+ return;
173
+ }
174
+ self.scanTimeoutTimer = [NSTimer scheduledTimerWithTimeInterval:(self.timeoutMs / 1000.0)
175
+ target:self
176
+ selector:@selector(onScanTimeout:)
177
+ userInfo:nil
178
+ repeats:NO];
179
+ }
180
+
181
+ - (void)onScanTimeout:(NSTimer *)timer {
182
+ if (![self tryMarkResultSent]) {
183
+ return;
184
+ }
185
+ [self.scanTimeoutTimer invalidate];
186
+ self.scanTimeoutTimer = nil;
187
+ [self cleanupCaptureSession];
188
+ [_session stopRunning];
189
+ [delegate sendScanTimeout];
190
+ }
191
+
192
+ #pragma mark - AVCaptureVideoDataOutputSampleBufferDelegate
193
+
194
+ - (void)captureOutput:(AVCaptureOutput *)captureOutput
195
+ didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
196
+ fromConnection:(AVCaptureConnection *)connection {
197
+
198
+ //Convert sampleBuffer into an image.
199
+ //MLKVisionImage *image = [[MLKVisionImage alloc] initWithBuffer:sampleBuffer];
200
+ CVImageBufferRef imageBuffer =
201
+ CMSampleBufferGetImageBuffer(sampleBuffer);
202
+ CIImage *ciImage = [CIImage imageWithCVPixelBuffer:imageBuffer];
203
+ CIContext *temporaryContext = [CIContext contextWithOptions:nil];
204
+ CGImageRef videoImage = [temporaryContext
205
+ createCGImage:ciImage
206
+ fromRect:CGRectMake(0, 0,
207
+ CVPixelBufferGetWidth(imageBuffer),
208
+ CVPixelBufferGetHeight(imageBuffer))];
209
+
210
+ UIImage *image = [[UIImage alloc] initWithCGImage:videoImage];
211
+ CGImageRelease(videoImage);
212
+
213
+ //We're going to crop UIImage to the onscreen viewfinder's box size for faster processing.
214
+ UIImage *croppedImg = nil;
215
+
216
+ //Define the crop coordinates.
217
+ CGRect screenRect = [[UIScreen mainScreen] bounds];
218
+ CGFloat screenWidth = screenRect.size.width;
219
+ CGFloat screenHeight = screenRect.size.height;
220
+
221
+ CGFloat imageWidth = image.size.width;
222
+ CGFloat imageHeight = image.size.height;
223
+
224
+ CGFloat actualFrameWidth = 0;
225
+ CGFloat actualFrameHeight = 0;
226
+
227
+ //Figure out which ratio is bigger and then subtract a value off the frame width in case some of the camera preview is hanging off screen.
228
+ if(imageWidth/screenWidth < imageHeight/screenHeight){
229
+ actualFrameWidth = imageWidth * _detectorSize;
230
+ actualFrameHeight = actualFrameWidth;
231
+ } else {
232
+ actualFrameHeight = imageHeight * _detectorSize;
233
+ actualFrameWidth = actualFrameHeight;
234
+ }
235
+
236
+ //Define crop rectangle.
237
+ CGRect cropRect = CGRectMake(imageWidth/2 - actualFrameWidth/2, imageHeight/2 - actualFrameHeight/2, actualFrameWidth, actualFrameHeight);
238
+
239
+ //Crop image
240
+ croppedImg = [self croppIngimageByImageName:image toRect:cropRect];
241
+
242
+ //Rotate the image.
243
+ MLKVisionImage *portraitImage = [[MLKVisionImage alloc] initWithImage:croppedImg];
244
+ portraitImage.orientation = UIImageOrientationRight;
245
+
246
+ //Send the image through the barcode reader.
247
+ [self.barcodeDetector processImage:portraitImage completion:^(NSArray<MLKBarcode *> *barcodes,
248
+ NSError *error) {
249
+ if (error != nil) {
250
+ return;
251
+ } else if (barcodes != nil) {
252
+ for (MLKBarcode *barcode in barcodes) {
253
+ if (![self tryMarkResultSent]) {
254
+ return;
255
+ }
256
+ NSLog(@"Barcode value: %@", barcode.rawValue);
257
+ [self.scanTimeoutTimer invalidate];
258
+ self.scanTimeoutTimer = nil;
259
+ [self cleanupCaptureSession];
260
+ [self->_session stopRunning];
261
+ [self->delegate sendResult:barcode];
262
+ break;
263
+ }
264
+ }
265
+ }];
266
+
267
+ }
268
+
269
+ #pragma mark - Camera setup
270
+
271
+ - (void)cleanupVideoProcessing {
272
+ if (self.videoDataOutput) {
273
+ [self.session removeOutput:self.videoDataOutput];
274
+ }
275
+ self.videoDataOutput = nil;
276
+ }
277
+
278
+ - (void)cleanupCaptureSession {
279
+ [self.session stopRunning];
280
+ [self cleanupVideoProcessing];
281
+ self.session = nil;
282
+ [self.previewLayer removeFromSuperlayer];
283
+ }
284
+
285
+ - (void)setUpVideoProcessing {
286
+ self.videoDataOutput = [[AVCaptureVideoDataOutput alloc] init];
287
+ NSDictionary *rgbOutputSettings = @{
288
+ (__bridge NSString*)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_32BGRA)
289
+ };
290
+ [self.videoDataOutput setVideoSettings:rgbOutputSettings];
291
+
292
+ if (![self.session canAddOutput:self.videoDataOutput]) {
293
+ [self cleanupVideoProcessing];
294
+ NSLog(@"Failed to setup video output");
295
+ return;
296
+ }
297
+ [self.videoDataOutput setAlwaysDiscardsLateVideoFrames:YES];
298
+ [self.videoDataOutput setSampleBufferDelegate:self queue:self.videoDataOutputQueue];
299
+ [self.session addOutput:self.videoDataOutput];
300
+ }
301
+
302
+ - (void)setUpCameraPreview {
303
+ self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
304
+ [self.previewLayer setBackgroundColor:[UIColor blackColor].CGColor];
305
+ [self.previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
306
+
307
+ self.previewLayer.frame = self.view.superview.bounds;
308
+ [self.view.layer addSublayer:self.previewLayer];
309
+
310
+ CGRect screenRect = [[UIScreen mainScreen] bounds];
311
+ CGFloat screenWidth = screenRect.size.width;
312
+ CGFloat screenHeight = screenRect.size.height;
313
+
314
+ CGFloat frameWidth = screenWidth*_detectorSize;
315
+ CGFloat frameHeight = frameWidth;
316
+
317
+ UILabel* _label1 = [[UILabel alloc] init];
318
+ _label1.frame = CGRectMake(screenWidth/2 - frameWidth/2, screenHeight/2 - frameHeight/2, frameWidth, frameHeight);
319
+ _label1.layer.masksToBounds = NO;
320
+ _label1.layer.cornerRadius = 30;
321
+ _label1.userInteractionEnabled = YES;
322
+ _label1.layer.borderColor = [UIColor whiteColor].CGColor;
323
+ _label1.layer.borderWidth = 3.0;
324
+ UITapGestureRecognizer* tapScanner = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(focusAtPoint:)];
325
+ [_label1 addGestureRecognizer:tapScanner];
326
+
327
+ CGFloat buttonSize = 45.0;
328
+
329
+ UIButton *_cancelButton = [[UIButton alloc] init];
330
+ [_cancelButton addTarget:self
331
+ action:@selector(closeView:)
332
+ forControlEvents:UIControlEventTouchUpInside];
333
+
334
+ NSString * cancelBase64String = @"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAQAAABpN6lAAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAAAmJLR0QAAKqNIzIAAAAJcEhZcwAADdcAAA3XAUIom3gAAAAHdElNRQfhCxMVEyaNvw4TAAADNElEQVR42u2dv1LqQBSHv1DwBjY0FjyAA7wCtna2ttrwLr6GY4PWKfQBJGNtb2PDMFQ2ewvmDheBC0l295zjYVO6Cb/vC4b82c0p+Lf1GDNkwAVfzKh4oyRguxWMGTFgyBnvVMwo+dzd8Y4F4cfyQl+aoFXr87LFtOCW4mfHc8qtjqtlyWS7u4lWMGG5h6rkfBN/vqfjapnSlaap3bpM/8s0Xyso9u799fJkTEGXp4NM5d9v9t3BrtYUHIMfCNwC9HYc+mwrOBY/sKAHN0d2tqLgePxA4Abua3TXr6AefuAeXmutoFtBXfzAKwd+AC0pqI8fmMNH7ZV0KmiCH/iAhwar6VPQDD/w0KFq9IFXPCpS0OWRq0ZrVnDZyJymb0HTvR8IXEKx43rJkoI2+C+rk+H+3msm/Qra4C/Xl/qTxhuRVdAGPzBZb6g4cOmoU0E7/OnmXY52G5NQED2xLQVJ0tpRkCypDQVJU+pXkDyhbgVZ0ulVkC2ZTgVZU+lTkD2RLgUiafQoEEuiQ4FoCnkF4glkA4jjy4ZQgS8XRA2+TBhV+PkDqcPPG0olfr5gavHzhFONnz6gevy0IU3gpwtqBj9NWFP48QObw48b2iR+vOBm8eOEN40fQ4Fx/PYKzONLKVCEL6FAGX5uBQrxcypQip9LgWL8HAqU46dWYAA/pQIj+KkUGMJPocAYfmwFBvFjKjCKH0uBYfwYCpLjd6QN/e7m/F/A+UHQ+c+g8xMh56fCzi+GnF8OO78h4vyWmPObos5vizt/MOL80Zjzh6POH487HyDhfIiM80FSzofJOR8o6XyorPPB0s6HyzufMOF8yozzSVPOp805nzjpfOqs88nTzqfPiwfw/v4I0RQ68MWS6MEXSaMLP3siffhZU+nEz5ZML36WdLrxkyfUj580pQ38ZEnt4CdJaws/emL3r9Z2/nJ156/Xd19gwXmJjQ6jhh/7zDXf0uwAfHPNc8N1R+7L7JwKLZ1KbTkvtla30pSeQ9+uVv9wWJ0KLp5Kbrovunoqu4v7wssrBa5Lb6+6uyy+vrlve4wZMuCCL2ZUvFESpBlatoIxIwYMOeOdihkln+s//wFFdoCM42fEswAAACV0RVh0ZGF0ZTpjcmVhdGUAMjAxNy0xMS0xOVQyMToxOTozOCswMTowMPNH2M8AAAAldEVYdGRhdGU6bW9kaWZ5ADIwMTctMTEtMTlUMjE6MTk6MzgrMDE6MDCCGmBzAAAAGXRFWHRTb2Z0d2FyZQB3d3cuaW5rc2NhcGUub3Jnm+48GgAAAABJRU5ErkJggg==";
335
+
336
+ NSURL *cancelImageUrl = [NSURL URLWithString:cancelBase64String];
337
+ NSData *cancelImageData = [NSData dataWithContentsOfURL:cancelImageUrl];
338
+ UIImage *cancelIcon = [UIImage imageWithData:cancelImageData];
339
+ [_cancelButton setImage:cancelIcon
340
+ forState:UIControlStateNormal];
341
+
342
+ CGFloat screenOffset = (screenWidth/2 - frameWidth/2)/2 - buttonSize/2;
343
+ NSLog(@"screenOffset %f", screenOffset);
344
+
345
+ _cancelButton.frame = CGRectMake(screenOffset, screenHeight-screenOffset-buttonSize, buttonSize, buttonSize);
346
+ _cancelButton.backgroundColor = [UIColor colorWithWhite:1 alpha:0.4];
347
+ _cancelButton.transform=CGAffineTransformMakeRotation(M_PI / 2);
348
+ _cancelButton.layer.cornerRadius = buttonSize/2;
349
+ _cancelButton.contentEdgeInsets = UIEdgeInsetsMake(15, 15, 15, 15);
350
+
351
+ [self.view addSubview:_cancelButton];
352
+
353
+
354
+
355
+
356
+ self.torchButton = [[UIButton alloc] init];
357
+ [self.torchButton addTarget:self
358
+ action:@selector(toggleFlashlight:)
359
+ forControlEvents:UIControlEventTouchUpInside];
360
+
361
+ NSString * torchBase64String = @"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAQAAABpN6lAAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAAAmJLR0QAAKqNIzIAAAAJcEhZcwAADdcAAA3XAUIom3gAAAAHdElNRQfhCxMVAzOqoPipAAADM0lEQVR42u2dv2sTcRyG38RWzGYHhxYHp+B+Lk7iIlhFXZvdPeAouOuS1X/AVCiiCBU6OjgIOffG+gvEgCIqlsQi9JxEbC/XJt8fz136eTJecve+T+6Su8t9czX5oqmWEiVa9DbHfAZKlaqrfuDlTERNbQ2VRXwM1VaNrv2v/nrU8n8f62VR0EbqZ8rUpqtLUjPyyv//htB0jV93FrCiBia/oRYv4BxWX5IS1xm4f4x8Cv7FV8RAS7SADKzvoYH7JlBxTAAdgMYE0AFoTAAdwDAMwzAMo7q8ws4IZsr0wjW++57gFqrfeenuAt6YAJLXvADbBFABJVgDPmobrP+DF5BpAxPwyH0WPk6IPKmyAB8/MJ/UZ80D9T/ojPtMfKwB3/UcqO/l/fd1TvAxIuAhstRclrQT/ShgzU/0Y17m8lMLOh9V+UjX3L8CfbKgr1Hf/zt04f3EvFLorU7QdfdzXFuR6v/SRbpsPjci1b9MFx3P7eD1d3SFLlnM3cD1r9IFD+Z+wJW/AvWluh4EqT/UJbraYZnTmvf627pA12IV3KQrTa7gqcf6z+g603BKXzzV/+Z6NSjFiicB9+gi07PhRcBZusb0XPdQ3/nXP5J5D58Dt8JGDHuZ3G8Pp60C//AS+jrBl85zeBc24FxgAe/HTulrVT2lkhIlao0d/RNYQGhO527Xu+rsGWnUUEe7uc+tOPXc+su5z13OVVB59lfqjH1u5ygI2CwYZtfQZmwB8a8WX9Vo7LSRurHjxBfQK5yaRs8TnL2rdPEow8XZ3wRKRnwBicPUmRBQPNY4uoDwlPxrML6AI78jZLvC5ToYCv83JOMqHPZwOHBCTkBJEtqOEB2AxgTQAWhMAB2AxgTQAWhMAB2AxgTQAWhMAB2AxgTQAWhMAB2AxgTQAWhMAB2AxgTQAWhMAB2AxgTQAWhMAB2AxgTQAWhMAB2AxgTQAWhMAB2AxgTQAWhMAB2AxgTQAWhMAB2AxgQEX8IAfHUpBLgNhg0+lDa8gJ7Tq2dgLLHLbXk93Fa3DEz/d6ttOrofpr01d2lure1DwaQ3Z492c/V4jptqKVFy4F2KB0qVqqt+nFh/ADjJgLiaxweIAAAAJXRFWHRkYXRlOmNyZWF0ZQAyMDE3LTExLTE5VDIxOjAzOjUxKzAxOjAwdhzbkgAAACV0RVh0ZGF0ZTptb2RpZnkAMjAxNy0xMS0xOVQyMTowMzo1MSswMTowMAdBYy4AAAAZdEVYdFNvZnR3YXJlAHd3dy5pbmtzY2FwZS5vcmeb7jwaAAAAAElFTkSuQmCCconv";
362
+
363
+ NSURL *torchImageUrl = [NSURL URLWithString:torchBase64String];
364
+ NSData *torchImageData = [NSData dataWithContentsOfURL:torchImageUrl];
365
+ UIImage *torchIcon = [UIImage imageWithData:torchImageData];
366
+ [self.torchButton setImage:torchIcon
367
+ forState:UIControlStateNormal];
368
+
369
+ self.torchButton.frame = CGRectMake(screenWidth-screenOffset-buttonSize, screenHeight-screenOffset-buttonSize, buttonSize, buttonSize);
370
+ self.torchButton.backgroundColor = [UIColor colorWithWhite:1 alpha:0.4];
371
+ self.torchButton.transform=CGAffineTransformMakeRotation(M_PI / 2);
372
+ self.torchButton.layer.cornerRadius = buttonSize/2;
373
+ self.torchButton.contentEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
374
+
375
+ [self.view addSubview:self.torchButton];
376
+
377
+ [self.view addSubview:_label1];
378
+
379
+ self.imageView = [[UIImageView alloc] initWithImage:nil];
380
+
381
+ UIView *catView = [[UIView alloc] initWithFrame:CGRectMake(0,0,frameWidth,frameHeight)];
382
+ self.imageView.frame = catView.bounds;
383
+
384
+ // add the imageview to the superview
385
+ [catView addSubview:self.imageView];
386
+
387
+ //add the view to the main view
388
+
389
+ [self.view addSubview:catView];
390
+
391
+ }
392
+
393
+ #pragma mark - Helper Functions
394
+
395
+ - (void)focusAtPoint:(id) sender {
396
+ NSLog(@"captured touch");
397
+ CGPoint touchPoint = [(UITapGestureRecognizer*)sender locationInView:self.view];
398
+ double focus_x = touchPoint.x/self.previewLayer.frame.size.width;
399
+ double focus_y = (touchPoint.y+66)/self.previewLayer.frame.size.height;
400
+
401
+ NSError *error;
402
+ NSArray *devices = [AVCaptureDevice devices];
403
+ for (AVCaptureDevice *device in devices){
404
+ NSLog(@"Device name: %@", [device localizedName]);
405
+ if ([device hasMediaType:AVMediaTypeVideo]) {
406
+ if ([device position] == AVCaptureDevicePositionBack) {
407
+ NSLog(@"Device position : back");
408
+ CGPoint point = CGPointMake(focus_y, 1-focus_x);
409
+ if ([device isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus] && [device lockForConfiguration:&error]){
410
+ [device setFocusPointOfInterest:point];
411
+
412
+ for (UIView *subView in self.view.subviews)
413
+ {
414
+ if (subView.tag == 99)
415
+ {
416
+ [subView removeFromSuperview];
417
+ }
418
+ }
419
+
420
+ CGRect rect = CGRectMake(touchPoint.x-30, touchPoint.y-30, 60, 60);
421
+ UIView *focusRect = [[UIView alloc] initWithFrame:rect];
422
+ focusRect.layer.borderColor = [UIColor colorWithRed:0.98 green:0.80 blue:0.18 alpha:.7].CGColor;
423
+ focusRect.layer.borderWidth = 1;
424
+ focusRect.tag = 99;
425
+ [self.view addSubview:focusRect];
426
+
427
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
428
+ [focusRect removeFromSuperview];
429
+ });
430
+
431
+ [device setFocusMode:AVCaptureFocusModeAutoFocus];
432
+ [device unlockForConfiguration];
433
+ }
434
+ }
435
+ }
436
+ }
437
+ }
438
+ static inline double radians (double degrees) {return degrees * M_PI/180;}
439
+ - (UIImage*) rotateImage:(UIImage*)src toOrientation:(UIImageOrientation) orientation
440
+ {
441
+ UIGraphicsBeginImageContext(src.size);
442
+
443
+ CGContextRef context = UIGraphicsGetCurrentContext();
444
+
445
+ if (orientation == UIImageOrientationRight) {
446
+ CGContextRotateCTM (context, radians(90));
447
+ } else if (orientation == UIImageOrientationLeft) {
448
+ CGContextRotateCTM (context, radians(-90));
449
+ } else if (orientation == UIImageOrientationDown) {
450
+ // NOTHING
451
+ } else if (orientation == UIImageOrientationUp) {
452
+ CGContextRotateCTM (context, radians(90));
453
+ }
454
+
455
+ [src drawAtPoint:CGPointMake(0, 0)];
456
+
457
+ UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
458
+ UIGraphicsEndImageContext();
459
+ return image;
460
+ }
461
+
462
+ - (UIImage *)croppIngimageByImageName:(UIImage *)imageToCrop toRect:(CGRect)rect
463
+ {
464
+ //CGRect CropRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height+15);
465
+
466
+ CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
467
+ UIImage *cropped = [UIImage imageWithCGImage:imageRef];
468
+ CGImageRelease(imageRef);
469
+
470
+ return cropped;
471
+ }
472
+
473
+ - (void) toggleFlashlight:(id)sender
474
+ {
475
+ // check if flashlight available
476
+ Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
477
+ if (captureDeviceClass != nil) {
478
+ AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
479
+ AVCapturePhotoSettings *photosettings = [AVCapturePhotoSettings photoSettings];
480
+ if ([device hasTorch] && [device hasFlash]){
481
+
482
+ [device lockForConfiguration:nil];
483
+ if (device.torchMode == AVCaptureTorchModeOff)
484
+ {
485
+ self.torchButton.backgroundColor = [UIColor colorWithWhite:1 alpha:1];
486
+ [device setTorchMode:AVCaptureTorchModeOn];
487
+ photosettings.flashMode = AVCaptureFlashModeOn;
488
+ //torchIsOn = YES;
489
+ }
490
+ else
491
+ {
492
+ self.torchButton.backgroundColor = [UIColor colorWithWhite:1 alpha:.4];
493
+ [device setTorchMode:AVCaptureTorchModeOff];
494
+ photosettings.flashMode = AVCaptureFlashModeOff;
495
+ // torchIsOn = NO;
496
+ }
497
+ [device unlockForConfiguration];
498
+ }
499
+ } }
500
+
501
+ - (void)closeView:(id)sender{
502
+ if (![self tryMarkResultSent]) {
503
+ return;
504
+ }
505
+ [self.scanTimeoutTimer invalidate];
506
+ self.scanTimeoutTimer = nil;
507
+ [ self cleanupCaptureSession];
508
+ [_session stopRunning];
509
+ [delegate closeScanner];
510
+ }
511
+
512
+
513
+ - (void)updateCameraSelection {
514
+ [self.session beginConfiguration];
515
+
516
+ // Remove old inputs
517
+ NSArray *oldInputs = [self.session inputs];
518
+ for (AVCaptureInput *oldInput in oldInputs) {
519
+ [self.session removeInput:oldInput];
520
+ }
521
+
522
+ AVCaptureDevicePosition desiredPosition = AVCaptureDevicePositionBack;
523
+ AVCaptureDeviceInput *input = [self captureDeviceInputForPosition:desiredPosition];
524
+ if (!input) {
525
+ // Failed, restore old inputs
526
+ for (AVCaptureInput *oldInput in oldInputs) {
527
+ [self.session addInput:oldInput];
528
+ }
529
+ } else {
530
+ // Succeeded, set input and update connection states
531
+ [self.session addInput:input];
532
+ }
533
+ [self.session commitConfiguration];
534
+ }
535
+
536
+ - (AVCaptureDeviceInput *)captureDeviceInputForPosition:(AVCaptureDevicePosition)desiredPosition {
537
+ for (AVCaptureDevice *device in [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) {
538
+ if (device.position == desiredPosition) {
539
+ NSError *error = nil;
540
+ AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device
541
+ error:&error];
542
+ if (error) {
543
+ NSLog(@"Could not initialize for AVMediaTypeVideo for device %@", device);
544
+ } else if ([self.session canAddInput:input]) {
545
+ return input;
546
+ }
547
+ }
548
+ }
549
+ return nil;
550
+ }
551
+
552
+
553
+
554
+ @end
Binary file
@@ -0,0 +1,8 @@
1
+ /*
2
+ Localizable.strings
3
+ scan-test-app
4
+
5
+ Created by Florian Biebel on 14.02.22.
6
+
7
+ */
8
+ "The device has no camera." = "Das Gerät verfügt über keine Kamera.";
@@ -0,0 +1,8 @@
1
+ /*
2
+ Localizable.strings
3
+ scan-test-app
4
+
5
+ Created by Florian Biebel on 14.02.22.
6
+
7
+ */
8
+ "The device has no camera." = "The device has no camera.";
@@ -0,0 +1,10 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var BarcodeScanner_plugin = require('./BarcodeScanner.plugin.js');
6
+
7
+
8
+
9
+ exports.MLKitBarcodeScanner = BarcodeScanner_plugin.MLKitBarcodeScanner;
10
+ //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiQmFyY29kZVNjYW5uZXIuY29udHJhY3QuanMiLCJzb3VyY2VzIjpbXSwic291cmNlc0NvbnRlbnQiOltdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7OzsifQ==