Skip to content
This repository has been archived by the owner on Jun 16, 2023. It is now read-only.

Commit

Permalink
fix: fast capture orientation. (#3269)
Browse files Browse the repository at this point in the history
Make sure orientation value stays consistent from the beginning to the end of the capture process.

Co-authored-by: Cristiano Coelho <cristianocca@hotmail.com>
  • Loading branch information
cristianoccazinsp and cristianocca authored Jul 2, 2021
1 parent df0418e commit afa7bb1
Showing 1 changed file with 30 additions and 23 deletions.
53 changes: 30 additions & 23 deletions ios/RN/RNCamera.m
Original file line number Diff line number Diff line change
@@ -750,13 +750,24 @@ - (void)takePicture:(NSDictionary *)options resolve:(RCTPromiseResolveBlock)reso
return;
}

if (!self.deviceOrientation) {
[self takePictureWithOrientation:options resolve:resolve reject:reject];
return;
}

NSInteger orientation = [options[@"orientation"] integerValue];
// make sure to get orientation info here
// as it may change if multiple consecutive calls are done
NSInteger orientation;
NSNumber* deviceOrientation;

@synchronized (self) {
if (!self.deviceOrientation) {
[self takePictureWithOrientation:options resolve:resolve reject:reject];
return;
}

orientation = [options[@"orientation"] integerValue];
deviceOrientation = self.deviceOrientation;

self.orientation = nil;
self.deviceOrientation = nil;
}

AVCaptureConnection *connection = [self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo];
[connection setVideoOrientation:orientation];
@try {
@@ -923,7 +934,7 @@ - (void)takePicture:(NSDictionary *)options resolve:(RCTPromiseResolveBlock)reso
}

}

CFDictionaryRef finalMetaData = nil;
if (writeExif) {
finalMetaData = (__bridge CFDictionaryRef)metadata;
@@ -950,7 +961,7 @@ - (void)takePicture:(NSDictionary *)options resolve:(RCTPromiseResolveBlock)reso
}

bool success = YES;

if (![options[@"doNotSave"] boolValue]) {
NSString* pathRes = [RNImageUtils writeImage:destData toPath:path];
if (!pathRes) {
@@ -959,9 +970,9 @@ - (void)takePicture:(NSDictionary *)options resolve:(RCTPromiseResolveBlock)reso
} else {
response[@"uri"] = pathRes;
}

}

if (success) {
response[@"width"] = @(takenImage.size.width);
response[@"height"] = @(takenImage.size.height);
@@ -977,18 +988,15 @@ - (void)takePicture:(NSDictionary *)options resolve:(RCTPromiseResolveBlock)reso
//[RNImageUtils updatePhotoMetadata:imageSampleBuffer withAdditionalData:@{ @"Orientation": @(imageRotation) } inResponse:response]; // TODO
}

response[@"pictureOrientation"] = @([self.orientation integerValue]);
response[@"deviceOrientation"] = @([self.deviceOrientation integerValue]);
response[@"pictureOrientation"] = @(orientation);
response[@"deviceOrientation"] = @([deviceOrientation integerValue]);

if (useFastMode) {
[self onPictureSaved:@{@"data": response, @"id": options[@"id"]}];
} else {
resolve(response);
}
}

self.orientation = nil;
self.deviceOrientation = nil;
}
else{
reject(@"E_IMAGE_CAPTURE_FAILED", @"Image could not be saved", error);
@@ -1016,7 +1024,7 @@ - (void)takePicture:(NSDictionary *)options resolve:(RCTPromiseResolveBlock)reso
}

- (void)recordWithOrientation:(NSDictionary *)options resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject{

UIInterfaceOrientation orientation = [self.sensorOrientationChecker getDeviceOrientation];
NSMutableDictionary *tmpOptions = [options mutableCopy];
if ([tmpOptions valueForKey:@"orientation"] == nil) {
@@ -1111,7 +1119,6 @@ - (void)record:(NSDictionary *)options resolve:(RCTPromiseResolveBlock)resolve r
// with this and the captureAudio prop
dispatch_async(self.sessionQueue, ^{
[self initializeAudioCaptureSessionInput];

// finally, make sure we got access to the capture device
// and turn the connection on.
if(self.audioCaptureDeviceInput != nil){
@@ -1186,17 +1193,17 @@ - (void)record:(NSDictionary *)options resolve:(RCTPromiseResolveBlock)resolve r
if (options[@"codec"]) {
if (@available(iOS 10, *)) {
AVVideoCodecType videoCodecType = options[@"codec"];

if ([self.movieFileOutput.availableVideoCodecTypes containsObject:videoCodecType]) {
self.videoCodecType = videoCodecType;

BOOL supportsBitRate = NO;

// prevent crashing due to unsupported keys
if (@available(iOS 12.0, *)) {
supportsBitRate = [[self.movieFileOutput supportedOutputSettingsKeysForConnection:connection] containsObject:AVVideoCompressionPropertiesKey];
}

if(options[@"videoBitrate"] && supportsBitRate) {
NSString *videoBitrate = options[@"videoBitrate"];
[self.movieFileOutput setOutputSettings:@{
@@ -2017,7 +2024,7 @@ - (void)mirrorVideo:(NSURL *)inputURL completion:(void (^)(NSURL* outputUR))comp

[instruction setLayerInstructions:@[transformer]];
[videoComposition setInstructions:@[instruction]];

//get preset for export via default or session
AVCaptureSessionPreset preset = [self getDefaultPreset];
if (self.session.sessionPreset != preset) {

0 comments on commit afa7bb1

Please sign in to comment.