Skip to content

Commit

Permalink
iOS15 specific recommended zoom factor (#58)
Browse files Browse the repository at this point in the history
* feat: implement ios15 specfic recommended zoom factor

* style: remove debug log

* fix: variable typings

* feat: update refocus timer interval to 2seconds
  • Loading branch information
ian-wd authored May 15, 2023
1 parent e69d6ef commit d36c777
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
6 changes: 3 additions & 3 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -430,4 +430,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: ab59e48bef4677d9d279df353a73581b0f9df2cd

COCOAPODS: 1.11.3
COCOAPODS: 1.12.0
60 changes: 58 additions & 2 deletions ios/ScannerViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 {

Expand Down

0 comments on commit d36c777

Please sign in to comment.