diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index bcc2c07..65bacb3 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -218,7 +218,7 @@ PODS: - React-jsinspector (0.68.0) - React-logger (0.68.0): - glog - - react-native-idscan-sdk (0.5.1): + - react-native-idscan-sdk (0.7.0): - React-Core - React-perflogger (0.68.0) - React-RCTActionSheet (0.68.0): @@ -413,7 +413,7 @@ SPEC CHECKSUMS: React-jsiexecutor: 010a66edf644339f6da72b34208b070089680415 React-jsinspector: 90f0bfd5d04e0b066c29216a110ffb9a6c34f23f React-logger: 8474fefa09d05f573a13c044cb0dfd751d4e52e3 - react-native-idscan-sdk: 10fcca037729c3c1f0213010599ceff6754a57d7 + react-native-idscan-sdk: 51842760e288b24c1cc209994eeaf75ce7132363 React-perflogger: 15cb741d6c2379f4d3fc8f9e4d4e1110ef3020cb React-RCTActionSheet: ea9099db0597bd769430db1e2d011fd5fdb7fc5e React-RCTAnimation: 252df4749866f2654f37612f839522cac91c1165 @@ -430,4 +430,4 @@ SPEC CHECKSUMS: PODFILE CHECKSUM: ab59e48bef4677d9d279df353a73581b0f9df2cd -COCOAPODS: 1.11.3 +COCOAPODS: 1.12.0 diff --git a/ios/ScannerViewController.m b/ios/ScannerViewController.m index f2e5926..8945878 100644 --- a/ios/ScannerViewController.m +++ b/ios/ScannerViewController.m @@ -183,7 +183,7 @@ - (void)initCapture [MWOverlay addToPreviewLayer: self.prevLayer]; #endif - self.focusTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(reFocus) userInfo:nil repeats:YES]; + self.focusTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(reFocus) userInfo:nil repeats:YES]; [self CustomOverlay]; } @@ -382,7 +382,12 @@ - (void)dealloc { - (void) startScanning { self.state = LAUNCHING_CAMERA; - [self.captureSession startRunning]; + + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + [self.captureSession startRunning]; + [self setRecommendedZoomFactor]; + }); + self.prevLayer.hidden = NO; self.state = CAMERA; } @@ -413,6 +418,57 @@ - (void) deinitCapture { } } +- (void) setRecommendedZoomFactor { + if (@available(iOS 15.0, *)) { + float deviceMinimumFocusDistance = [self.device minimumFocusDistance]; + + if (deviceMinimumFocusDistance == -1) { + return; + } + + CMVideoDimensions formatDimensions = CMVideoFormatDescriptionGetDimensions([self.device.activeFormat formatDescription]); + float rectOfInterestWidth = (float)formatDimensions.height / (float)formatDimensions.width; + + float deviceFieldOfView = [self.device.activeFormat videoFieldOfView]; + float minimumSubjectDistanceForCode = [self minimumSubjectDistanceForCode:deviceFieldOfView minimumCodeSize:20 previewFillPercentage:rectOfInterestWidth]; + + if (minimumSubjectDistanceForCode < deviceMinimumFocusDistance) { + float zoomFactor = deviceMinimumFocusDistance / minimumSubjectDistanceForCode; + + @try { + NSError *error; + if ([self.device lockForConfiguration:&error]) { + self.device.videoZoomFactor = zoomFactor; + + [self.device unlockForConfiguration]; + } + } + @catch (id exceptionError) { + NSLog(@"Could not lock for configuration"); + } + } + } +} + +- (float) minimumSubjectDistanceForCode:(float)fieldOfView + minimumCodeSize:(float)minimumCodeSize + previewFillPercentage:(float)previewFillPercentage +{ + /* + Given the camera horizontal field of view, we can compute the distance (mm) to make a code + of minimumCodeSize (mm) fill the previewFillPercentage. + */ + float fieldOfViewDivided = fieldOfView / 2; + float radians = [self degreesToRadians: fieldOfViewDivided]; + float filledCodeSize = minimumCodeSize / previewFillPercentage; + + return filledCodeSize / tan(radians); +} + +- (float) degreesToRadians:(float)degrees +{ + return degrees * M_PI / 180; +} - (void)decodeResultNotification: (NSNotification *)notification {