From fdffed4d8b8433e015acb3b9e39bda5bbc8b3886 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franc=CC=A7ois=20Nadeau?= Date: Mon, 22 Jul 2019 15:07:37 -0400 Subject: [PATCH] Added some options (max width/height and quality) to the takePicture call. --- index.js | 5 ++-- ios/ALPRCameraManager.mm | 49 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index ff82e31..4916702 100644 --- a/index.js +++ b/index.js @@ -98,10 +98,11 @@ export default class Camera extends Component { } // Take a picture of what is currently seen by the user. + // Possible options: width (int), height (int) and quality (float). // @return a Promise. // @warn Currently only works on iOS. - async takePicture() { - return await CameraManager.takePicture(); + async takePicture(options) { + return await CameraManager.takePicture(options); } render() { diff --git a/ios/ALPRCameraManager.mm b/ios/ALPRCameraManager.mm index b787383..9569d5a 100644 --- a/ios/ALPRCameraManager.mm +++ b/ios/ALPRCameraManager.mm @@ -192,15 +192,17 @@ - (id)init { }]; } -RCT_EXPORT_METHOD(takePicture:(RCTPromiseResolveBlock)resolve +RCT_EXPORT_METHOD(takePicture:(NSDictionary *)options + resolve:(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]; + NSData* compressedImage = [ALPRCameraManager imageWithImage:imageData options:options]; NSString *path = [ALPRCameraManager generatePathInDirectory:[[ALPRCameraManager cacheDirectoryPath] stringByAppendingPathComponent:@"Camera"] withExtension:@".jpg"]; - NSString *uri = [ALPRCameraManager writeImage:imageData toPath:path]; + NSString *uri = [ALPRCameraManager writeImage:compressedImage toPath:path]; resolve(uri); } else { reject(@"E_IMAGE_CAPTURE_FAILED", @"Image could not be captured", error); @@ -208,6 +210,49 @@ - (id)init { }]; } ++ (NSData *)imageWithImage:(NSData *)imageData options:(NSDictionary *)options { + UIImage *image = [UIImage imageWithData:imageData]; + + // Calculate the image size. + int width = 0, height = 0; + float quality; + + if([options valueForKey:@"width"] != nil) { + width = [options[@"width"] intValue]; + } + if([options valueForKey:@"height"] != nil) { + height = [options[@"height"] intValue]; + } + + if(image.size.width > image.size.height) { + if(width == 0) { + width = image.size.width; // Default max width + } + height = width * image.size.height / image.size.width; + + } else { + if(height == 0) { + height = image.size.height; // Default max height + } + width = height * image.size.width / image.size.height; + } + CGSize size = CGSizeMake(width,height); + + if([options valueForKey:@"quality"] != nil) { + quality = [options[@"quality"] floatValue]; + } else { + quality = 1.0; // Default quality + } + + UIGraphicsBeginImageContext(size); + [image drawInRect:CGRectMake(0, 0, size.width, size.height)]; + UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext(); + UIGraphicsEndImageContext(); + + NSData *destData = UIImageJPEGRepresentation(destImage, quality); + return destData; +} + + (NSString *)generatePathInDirectory:(NSString *)directory withExtension:(NSString *)extension { NSString *fileName = [[[NSUUID UUID] UUIDString] stringByAppendingString:extension];