Skip to content

Commit d36c777

Browse files
authored
iOS15 specific recommended zoom factor (#58)
* feat: implement ios15 specfic recommended zoom factor * style: remove debug log * fix: variable typings * feat: update refocus timer interval to 2seconds
1 parent e69d6ef commit d36c777

File tree

2 files changed

+61
-5
lines changed

2 files changed

+61
-5
lines changed

example/ios/Podfile.lock

+3-3
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ PODS:
218218
- React-jsinspector (0.68.0)
219219
- React-logger (0.68.0):
220220
- glog
221-
- react-native-idscan-sdk (0.5.1):
221+
- react-native-idscan-sdk (0.7.0):
222222
- React-Core
223223
- React-perflogger (0.68.0)
224224
- React-RCTActionSheet (0.68.0):
@@ -413,7 +413,7 @@ SPEC CHECKSUMS:
413413
React-jsiexecutor: 010a66edf644339f6da72b34208b070089680415
414414
React-jsinspector: 90f0bfd5d04e0b066c29216a110ffb9a6c34f23f
415415
React-logger: 8474fefa09d05f573a13c044cb0dfd751d4e52e3
416-
react-native-idscan-sdk: 10fcca037729c3c1f0213010599ceff6754a57d7
416+
react-native-idscan-sdk: 51842760e288b24c1cc209994eeaf75ce7132363
417417
React-perflogger: 15cb741d6c2379f4d3fc8f9e4d4e1110ef3020cb
418418
React-RCTActionSheet: ea9099db0597bd769430db1e2d011fd5fdb7fc5e
419419
React-RCTAnimation: 252df4749866f2654f37612f839522cac91c1165
@@ -430,4 +430,4 @@ SPEC CHECKSUMS:
430430

431431
PODFILE CHECKSUM: ab59e48bef4677d9d279df353a73581b0f9df2cd
432432

433-
COCOAPODS: 1.11.3
433+
COCOAPODS: 1.12.0

ios/ScannerViewController.m

+58-2
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ - (void)initCapture
183183
[MWOverlay addToPreviewLayer: self.prevLayer];
184184
#endif
185185

186-
self.focusTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(reFocus) userInfo:nil repeats:YES];
186+
self.focusTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(reFocus) userInfo:nil repeats:YES];
187187

188188
[self CustomOverlay];
189189
}
@@ -382,7 +382,12 @@ - (void)dealloc {
382382

383383
- (void) startScanning {
384384
self.state = LAUNCHING_CAMERA;
385-
[self.captureSession startRunning];
385+
386+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
387+
[self.captureSession startRunning];
388+
[self setRecommendedZoomFactor];
389+
});
390+
386391
self.prevLayer.hidden = NO;
387392
self.state = CAMERA;
388393
}
@@ -413,6 +418,57 @@ - (void) deinitCapture {
413418
}
414419
}
415420

421+
- (void) setRecommendedZoomFactor {
422+
if (@available(iOS 15.0, *)) {
423+
float deviceMinimumFocusDistance = [self.device minimumFocusDistance];
424+
425+
if (deviceMinimumFocusDistance == -1) {
426+
return;
427+
}
428+
429+
CMVideoDimensions formatDimensions = CMVideoFormatDescriptionGetDimensions([self.device.activeFormat formatDescription]);
430+
float rectOfInterestWidth = (float)formatDimensions.height / (float)formatDimensions.width;
431+
432+
float deviceFieldOfView = [self.device.activeFormat videoFieldOfView];
433+
float minimumSubjectDistanceForCode = [self minimumSubjectDistanceForCode:deviceFieldOfView minimumCodeSize:20 previewFillPercentage:rectOfInterestWidth];
434+
435+
if (minimumSubjectDistanceForCode < deviceMinimumFocusDistance) {
436+
float zoomFactor = deviceMinimumFocusDistance / minimumSubjectDistanceForCode;
437+
438+
@try {
439+
NSError *error;
440+
if ([self.device lockForConfiguration:&error]) {
441+
self.device.videoZoomFactor = zoomFactor;
442+
443+
[self.device unlockForConfiguration];
444+
}
445+
}
446+
@catch (id exceptionError) {
447+
NSLog(@"Could not lock for configuration");
448+
}
449+
}
450+
}
451+
}
452+
453+
- (float) minimumSubjectDistanceForCode:(float)fieldOfView
454+
minimumCodeSize:(float)minimumCodeSize
455+
previewFillPercentage:(float)previewFillPercentage
456+
{
457+
/*
458+
Given the camera horizontal field of view, we can compute the distance (mm) to make a code
459+
of minimumCodeSize (mm) fill the previewFillPercentage.
460+
*/
461+
float fieldOfViewDivided = fieldOfView / 2;
462+
float radians = [self degreesToRadians: fieldOfViewDivided];
463+
float filledCodeSize = minimumCodeSize / previewFillPercentage;
464+
465+
return filledCodeSize / tan(radians);
466+
}
467+
468+
- (float) degreesToRadians:(float)degrees
469+
{
470+
return degrees * M_PI / 180;
471+
}
416472

417473
- (void)decodeResultNotification: (NSNotification *)notification {
418474

0 commit comments

Comments
 (0)