Adding support for camera switching (WIP)
This commit is contained in:
parent
d9ae1f5e56
commit
cb8165eb14
|
@ -38,14 +38,17 @@ var Camera = React.createClass({
|
||||||
render: function() {
|
render: function() {
|
||||||
var style = flattenStyle([styles.base, this.props.style]);
|
var style = flattenStyle([styles.base, this.props.style]);
|
||||||
var aspect = this.props.aspect || 'Fill';
|
var aspect = this.props.aspect || 'Fill';
|
||||||
|
var camera = this.props.camera || 'Back';
|
||||||
var orientation = this.props.orientation || 'Portrait';
|
var orientation = this.props.orientation || 'Portrait';
|
||||||
|
|
||||||
aspect = NativeModules.CameraManager.aspects[aspect];
|
aspect = NativeModules.CameraManager.aspects[aspect];
|
||||||
|
camera = NativeModules.CameraManager.cameras[camera];
|
||||||
orientation = NativeModules.CameraManager.orientations[orientation];
|
orientation = NativeModules.CameraManager.orientations[orientation];
|
||||||
|
|
||||||
var nativeProps = merge(this.props, {
|
var nativeProps = merge(this.props, {
|
||||||
style,
|
style,
|
||||||
aspect: aspect,
|
aspect: aspect,
|
||||||
|
camera: camera,
|
||||||
orientation: orientation,
|
orientation: orientation,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -54,7 +57,11 @@ var Camera = React.createClass({
|
||||||
});
|
});
|
||||||
|
|
||||||
var RCTCamera = createReactIOSNativeComponentClass({
|
var RCTCamera = createReactIOSNativeComponentClass({
|
||||||
validAttributes: merge(ReactIOSViewAttributes.UIView, { aspect: true, orientation: true }),
|
validAttributes: merge(ReactIOSViewAttributes.UIView, {
|
||||||
|
aspect: true,
|
||||||
|
camera: true,
|
||||||
|
orientation: true
|
||||||
|
}),
|
||||||
uiViewClassName: 'RCTCamera',
|
uiViewClassName: 'RCTCamera',
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
#import <UIKit/UIKit.h>
|
#import <UIKit/UIKit.h>
|
||||||
|
#import <AVFoundation/AVFoundation.h>
|
||||||
#import "ViewfinderView.h"
|
#import "ViewfinderView.h"
|
||||||
|
|
||||||
@class AVCaptureSession;
|
@class AVCaptureSession;
|
||||||
|
@ -7,5 +8,6 @@
|
||||||
|
|
||||||
@property (nonatomic) AVCaptureSession *session;
|
@property (nonatomic) AVCaptureSession *session;
|
||||||
@property (nonatomic) ViewfinderView *viewfinder;
|
@property (nonatomic) ViewfinderView *viewfinder;
|
||||||
|
@property (nonatomic) AVCaptureDeviceInput *captureDeviceInput;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
73
RCTCamera.m
73
RCTCamera.m
|
@ -5,16 +5,42 @@
|
||||||
|
|
||||||
@implementation RCTCamera
|
@implementation RCTCamera
|
||||||
|
|
||||||
- (void)setOrientation:(NSInteger)orientation
|
|
||||||
{
|
|
||||||
[[(AVCaptureVideoPreviewLayer *)[[self viewfinder] layer] connection] setVideoOrientation:orientation];
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)setAspect:(NSString *)aspect
|
- (void)setAspect:(NSString *)aspect
|
||||||
{
|
{
|
||||||
[(AVCaptureVideoPreviewLayer *)[[self viewfinder] layer] setVideoGravity: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
|
- (id)init
|
||||||
{
|
{
|
||||||
if ((self = [super init])) {
|
if ((self = [super init])) {
|
||||||
|
@ -27,30 +53,18 @@
|
||||||
|
|
||||||
NSError *error = nil;
|
NSError *error = nil;
|
||||||
|
|
||||||
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
|
AVCaptureDevice *captureDevice = [self deviceWithMediaType:AVMediaTypeVideo preferringPosition:AVCaptureDevicePositionBack];
|
||||||
AVCaptureDevice *captureDevice = [devices firstObject];
|
|
||||||
|
|
||||||
AVCaptureDevicePosition position = AVCaptureDevicePositionBack;
|
_captureDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
|
||||||
|
|
||||||
for (AVCaptureDevice *device in devices)
|
|
||||||
{
|
|
||||||
if ([device position] == position)
|
|
||||||
{
|
|
||||||
captureDevice = device;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
AVCaptureDeviceInput *captureDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
|
|
||||||
|
|
||||||
if (error)
|
if (error)
|
||||||
{
|
{
|
||||||
NSLog(@"%@", 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] connection] setVideoOrientation:AVCaptureVideoOrientationPortrait];
|
||||||
[(AVCaptureVideoPreviewLayer *)[[self viewfinder] layer] setVideoGravity:AVLayerVideoGravityResizeAspectFill];
|
[(AVCaptureVideoPreviewLayer *)[[self viewfinder] layer] setVideoGravity:AVLayerVideoGravityResizeAspectFill];
|
||||||
|
@ -62,6 +76,23 @@
|
||||||
return self;
|
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 *)reactSubviews
|
||||||
{
|
{
|
||||||
NSArray *subviews = @[_viewfinder];
|
NSArray *subviews = @[_viewfinder];
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
RCT_EXPORT_VIEW_PROPERTY(aspect, NSString);
|
RCT_EXPORT_VIEW_PROPERTY(aspect, NSString);
|
||||||
|
RCT_EXPORT_VIEW_PROPERTY(camera, NSInteger);
|
||||||
RCT_EXPORT_VIEW_PROPERTY(orientation, NSInteger);
|
RCT_EXPORT_VIEW_PROPERTY(orientation, NSInteger);
|
||||||
|
|
||||||
- (NSDictionary *)constantsToExport
|
- (NSDictionary *)constantsToExport
|
||||||
|
@ -23,6 +24,10 @@ RCT_EXPORT_VIEW_PROPERTY(orientation, NSInteger);
|
||||||
@"Fit": AVLayerVideoGravityResizeAspect,
|
@"Fit": AVLayerVideoGravityResizeAspect,
|
||||||
@"Fill": AVLayerVideoGravityResizeAspectFill
|
@"Fill": AVLayerVideoGravityResizeAspectFill
|
||||||
},
|
},
|
||||||
|
@"cameras": @{
|
||||||
|
@"Front": @(AVCaptureDevicePositionFront),
|
||||||
|
@"Back": @(AVCaptureDevicePositionBack)
|
||||||
|
},
|
||||||
@"orientations": @{
|
@"orientations": @{
|
||||||
@"LandscapeLeft": @(AVCaptureVideoOrientationLandscapeLeft),
|
@"LandscapeLeft": @(AVCaptureVideoOrientationLandscapeLeft),
|
||||||
@"LandscapeRight": @(AVCaptureVideoOrientationLandscapeRight),
|
@"LandscapeRight": @(AVCaptureVideoOrientationLandscapeRight),
|
||||||
|
|
Loading…
Reference in New Issue