Merge pull request #49 from andreaskeller/torchMode

Adds torch mode property
This commit is contained in:
Loch Wansbrough 2015-06-15 21:32:33 -07:00
commit e358622ff2
5 changed files with 55 additions and 2 deletions

View File

@ -16,7 +16,8 @@ var constants = {
CaptureMode: NativeModules.CameraManager.CaptureMode, CaptureMode: NativeModules.CameraManager.CaptureMode,
CaptureTarget: NativeModules.CameraManager.CaptureTarget, CaptureTarget: NativeModules.CameraManager.CaptureTarget,
Orientation: NativeModules.CameraManager.Orientation, Orientation: NativeModules.CameraManager.Orientation,
FlashMode: NativeModules.CameraManager.FlashMode FlashMode: NativeModules.CameraManager.FlashMode,
TorchMode: NativeModules.CameraManager.TorchMode,
}; };
var Camera = React.createClass({ var Camera = React.createClass({
@ -44,6 +45,10 @@ var Camera = React.createClass({
flashMode: PropTypes.oneOfType([ flashMode: PropTypes.oneOfType([
PropTypes.string, PropTypes.string,
PropTypes.number PropTypes.number
]),
torchMode: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number
]) ])
}, },
@ -62,6 +67,7 @@ var Camera = React.createClass({
captureMode: constants.CaptureMode.still, captureMode: constants.CaptureMode.still,
captureTarget: constants.CaptureTarget.memory, captureTarget: constants.CaptureTarget.memory,
flashMode: constants.FlashMode.off, flashMode: constants.FlashMode.off,
torchMode: constants.TorchMode.off
}; };
}, },
@ -89,7 +95,8 @@ var Camera = React.createClass({
var aspect = this.props.aspect, var aspect = this.props.aspect,
type = this.props.type, type = this.props.type,
orientation = this.props.orientation, orientation = this.props.orientation,
flashMode = this.props.flashMode; flashMode = this.props.flashMode,
torchMode = this.props.torchMode;
var legacyProps = { var legacyProps = {
aspect: { aspect: {
@ -152,6 +159,7 @@ var Camera = React.createClass({
type: type, type: type,
orientation: orientation, orientation: orientation,
flashMode: flashMode, flashMode: flashMode,
torchMode: torchMode
}); });
return <RCTCamera {... nativeProps} /> return <RCTCamera {... nativeProps} />
@ -192,6 +200,7 @@ var RCTCamera = createReactNativeComponentClass({
type: true, type: true,
orientation: true, orientation: true,
flashMode: true, flashMode: true,
torchMode: true
}), }),
uiViewClassName: 'RCTCamera', uiViewClassName: 'RCTCamera',
}); });

View File

@ -53,6 +53,11 @@
[self.manager changeFlashMode:flashMode]; [self.manager changeFlashMode:flashMode];
} }
- (void)setTorchMode:(NSInteger)torchMode
{
[self.manager changeTorchMode:torchMode];
}
- (id)initWithManager:(RCTCameraManager*)manager - (id)initWithManager:(RCTCameraManager*)manager
{ {

View File

@ -39,6 +39,12 @@ typedef NS_ENUM(NSInteger, RCTCameraFlashMode) {
RCTCameraFlashModeAuto = AVCaptureFlashModeAuto RCTCameraFlashModeAuto = AVCaptureFlashModeAuto
}; };
typedef NS_ENUM(NSInteger, RCTCameraTorchMode) {
RCTCameraTorchModeOff = AVCaptureTorchModeOff,
RCTCameraTorchModeOn = AVCaptureTorchModeOn,
RCTCameraTorchModeAuto = AVCaptureTorchModeAuto
};
@interface RCTCameraManager : RCTViewManager<AVCaptureMetadataOutputObjectsDelegate> @interface RCTCameraManager : RCTViewManager<AVCaptureMetadataOutputObjectsDelegate>
@property (nonatomic) dispatch_queue_t sessionQueue; @property (nonatomic) dispatch_queue_t sessionQueue;
@ -54,6 +60,7 @@ typedef NS_ENUM(NSInteger, RCTCameraFlashMode) {
- (void)changeCamera:(NSInteger)camera; - (void)changeCamera:(NSInteger)camera;
- (void)changeOrientation:(NSInteger)orientation; - (void)changeOrientation:(NSInteger)orientation;
- (void)changeFlashMode:(NSInteger)flashMode; - (void)changeFlashMode:(NSInteger)flashMode;
- (void)changeTorchMode:(NSInteger)torchMode;
- (AVCaptureDevice *)deviceWithMediaType:(NSString *)mediaType preferringPosition:(AVCaptureDevicePosition)position; - (AVCaptureDevice *)deviceWithMediaType:(NSString *)mediaType preferringPosition:(AVCaptureDevicePosition)position;
- (void)capture:(NSDictionary*)options callback:(RCTResponseSenderBlock)callback; - (void)capture:(NSDictionary*)options callback:(RCTResponseSenderBlock)callback;

View File

@ -22,6 +22,7 @@ RCT_EXPORT_VIEW_PROPERTY(aspect, NSInteger);
RCT_EXPORT_VIEW_PROPERTY(type, NSInteger); RCT_EXPORT_VIEW_PROPERTY(type, NSInteger);
RCT_EXPORT_VIEW_PROPERTY(orientation, NSInteger); RCT_EXPORT_VIEW_PROPERTY(orientation, NSInteger);
RCT_EXPORT_VIEW_PROPERTY(flashMode, NSInteger); RCT_EXPORT_VIEW_PROPERTY(flashMode, NSInteger);
RCT_EXPORT_VIEW_PROPERTY(torchMode, NSInteger);
- (NSDictionary *)constantsToExport - (NSDictionary *)constantsToExport
{ {
@ -55,6 +56,11 @@ RCT_EXPORT_VIEW_PROPERTY(flashMode, NSInteger);
@"off": @(RCTCameraFlashModeOff), @"off": @(RCTCameraFlashModeOff),
@"on": @(RCTCameraFlashModeOn), @"on": @(RCTCameraFlashModeOn),
@"auto": @(RCTCameraFlashModeAuto) @"auto": @(RCTCameraFlashModeAuto)
},
@"TorchMode": @{
@"off": @(RCTCameraTorchModeOff),
@"on": @(RCTCameraTorchModeOn),
@"auto": @(RCTCameraTorchModeAuto)
} }
}; };
} }
@ -185,6 +191,23 @@ RCT_EXPORT_METHOD(changeOrientation:(NSInteger)orientation) {
self.previewLayer.connection.videoOrientation = orientation; self.previewLayer.connection.videoOrientation = orientation;
} }
RCT_EXPORT_METHOD(changeTorchMode:(NSInteger)torchMode) {
AVCaptureDevice *device = [self.captureDeviceInput device];
NSError *error = nil;
if ([device hasTorch]) {
if ([device lockForConfiguration:&error])
{
[device setTorchMode: torchMode];
[device unlockForConfiguration];
}
else
{
NSLog(@"%@", error);
}
}
}
RCT_EXPORT_METHOD(capture:(NSDictionary *)options callback:(RCTResponseSenderBlock)callback) { RCT_EXPORT_METHOD(capture:(NSDictionary *)options callback:(RCTResponseSenderBlock)callback) {
NSInteger captureMode = [[options valueForKey:@"mode"] intValue]; NSInteger captureMode = [[options valueForKey:@"mode"] intValue];
NSInteger captureTarget = [[options valueForKey:@"target"] intValue]; NSInteger captureTarget = [[options valueForKey:@"target"] intValue];

View File

@ -158,6 +158,15 @@ Values:
Use the `flashMode` property to specify the camera flash mode. Use the `flashMode` property to specify the camera flash mode.
#### `torchMode`
Values:
`Camera.constants.TorchMode.on`,
`Camera.constants.TorchMode.off`,
`Camera.constants.TorchMode.auto`
Use the `torchMode` property to specify the camera torch mode.
## Component methods ## Component methods
You can access component methods by adding a `ref` (ie. `ref="camera"`) prop to your `<Camera>` element, then you can use `this.refs.camera.capture(cb)`, etc. inside your component. You can access component methods by adding a `ref` (ie. `ref="camera"`) prop to your `<Camera>` element, then you can use `this.refs.camera.capture(cb)`, etc. inside your component.