Skip to content

Commit

Permalink
Merge pull request #2 from franck-nadeau/takePicture-ios
Browse files Browse the repository at this point in the history
Added functionality so that a user can take a picture of what is seen.
  • Loading branch information
Francois Nadeau authored Apr 4, 2019
2 parents 2b56f84 + d925c3c commit 97230d2
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ export default class Camera extends Component {
}
}

// Take a picture of what is currently seen by the user.
// @return a Promise<String:uri>.
// @warn Currently only works on iOS.
async takePicture() {
return await CameraManager.takePicture();
}

render() {
const nativeProps = convertNativeProps(this.props);

Expand Down
59 changes: 59 additions & 0 deletions ios/ALPRCameraManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ @interface ALPRCameraManager () {
UIDeviceOrientation deviceOrientation;
}
@property (atomic) BOOL isProcessingFrame;
@property(nonatomic, strong) AVCaptureStillImageOutput *stillImageOutput;

@end

Expand Down Expand Up @@ -191,6 +192,56 @@ - (id)init {
}];
}

RCT_EXPORT_METHOD(takePicture:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject) {
AVCaptureConnection *connection = [self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo];
[connection setVideoOrientation:self.previewLayer.connection.videoOrientation];
[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:connection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
if (imageSampleBuffer && !error) {
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
NSString *path = [ALPRCameraManager generatePathInDirectory:[[ALPRCameraManager cacheDirectoryPath] stringByAppendingPathComponent:@"Camera"] withExtension:@".jpg"];
NSString *uri = [ALPRCameraManager writeImage:imageData toPath:path];
resolve(uri);
} else {
reject(@"E_IMAGE_CAPTURE_FAILED", @"Image could not be captured", error);
}
}];
}

+ (NSString *)generatePathInDirectory:(NSString *)directory withExtension:(NSString *)extension
{
NSString *fileName = [[[NSUUID UUID] UUIDString] stringByAppendingString:extension];
[ALPRCameraManager ensureDirExistsWithPath:directory];
return [directory stringByAppendingPathComponent:fileName];
}

+ (BOOL)ensureDirExistsWithPath:(NSString *)path
{
BOOL isDir = NO;
NSError *error;
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDir];
if (!(exists && isDir)) {
[[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
if (error) {
return NO;
}
}
return YES;
}

+ (NSString *)writeImage:(NSData *)image toPath:(NSString *)path
{
[image writeToFile:path atomically:YES];
NSURL *fileURL = [NSURL fileURLWithPath:path];
return [fileURL absoluteString];
}

+ (NSString *)cacheDirectoryPath
{
NSArray *array = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
return [array objectAtIndex:0];
}

- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection {
Expand Down Expand Up @@ -276,6 +327,14 @@ - (void)startSession {
[self.session addOutput:videoDataOutput];
}

AVCaptureStillImageOutput *stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
if ([self.session canAddOutput:stillImageOutput]) {
stillImageOutput.outputSettings = @{AVVideoCodecKey : AVVideoCodecJPEG};
[self.session addOutput:stillImageOutput];
[stillImageOutput setHighResolutionStillImageOutputEnabled:YES];
self.stillImageOutput = stillImageOutput;
}

__weak ALPRCameraManager *weakSelf = self;
[self setRuntimeErrorHandlingObserver:[NSNotificationCenter.defaultCenter addObserverForName:AVCaptureSessionRuntimeErrorNotification object:self.session queue:nil usingBlock:^(NSNotification *note) {
ALPRCameraManager *strongSelf = weakSelf;
Expand Down

0 comments on commit 97230d2

Please sign in to comment.