Adds torchMode property

This commit is contained in:
Andreas Keller 2015-06-15 20:58:36 +02:00
parent 79225127c8
commit 6d3af38178
5 changed files with 55 additions and 2 deletions

View File

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

View File

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

View File

@ -39,6 +39,12 @@ typedef NS_ENUM(NSInteger, RCTCameraFlashMode) {
RCTCameraFlashModeAuto = AVCaptureFlashModeAuto
};
typedef NS_ENUM(NSInteger, RCTCameraTorchMode) {
RCTCameraTorchModeOff = AVCaptureTorchModeOff,
RCTCameraTorchModeOn = AVCaptureTorchModeOn,
RCTCameraTorchModeAuto = AVCaptureTorchModeAuto
};
@interface RCTCameraManager : RCTViewManager<AVCaptureMetadataOutputObjectsDelegate>
@property (nonatomic) dispatch_queue_t sessionQueue;
@ -54,6 +60,7 @@ typedef NS_ENUM(NSInteger, RCTCameraFlashMode) {
- (void)changeCamera:(NSInteger)camera;
- (void)changeOrientation:(NSInteger)orientation;
- (void)changeFlashMode:(NSInteger)flashMode;
- (void)changeTorchMode:(NSInteger)torchMode;
- (AVCaptureDevice *)deviceWithMediaType:(NSString *)mediaType preferringPosition:(AVCaptureDevicePosition)position;
- (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(orientation, NSInteger);
RCT_EXPORT_VIEW_PROPERTY(flashMode, NSInteger);
RCT_EXPORT_VIEW_PROPERTY(torchMode, NSInteger);
- (NSDictionary *)constantsToExport
{
@ -55,6 +56,11 @@ RCT_EXPORT_VIEW_PROPERTY(flashMode, NSInteger);
@"off": @(RCTCameraFlashModeOff),
@"on": @(RCTCameraFlashModeOn),
@"auto": @(RCTCameraFlashModeAuto)
},
@"TorchMode": @{
@"off": @(RCTCameraTorchModeOff),
@"on": @(RCTCameraTorchModeOn),
@"auto": @(RCTCameraTorchModeAuto)
}
};
}
@ -185,6 +191,23 @@ RCT_EXPORT_METHOD(changeOrientation:(NSInteger)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) {
NSInteger captureMode = [[options valueForKey:@"mode"] intValue];
NSInteger captureTarget = [[options valueForKey:@"target"] intValue];

View File

@ -158,6 +158,15 @@ Values:
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
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.