From 4ffedae7923446d607d4dd4680bf5b3fa6409a08 Mon Sep 17 00:00:00 2001 From: Thomas Parslow Date: Sun, 20 May 2018 23:29:16 +0200 Subject: [PATCH] Support orientation option when taking picture on ios --- ios/RN/RNCamera.m | 8 +++++++- ios/RN/RNCameraManager.h | 8 ++++++++ ios/RN/RNCameraManager.m | 7 +++++++ src/RNCamera.js | 6 ++++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/ios/RN/RNCamera.m b/ios/RN/RNCamera.m index 470bc68..d4cea11 100644 --- a/ios/RN/RNCamera.m +++ b/ios/RN/RNCamera.m @@ -309,7 +309,13 @@ static NSDictionary *defaultFaceDetectorOptions = nil; - (void)takePicture:(NSDictionary *)options resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject { AVCaptureConnection *connection = [self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo]; - [connection setVideoOrientation:[RNCameraUtils videoOrientationForDeviceOrientation:[[UIDevice currentDevice] orientation]]]; + int orientation; + if ([options[@"orientation"] integerValue]) { + orientation = [options[@"orientation"] integerValue]; + } else { + orientation = [RNCameraUtils videoOrientationForDeviceOrientation:[[UIDevice currentDevice] orientation]]; + } + [connection setVideoOrientation:orientation]; [self.stillImageOutput captureStillImageAsynchronouslyFromConnection:connection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) { if (imageSampleBuffer && !error) { NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]; diff --git a/ios/RN/RNCameraManager.h b/ios/RN/RNCameraManager.h index f900f3f..c07b2ab 100644 --- a/ios/RN/RNCameraManager.h +++ b/ios/RN/RNCameraManager.h @@ -18,6 +18,14 @@ typedef NS_ENUM(NSInteger, RNCameraFlashMode) { RNCameraFlashModeAuto = AVCaptureFlashModeAuto }; +typedef NS_ENUM(NSInteger, RNCameraOrientation) { + RNCameraOrientationAuto = 0, + RNCameraOrientationLandscapeLeft = AVCaptureVideoOrientationLandscapeLeft, + RNCameraOrientationLandscapeRight = AVCaptureVideoOrientationLandscapeRight, + RNCameraOrientationPortrait = AVCaptureVideoOrientationPortrait, + RNCameraOrientationPortraitUpsideDown = AVCaptureVideoOrientationPortraitUpsideDown +}; + typedef NS_ENUM(NSInteger, RNCameraAutoFocus) { RNCameraAutoFocusOff = AVCaptureFocusModeLocked, RNCameraAutoFocusOn = AVCaptureFocusModeContinuousAutoFocus, diff --git a/ios/RN/RNCameraManager.m b/ios/RN/RNCameraManager.m index 0e067c0..89f710e 100644 --- a/ios/RN/RNCameraManager.m +++ b/ios/RN/RNCameraManager.m @@ -56,6 +56,13 @@ RCT_EXPORT_VIEW_PROPERTY(onFacesDetected, RCTDirectEventBlock); @"4:3": @(RNCameraVideo4x3), @"288p": @(RNCameraVideo288p), }, + @"Orientation": @{ + @"auto": @(RNCameraOrientationAuto), + @"landscapeLeft": @(RNCameraOrientationLandscapeLeft), + @"landscapeRight": @(RNCameraOrientationLandscapeRight), + @"portrait": @(RNCameraOrientationPortrait), + @"portraitUpsideDown": @(RNCameraOrientationPortraitUpsideDown) + }, @"VideoCodec": [[self class] validCodecTypes], @"BarCodeType" : [[self class] validBarCodeTypes], @"FaceDetection" : [[self class] faceDetectorConstants] diff --git a/src/RNCamera.js b/src/RNCamera.js index 793bb81..91a6c28 100644 --- a/src/RNCamera.js +++ b/src/RNCamera.js @@ -30,8 +30,11 @@ const styles = StyleSheet.create({ }, }); +type Orientation = "auto"|"landscapeLeft"|"landscapeRight"|"portrait"|"portraitUpsideDown"; + type PictureOptions = { quality?: number, + orientation?: Orientation, base64?: boolean, mirrorImage?: boolean, exif?: boolean, @@ -247,6 +250,9 @@ export default class Camera extends React.Component { if (!options.quality) { options.quality = 1; } + if (options.orientation) { + options.orientation = CameraManager.Orientation[options.orientation]; + } return await CameraManager.takePicture(options, this._cameraHandle); }