extract sensor checker to a separate class

This commit is contained in:
Radu Popovici 2016-03-24 09:31:12 +02:00
parent 8f252aa687
commit f7524c20ce
4 changed files with 106 additions and 63 deletions

View File

@ -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 = "<group>"; };
4107014A1ACB732B00C6AA39 /* RCTCameraManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTCameraManager.m; sourceTree = "<group>"; };
454EBCF31B5082DC00AD0F86 /* NSMutableDictionary+ImageMetadata.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSMutableDictionary+ImageMetadata.m"; sourceTree = "<group>"; };
9FE592B11CA3CBF500788287 /* RCTSensorOrientationChecker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTSensorOrientationChecker.h; sourceTree = "<group>"; };
9FE592B21CA3CBF500788287 /* RCTSensorOrientationChecker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTSensorOrientationChecker.m; sourceTree = "<group>"; };
/* 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;
};

View File

@ -9,11 +9,11 @@
#import <AssetsLibrary/ALAssetsLibrary.h>
#import <AVFoundation/AVFoundation.h>
#import <ImageIO/ImageIO.h>
#import <CoreMotion/CoreMotion.h>
#import "RCTSensorOrientationChecker.h"
@interface RCTCameraManager ()
@property (strong, nonatomic) CMMotionManager * motionManager;
@property (strong, nonatomic) RCTSensorOrientationChecker * sensorOrientationChecker;
@end
@ -465,8 +465,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 getDeviceOrientation:^(UIInterfaceOrientation orientation) {
[[self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo] setVideoOrientation:[self convertToAVCaptureVideoOrientation: orientation]];
[self.sensorOrientationChecker getDeviceOrientation:^(UIInterfaceOrientation orientation) {
[[self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo] setVideoOrientation:[self.sensorOrientationChecker convertToAVCaptureVideoOrientation: orientation]];
[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:[self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo] completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
@ -872,63 +872,4 @@ didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
}
}
- (void)getDeviceOrientation:(void(^)(UIInterfaceOrientation orientation))callback
{
self.motionManager = [[CMMotionManager alloc] init];
self.motionManager.accelerometerUpdateInterval = DBL_MAX; // infinite delay, never update
self.motionManager.gyroUpdateInterval = DBL_MAX;
__weak CMMotionManager * weakMotionManager = self.motionManager;
__weak __typeof(self) weakSelf = self;
[self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue new]
withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
// stop after the first sample
[weakMotionManager stopAccelerometerUpdates];
UIInterfaceOrientation deviceOrientation = UIInterfaceOrientationUnknown;
if (!error) {
deviceOrientation = [weakSelf getOrientationBy:accelerometerData.acceleration];
}
if (callback) {
callback(deviceOrientation);
}
}];
}
- (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

View File

@ -0,0 +1,17 @@
//
// RCTSensorOrientationChecker.h
// RCTCamera
//
// Created by Radu Popovici on 24/03/16.
//
//
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface RCTSensorOrientationChecker : NSObject
- (void)getDeviceOrientation:(void(^)(UIInterfaceOrientation orientation))callback;
- (AVCaptureVideoOrientation)convertToAVCaptureVideoOrientation:(UIInterfaceOrientation)orientation;
@end

View File

@ -0,0 +1,79 @@
//
// RCTSensorOrientationChecker.m
// RCTCamera
//
// Created by Radu Popovici on 24/03/16.
//
//
#import "RCTSensorOrientationChecker.h"
#import <CoreMotion/CoreMotion.h>
@interface RCTSensorOrientationChecker ()
@property (strong, nonatomic) CMMotionManager * motionManager;
@end
@implementation RCTSensorOrientationChecker
- (void)getDeviceOrientation:(void(^)(UIInterfaceOrientation orientation))callback
{
self.motionManager = [[CMMotionManager alloc] init];
self.motionManager.accelerometerUpdateInterval = DBL_MAX; // infinite delay, never update
self.motionManager.gyroUpdateInterval = DBL_MAX;
__weak CMMotionManager * weakMotionManager = self.motionManager;
__weak __typeof(self) weakSelf = self;
[self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue new]
withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
// stop after the first sample
[weakMotionManager stopAccelerometerUpdates];
UIInterfaceOrientation deviceOrientation = UIInterfaceOrientationUnknown;
if (!error) {
deviceOrientation = [weakSelf getOrientationBy:accelerometerData.acceleration];
}
if (callback) {
callback(deviceOrientation);
}
}];
}
- (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