Skip to content

Commit

Permalink
handle capture exception
Browse files Browse the repository at this point in the history
  • Loading branch information
den1spolyakov committed Feb 15, 2019
1 parent 09303fb commit 136e3e2
Showing 1 changed file with 64 additions and 56 deletions.
120 changes: 64 additions & 56 deletions ios/RCTCameraManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -605,69 +605,77 @@ - (void)captureStill:(NSInteger)target options:(NSDictionary *)options orientati
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
[self saveImage:imageData target:target metadata:nil resolve:resolve reject:reject];
#else
[[self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo] setVideoOrientation:orientation];

[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:[self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo] completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {

if (imageDataSampleBuffer) {
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];

// Create image source
CGImageSourceRef source = CGImageSourceCreateWithData((CFDataRef)imageData, NULL);
//get all the metadata in the image
NSMutableDictionary *imageMetadata = [(NSDictionary *) CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(source, 0, NULL)) mutableCopy];

// create cgimage
CGImageRef CGImage;
CGImage = CGImageSourceCreateImageAtIndex(source, 0, NULL);

// Rotate it
CGImageRef rotatedCGImage;
if ([options objectForKey:@"rotation"]) {
float rotation = [[options objectForKey:@"rotation"] floatValue];
rotatedCGImage = [self newCGImageRotatedByAngle:CGImage angle:rotation];
} else {
// Get metadata orientation
int metadataOrientation = [[imageMetadata objectForKey:(NSString *)kCGImagePropertyOrientation] intValue];

if (metadataOrientation == 6) {
rotatedCGImage = [self newCGImageRotatedByAngle:CGImage angle:270];
} else if (metadataOrientation == 1) {
rotatedCGImage = [self newCGImageRotatedByAngle:CGImage angle:0];
} else if (metadataOrientation == 3) {
rotatedCGImage = [self newCGImageRotatedByAngle:CGImage angle:180];
@try {
[[self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo] setVideoOrientation:orientation];

[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:[self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo] completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {

if (imageDataSampleBuffer) {
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];

// Create image source
CGImageSourceRef source = CGImageSourceCreateWithData((CFDataRef)imageData, NULL);
//get all the metadata in the image
NSMutableDictionary *imageMetadata = [(NSDictionary *) CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(source, 0, NULL)) mutableCopy];

// create cgimage
CGImageRef CGImage;
CGImage = CGImageSourceCreateImageAtIndex(source, 0, NULL);

// Rotate it
CGImageRef rotatedCGImage;
if ([options objectForKey:@"rotation"]) {
float rotation = [[options objectForKey:@"rotation"] floatValue];
rotatedCGImage = [self newCGImageRotatedByAngle:CGImage angle:rotation];
} else {
rotatedCGImage = [self newCGImageRotatedByAngle:CGImage angle:0];
// Get metadata orientation
int metadataOrientation = [[imageMetadata objectForKey:(NSString *)kCGImagePropertyOrientation] intValue];

if (metadataOrientation == 6) {
rotatedCGImage = [self newCGImageRotatedByAngle:CGImage angle:270];
} else if (metadataOrientation == 1) {
rotatedCGImage = [self newCGImageRotatedByAngle:CGImage angle:0];
} else if (metadataOrientation == 3) {
rotatedCGImage = [self newCGImageRotatedByAngle:CGImage angle:180];
} else {
rotatedCGImage = [self newCGImageRotatedByAngle:CGImage angle:0];
}
}
}
CGImageRelease(CGImage);
CGImageRelease(CGImage);

// Erase metadata orientation
[imageMetadata removeObjectForKey:(NSString *)kCGImagePropertyOrientation];
// Erase stupid TIFF stuff
[imageMetadata removeObjectForKey:(NSString *)kCGImagePropertyTIFFDictionary];
// Erase metadata orientation
[imageMetadata removeObjectForKey:(NSString *)kCGImagePropertyOrientation];
// Erase stupid TIFF stuff
[imageMetadata removeObjectForKey:(NSString *)kCGImagePropertyTIFFDictionary];

// Add input metadata
[imageMetadata mergeMetadata:[options objectForKey:@"metadata"]];
// Add input metadata
[imageMetadata mergeMetadata:[options objectForKey:@"metadata"]];

// Create destination thing
NSMutableData *rotatedImageData = [NSMutableData data];
CGImageDestinationRef destination = CGImageDestinationCreateWithData((CFMutableDataRef)rotatedImageData, CGImageSourceGetType(source), 1, NULL);
CFRelease(source);
// add the image to the destination, reattaching metadata
CGImageDestinationAddImage(destination, rotatedCGImage, (CFDictionaryRef) imageMetadata);
// And write
CGImageDestinationFinalize(destination);
CFRelease(destination);
// Create destination thing
NSMutableData *rotatedImageData = [NSMutableData data];
CGImageDestinationRef destination = CGImageDestinationCreateWithData((CFMutableDataRef)rotatedImageData, CGImageSourceGetType(source), 1, NULL);
CFRelease(source);
// add the image to the destination, reattaching metadata
CGImageDestinationAddImage(destination, rotatedCGImage, (CFDictionaryRef) imageMetadata);
// And write
CGImageDestinationFinalize(destination);
CFRelease(destination);

[self saveImage:rotatedImageData target:target metadata:imageMetadata resolve:resolve reject:reject];
[self saveImage:rotatedImageData target:target metadata:imageMetadata resolve:resolve reject:reject];

CGImageRelease(rotatedCGImage);
}
else {
reject(RCTErrorUnspecified, nil, RCTErrorWithMessage(error.description));
}
}];
CGImageRelease(rotatedCGImage);
}
else {
reject(RCTErrorUnspecified, nil, RCTErrorWithMessage(error.description));
}
}];
} @catch (NSException *exception) {
reject(
@"E_IMAGE_CAPTURE_FAILED",
@"Got exception while taking picture",
[NSError errorWithDomain:@"E_IMAGE_CAPTURE_FAILED" code: 500 userInfo:@{NSLocalizedDescriptionKey:exception.reason}]
);
}
#endif
});
}
Expand Down

0 comments on commit 136e3e2

Please sign in to comment.