Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement pinch to zoom #111

Merged
merged 2 commits into from
May 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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