diff --git a/android/src/main/java/com/lwansbrough/RCTCamera/RCTCamera.java b/android/src/main/java/com/lwansbrough/RCTCamera/RCTCamera.java index 08d9260..44322e3 100644 --- a/android/src/main/java/com/lwansbrough/RCTCamera/RCTCamera.java +++ b/android/src/main/java/com/lwansbrough/RCTCamera/RCTCamera.java @@ -224,6 +224,32 @@ public class RCTCamera { } } + public void adjustCameraRotationToDeviceOrientation(int type, int deviceOrientation) + { + Camera camera = _cameras.get(type); + if (null == camera) { + return; + } + + CameraInfoWrapper cameraInfo = _cameraInfos.get(type); + int rotation; + int orientation = cameraInfo.info.orientation; + if (cameraInfo.info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { + rotation = (orientation + deviceOrientation * 90) % 360; + } else { + rotation = (orientation - deviceOrientation * 90 + 360) % 360; + } + cameraInfo.rotation = rotation; + Camera.Parameters parameters = camera.getParameters(); + parameters.setRotation(cameraInfo.rotation); + + try { + camera.setParameters(parameters); + } catch (Exception e) { + e.printStackTrace(); + } + } + private void adjustPreviewLayout(int type) { Camera camera = _cameras.get(type); if (null == camera) { diff --git a/android/src/main/java/com/lwansbrough/RCTCamera/RCTCameraModule.java b/android/src/main/java/com/lwansbrough/RCTCamera/RCTCameraModule.java index de2acac..0bade9d 100644 --- a/android/src/main/java/com/lwansbrough/RCTCamera/RCTCameraModule.java +++ b/android/src/main/java/com/lwansbrough/RCTCamera/RCTCameraModule.java @@ -53,10 +53,12 @@ public class RCTCameraModule extends ReactContextBaseJavaModule { public static final int MEDIA_TYPE_VIDEO = 2; private final ReactApplicationContext _reactContext; + private RCTSensorOrientationChecker _sensorOrientationChecker; public RCTCameraModule(ReactApplicationContext reactContext) { super(reactContext); _reactContext = reactContext; + _sensorOrientationChecker = new RCTSensorOrientationChecker(_reactContext); } @Override @@ -173,17 +175,26 @@ public class RCTCameraModule extends ReactContextBaseJavaModule { @ReactMethod public void capture(final ReadableMap options, final Promise promise) { + _sensorOrientationChecker.onResume(); + _sensorOrientationChecker.registerOrientationListener(new RCTSensorOrientationListener() { + @Override + public void orientationEvent() { + int deviceOrientation = _sensorOrientationChecker.getOrientation(); + _sensorOrientationChecker.unregisterOrientationListener(); + _sensorOrientationChecker.onPause(); + captureWithOrientation(options, promise, deviceOrientation); + } + }); + } + + public void captureWithOrientation(final ReadableMap options, final Promise promise, int deviceOrientation) { Camera camera = RCTCamera.getInstance().acquireCameraInstance(options.getInt("type")); if (null == camera) { promise.reject("No camera found."); return; } - if (options.getBoolean("playSoundOnCapture")) { - MediaActionSound sound = new MediaActionSound(); - sound.play(MediaActionSound.SHUTTER_CLICK); - } - + RCTCamera.getInstance().adjustCameraRotationToDeviceOrientation(options.getInt("type"), deviceOrientation); RCTCamera.getInstance().setCaptureQuality(options.getInt("type"), options.getString("quality")); camera.takePicture(null, null, new Camera.PictureCallback() { @Override diff --git a/android/src/main/java/com/lwansbrough/RCTCamera/RCTSensorOrientationChecker.java b/android/src/main/java/com/lwansbrough/RCTCamera/RCTSensorOrientationChecker.java new file mode 100644 index 0000000..c2aadfb --- /dev/null +++ b/android/src/main/java/com/lwansbrough/RCTCamera/RCTSensorOrientationChecker.java @@ -0,0 +1,84 @@ +/** + * Created by rpopovici on 23/03/16. + */ + +package com.lwansbrough.RCTCamera; + +import android.content.Context; +import android.hardware.Sensor; +import android.hardware.SensorEvent; +import android.hardware.SensorEventListener; +import android.hardware.SensorManager; + +import com.facebook.react.bridge.ReactApplicationContext; + +interface RCTSensorOrientationListener { + void orientationEvent(); +} + +public class RCTSensorOrientationChecker { + + int mOrientation = 0; + private SensorEventListener mSensorEventListener; + private SensorManager mSensorManager; + private RCTSensorOrientationListener mListener = null; + + public RCTSensorOrientationChecker( ReactApplicationContext reactContext) { + mSensorEventListener = new Listener(); + mSensorManager = (SensorManager) reactContext.getSystemService(Context.SENSOR_SERVICE); + + } + + /** + * Call on activity onResume() + */ + public void onResume() { + mSensorManager.registerListener(mSensorEventListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL); + } + + /** + * Call on activity onPause() + */ + public void onPause() { + mSensorManager.unregisterListener(mSensorEventListener); + } + + private class Listener implements SensorEventListener { + + @Override + public void onSensorChanged(SensorEvent event) { + float x = event.values[0]; + float y = event.values[1]; + + if (x<5 && x>-5 && y > 5) + mOrientation = 0; + else if (x<-5 && y<5 && y>-5) + mOrientation = 3; + else if (x<5 && x>-5 && y<-5) + mOrientation = 2; + else if (x>5 && y<5 && y>-5) + mOrientation = 1; + + if (mListener != null) { + mListener.orientationEvent(); + } + } + + @Override + public void onAccuracyChanged(Sensor sensor, int accuracy) { + + } + } + + public int getOrientation() { + return mOrientation; + } + + public void registerOrientationListener(RCTSensorOrientationListener listener) { + this.mListener = listener; + } + + public void unregisterOrientationListener() { + mListener = null; + } +} diff --git a/ios/RCTCamera.xcodeproj/project.pbxproj b/ios/RCTCamera.xcodeproj/project.pbxproj index eda5d03..e623715 100644 --- a/ios/RCTCamera.xcodeproj/project.pbxproj +++ b/ios/RCTCamera.xcodeproj/project.pbxproj @@ -11,6 +11,7 @@ 4107014D1ACB732B00C6AA39 /* RCTCamera.m in Sources */ = {isa = PBXBuildFile; fileRef = 410701481ACB732B00C6AA39 /* RCTCamera.m */; }; 4107014E1ACB732B00C6AA39 /* RCTCameraManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 4107014A1ACB732B00C6AA39 /* RCTCameraManager.m */; }; 454EBCF41B5082DC00AD0F86 /* NSMutableDictionary+ImageMetadata.m in Sources */ = {isa = PBXBuildFile; fileRef = 454EBCF31B5082DC00AD0F86 /* NSMutableDictionary+ImageMetadata.m */; }; + 9FE592B31CA3CBF500788287 /* RCTSensorOrientationChecker.m in Sources */ = {isa = PBXBuildFile; fileRef = 9FE592B21CA3CBF500788287 /* RCTSensorOrientationChecker.m */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -34,6 +35,8 @@ 410701491ACB732B00C6AA39 /* RCTCameraManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTCameraManager.h; sourceTree = ""; }; 4107014A1ACB732B00C6AA39 /* RCTCameraManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTCameraManager.m; sourceTree = ""; }; 454EBCF31B5082DC00AD0F86 /* NSMutableDictionary+ImageMetadata.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSMutableDictionary+ImageMetadata.m"; sourceTree = ""; }; + 9FE592B11CA3CBF500788287 /* RCTSensorOrientationChecker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTSensorOrientationChecker.h; sourceTree = ""; }; + 9FE592B21CA3CBF500788287 /* RCTSensorOrientationChecker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTSensorOrientationChecker.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -50,6 +53,8 @@ 410701241ACB719800C6AA39 = { isa = PBXGroup; children = ( + 9FE592B11CA3CBF500788287 /* RCTSensorOrientationChecker.h */, + 9FE592B21CA3CBF500788287 /* RCTSensorOrientationChecker.m */, 0314E39C1B661A460092D183 /* CameraFocusSquare.m */, 0314E39B1B661A0C0092D183 /* CameraFocusSquare.h */, 454EBCF31B5082DC00AD0F86 /* NSMutableDictionary+ImageMetadata.m */, @@ -128,6 +133,7 @@ 454EBCF41B5082DC00AD0F86 /* NSMutableDictionary+ImageMetadata.m in Sources */, 4107014E1ACB732B00C6AA39 /* RCTCameraManager.m in Sources */, 4107014D1ACB732B00C6AA39 /* RCTCamera.m in Sources */, + 9FE592B31CA3CBF500788287 /* RCTSensorOrientationChecker.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/ios/RCTCameraManager.m b/ios/RCTCameraManager.m index 613fb41..80eacf7 100644 --- a/ios/RCTCameraManager.m +++ b/ios/RCTCameraManager.m @@ -9,6 +9,13 @@ #import #import #import +#import "RCTSensorOrientationChecker.h" + +@interface RCTCameraManager () + +@property (strong, nonatomic) RCTSensorOrientationChecker * sensorOrientationChecker; + +@end @implementation RCTCameraManager @@ -251,6 +258,8 @@ RCT_CUSTOM_VIEW_PROPERTY(captureAudio, BOOL, RCTCamera) { self.mirrorImage = false; self.sessionQueue = dispatch_queue_create("cameraManagerQueue", DISPATCH_QUEUE_SERIAL); + + self.sensorOrientationChecker = [RCTSensorOrientationChecker new]; } return self; } @@ -503,7 +512,8 @@ RCT_EXPORT_METHOD(hasFlash:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRej NSData *imageData = UIImageJPEGRepresentation(image, 1.0); [self saveImage:imageData target:target metadata:nil resolve:resolve reject:reject]; #else - [[self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo] setVideoOrientation:self.previewLayer.connection.videoOrientation]; + [self.sensorOrientationChecker getDeviceOrientationWithBlock:^(UIInterfaceOrientation orientation) { + [[self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo] setVideoOrientation:[self.sensorOrientationChecker convertToAVCaptureVideoOrientation: orientation]]; [self.stillImageOutput captureStillImageAsynchronouslyFromConnection:[self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo] completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) { @@ -566,6 +576,7 @@ RCT_EXPORT_METHOD(hasFlash:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRej reject(RCTErrorUnspecified, nil, RCTErrorWithMessage(error.description)); } }]; + }]; #endif }); } diff --git a/ios/RCTSensorOrientationChecker.h b/ios/RCTSensorOrientationChecker.h new file mode 100644 index 0000000..15dc9e4 --- /dev/null +++ b/ios/RCTSensorOrientationChecker.h @@ -0,0 +1,21 @@ +// +// RCTSensorOrientationChecker.h +// RCTCamera +// +// Created by Radu Popovici on 24/03/16. +// +// + +#import +#import + +typedef void (^RCTSensorCallback) (UIInterfaceOrientation orientation); + +@interface RCTSensorOrientationChecker : NSObject + +@property (assign, nonatomic) UIInterfaceOrientation orientation; + +- (void)getDeviceOrientationWithBlock:(RCTSensorCallback)callback; +- (AVCaptureVideoOrientation)convertToAVCaptureVideoOrientation:(UIInterfaceOrientation)orientation; + +@end diff --git a/ios/RCTSensorOrientationChecker.m b/ios/RCTSensorOrientationChecker.m new file mode 100644 index 0000000..7957a13 --- /dev/null +++ b/ios/RCTSensorOrientationChecker.m @@ -0,0 +1,105 @@ +// +// RCTSensorOrientationChecker.m +// RCTCamera +// +// Created by Radu Popovici on 24/03/16. +// +// + +#import "RCTSensorOrientationChecker.h" +#import + + +@interface RCTSensorOrientationChecker () + +@property (strong, nonatomic) CMMotionManager * motionManager; +@property (strong, nonatomic) RCTSensorCallback orientationCallback; + +@end + +@implementation RCTSensorOrientationChecker + +- (instancetype)init +{ + self = [super init]; + if (self) { + // Initialization code + self.motionManager = [[CMMotionManager alloc] init]; + self.motionManager.accelerometerUpdateInterval = 0.2; + self.motionManager.gyroUpdateInterval = 0.2; + self.orientationCallback = nil; + } + return self; +} + +- (void)dealloc +{ + [self pause]; +} + +- (void)resume +{ + __weak __typeof(self) weakSelf = self; + [self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue new] + withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) { + if (!error) { + self.orientation = [weakSelf getOrientationBy:accelerometerData.acceleration]; + } + if (self.orientationCallback) { + self.orientationCallback(self.orientation); + } + }]; +} + +- (void)pause +{ + [self.motionManager stopAccelerometerUpdates]; +} + +- (void)getDeviceOrientationWithBlock:(RCTSensorCallback)callback +{ + __weak __typeof(self) weakSelf = self; + self.orientationCallback = ^(UIInterfaceOrientation orientation) { + if (callback) { + callback(orientation); + } + weakSelf.orientationCallback = nil; + [weakSelf pause]; + }; + [self resume]; +} + +- (UIInterfaceOrientation)getOrientationBy:(CMAcceleration)acceleration +{ + if(acceleration.x >= 0.75) { + return UIInterfaceOrientationLandscapeLeft; + } + if(acceleration.x <= -0.75) { + return UIInterfaceOrientationLandscapeRight; + } + if(acceleration.y >= -0.75) { + return UIInterfaceOrientationPortrait; + } + if(acceleration.y >= 0.75) { + return UIInterfaceOrientationPortraitUpsideDown; + } + return [[UIApplication sharedApplication] statusBarOrientation]; +} + +- (AVCaptureVideoOrientation)convertToAVCaptureVideoOrientation:(UIInterfaceOrientation)orientation +{ + switch (orientation) { + case UIInterfaceOrientationPortrait: + return AVCaptureVideoOrientationPortrait; + case UIInterfaceOrientationPortraitUpsideDown: + return AVCaptureVideoOrientationPortraitUpsideDown; + case UIInterfaceOrientationLandscapeLeft: + return AVCaptureVideoOrientationLandscapeLeft; + case UIInterfaceOrientationLandscapeRight: + return AVCaptureVideoOrientationLandscapeRight; + default: + return 0; // unknown + } +} + +@end