Skip to content

Commit

Permalink
Added some options (max width/height and quality) to the takePicture …
Browse files Browse the repository at this point in the history
…call.
  • Loading branch information
François Nadeau committed Jul 22, 2019
1 parent 97230d2 commit fdffed4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<String:uri>.
// @warn Currently only works on iOS.
async takePicture() {
return await CameraManager.takePicture();
async takePicture(options) {
return await CameraManager.takePicture(options);
}

render() {
Expand Down
49 changes: 47 additions & 2 deletions ios/ALPRCameraManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -192,22 +192,67 @@ - (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);
}
}];
}

+ (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];
Expand Down

0 comments on commit fdffed4

Please sign in to comment.