Skip to content

Commit

Permalink
Merge pull request #111 from RobertSasak/features/zoom-ios
Browse files Browse the repository at this point in the history
Implement pinch to zoom
  • Loading branch information
RobertSasak authored May 4, 2021
2 parents 405e0b4 + bb41122 commit ae8a12d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,7 @@ Note: If you are getting errors, double check that you have completed all of the

- OpenALPR built from [OpenALPR-iOS](https://github.com/twelve17/openalpr-ios)
- Project scaffold based on [react-native-camera](https://github.com/lwansbrough/react-native-camera)

## Sponsors

- [repocoin.io](https://repocoin.io)
2 changes: 1 addition & 1 deletion example/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ export default class App extends Component {
</TouchableOpacity>
<Text style={styles.plateText}>{plate}</Text>
<Text style={styles.confidenceText}>
{confidence ? confidence + '%' : ''}
{confidence ? (+confidence).toFixed(1) + '%' : ''}
</Text>
<TouchableOpacity
style={styles.takePicture}
Expand Down
13 changes: 10 additions & 3 deletions ios/ALPRCamera.m
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.camFocus removeFromSuperview];
}
NSDictionary *event = @{
@"target": self.reactTag,
@"touchPoint": @{
@"x": [NSNumber numberWithDouble:touchPoint.x],
@"y": [NSNumber numberWithDouble:touchPoint.y]
}
};
[self.bridge.eventDispatcher sendAppEventWithName:@"focusChanged" body:event];

// Show animated rectangle on the touched area
if (_touchToFocus) {
Expand All @@ -181,13 +189,12 @@ - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
if (allTouchesEnded) {
_multipleTouches = NO;
}

}

-(void) handlePinchToZoomRecognizer:(UIPinchGestureRecognizer*)pinchRecognizer {

if (pinchRecognizer.state == UIGestureRecognizerStateChanged) {
return;
[self.manager zoom:pinchRecognizer.velocity reactTag:self.reactTag];
}
}

Expand Down
1 change: 1 addition & 0 deletions ios/ALPRCameraManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@ typedef NS_ENUM(NSInteger, ALPRCameraTorchMode) {
- (void)startSession;
- (void)stopSession;
- (void)focusAtThePoint:(CGPoint) atPoint;
- (void)zoom:(CGFloat)velocity reactTag:(NSNumber *)reactTag;

@end
30 changes: 30 additions & 0 deletions ios/ALPRCameraManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,36 @@ - (void)focusAtThePoint:(CGPoint) atPoint;
}
}

- (void)zoom:(CGFloat)velocity reactTag:(NSNumber *)reactTag{
if (isnan(velocity)) {
return;
}
const CGFloat pinchVelocityDividerFactor = 20.0f; // TODO: calibrate or make this component's property
NSError *error = nil;
AVCaptureDevice *device = [[self videoCaptureDeviceInput] device];
if ([device lockForConfiguration:&error]) {
CGFloat zoomFactor = device.videoZoomFactor + atan(velocity / pinchVelocityDividerFactor);
if (zoomFactor > device.activeFormat.videoMaxZoomFactor) {
zoomFactor = device.activeFormat.videoMaxZoomFactor;
} else if (zoomFactor < 1) {
zoomFactor = 1.0f;
}

NSDictionary *event = @{
@"target": reactTag,
@"zoomFactor": [NSNumber numberWithDouble:zoomFactor],
@"velocity": [NSNumber numberWithDouble:velocity]
};

[self.bridge.eventDispatcher sendAppEventWithName:@"zoomChanged" body:event];

device.videoZoomFactor = zoomFactor;
[device unlockForConfiguration];
} else {
NSLog(@"error: %@", error);
}
}

- (void)setCaptureQuality:(NSString *)quality
{
#if !(TARGET_IPHONE_SIMULATOR)
Expand Down

0 comments on commit ae8a12d

Please sign in to comment.