Adding support for camera switching (WIP)

This commit is contained in:
Lochlan Wansbrough 2015-04-01 10:32:53 -07:00
parent d9ae1f5e56
commit cb8165eb14
4 changed files with 68 additions and 23 deletions

View File

@ -38,14 +38,17 @@ var Camera = React.createClass({
render: function() {
var style = flattenStyle([styles.base, this.props.style]);
var aspect = this.props.aspect || 'Fill';
var camera = this.props.camera || 'Back';
var orientation = this.props.orientation || 'Portrait';
aspect = NativeModules.CameraManager.aspects[aspect];
camera = NativeModules.CameraManager.cameras[camera];
orientation = NativeModules.CameraManager.orientations[orientation];
var nativeProps = merge(this.props, {
style,
aspect: aspect,
camera: camera,
orientation: orientation,
});
@ -54,7 +57,11 @@ var Camera = React.createClass({
});
var RCTCamera = createReactIOSNativeComponentClass({
validAttributes: merge(ReactIOSViewAttributes.UIView, { aspect: true, orientation: true }),
validAttributes: merge(ReactIOSViewAttributes.UIView, {
aspect: true,
camera: true,
orientation: true
}),
uiViewClassName: 'RCTCamera',
});

View File

@ -1,4 +1,5 @@
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import "ViewfinderView.h"
@class AVCaptureSession;
@ -7,5 +8,6 @@
@property (nonatomic) AVCaptureSession *session;
@property (nonatomic) ViewfinderView *viewfinder;
@property (nonatomic) AVCaptureDeviceInput *captureDeviceInput;
@end
@end

View File

@ -5,16 +5,42 @@
@implementation RCTCamera
- (void)setOrientation:(NSInteger)orientation
{
[[(AVCaptureVideoPreviewLayer *)[[self viewfinder] layer] connection] setVideoOrientation:orientation];
}
- (void)setAspect:(NSString *)aspect
{
[(AVCaptureVideoPreviewLayer *)[[self viewfinder] layer] setVideoGravity:aspect];
}
- (void)setCamera:(NSInteger)camera
{
// AVCaptureDevice *currentVideoDevice = [_captureDeviceInput device];
AVCaptureDevice *videoDevice = [self deviceWithMediaType:AVMediaTypeVideo preferringPosition:(AVCaptureDevicePosition)camera];
AVCaptureDeviceInput *videoDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:nil];
[[self session] beginConfiguration];
[[self session] removeInput:[self captureDeviceInput]];
if ([[self session] canAddInput:videoDeviceInput])
{
// [[NSNotificationCenter defaultCenter] removeObserver:self name:AVCaptureDeviceSubjectAreaDidChangeNotification object:currentVideoDevice];
//
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(subjectAreaDidChange:) name:AVCaptureDeviceSubjectAreaDidChangeNotification object:videoDevice];
//
[[self session] addInput:videoDeviceInput];
[self setCaptureDeviceInput:videoDeviceInput];
}
else
{
[[self session] addInput:_captureDeviceInput];
}
[[self session] commitConfiguration];
}
- (void)setOrientation:(NSInteger)orientation
{
[[(AVCaptureVideoPreviewLayer *)[[self viewfinder] layer] connection] setVideoOrientation:orientation];
}
- (id)init
{
if ((self = [super init])) {
@ -27,30 +53,18 @@
NSError *error = nil;
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
AVCaptureDevice *captureDevice = [devices firstObject];
AVCaptureDevice *captureDevice = [self deviceWithMediaType:AVMediaTypeVideo preferringPosition:AVCaptureDevicePositionBack];
AVCaptureDevicePosition position = AVCaptureDevicePositionBack;
for (AVCaptureDevice *device in devices)
{
if ([device position] == position)
{
captureDevice = device;
break;
}
}
AVCaptureDeviceInput *captureDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
_captureDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
if (error)
{
NSLog(@"%@", error);
}
if ([session canAddInput:captureDeviceInput])
if ([session canAddInput:_captureDeviceInput])
{
[session addInput:captureDeviceInput];
[session addInput:_captureDeviceInput];
[[(AVCaptureVideoPreviewLayer *)[[self viewfinder] layer] connection] setVideoOrientation:AVCaptureVideoOrientationPortrait];
[(AVCaptureVideoPreviewLayer *)[[self viewfinder] layer] setVideoGravity:AVLayerVideoGravityResizeAspectFill];
@ -62,6 +76,23 @@
return self;
}
- (AVCaptureDevice *)deviceWithMediaType:(NSString *)mediaType preferringPosition:(AVCaptureDevicePosition)position
{
NSArray *devices = [AVCaptureDevice devicesWithMediaType:mediaType];
AVCaptureDevice *captureDevice = [devices firstObject];
for (AVCaptureDevice *device in devices)
{
if ([device position] == position)
{
captureDevice = device;
break;
}
}
return captureDevice;
}
- (NSArray *)reactSubviews
{
NSArray *subviews = @[_viewfinder];

View File

@ -13,6 +13,7 @@
}
RCT_EXPORT_VIEW_PROPERTY(aspect, NSString);
RCT_EXPORT_VIEW_PROPERTY(camera, NSInteger);
RCT_EXPORT_VIEW_PROPERTY(orientation, NSInteger);
- (NSDictionary *)constantsToExport
@ -23,6 +24,10 @@ RCT_EXPORT_VIEW_PROPERTY(orientation, NSInteger);
@"Fit": AVLayerVideoGravityResizeAspect,
@"Fill": AVLayerVideoGravityResizeAspectFill
},
@"cameras": @{
@"Front": @(AVCaptureDevicePositionFront),
@"Back": @(AVCaptureDevicePositionBack)
},
@"orientations": @{
@"LandscapeLeft": @(AVCaptureVideoOrientationLandscapeLeft),
@"LandscapeRight": @(AVCaptureVideoOrientationLandscapeRight),