From 6d3af3817813c1875b95e4471aeb292705ea4da6 Mon Sep 17 00:00:00 2001 From: Andreas Keller Date: Mon, 15 Jun 2015 20:58:36 +0200 Subject: [PATCH] Adds torchMode property --- Camera.ios.js | 13 +++++++++++-- RCTCamera.m | 5 +++++ RCTCameraManager.h | 7 +++++++ RCTCameraManager.m | 23 +++++++++++++++++++++++ README.md | 9 +++++++++ 5 files changed, 55 insertions(+), 2 deletions(-) diff --git a/Camera.ios.js b/Camera.ios.js index 5ab85fe..243f0bd 100644 --- a/Camera.ios.js +++ b/Camera.ios.js @@ -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 @@ -192,6 +200,7 @@ var RCTCamera = createReactNativeComponentClass({ type: true, orientation: true, flashMode: true, + torchMode: true }), uiViewClassName: 'RCTCamera', }); diff --git a/RCTCamera.m b/RCTCamera.m index fb63b21..7be3c8b 100644 --- a/RCTCamera.m +++ b/RCTCamera.m @@ -53,6 +53,11 @@ [self.manager changeFlashMode:flashMode]; } +- (void)setTorchMode:(NSInteger)torchMode +{ + [self.manager changeTorchMode:torchMode]; +} + - (id)initWithManager:(RCTCameraManager*)manager { diff --git a/RCTCameraManager.h b/RCTCameraManager.h index a8e4d6f..8474edc 100644 --- a/RCTCameraManager.h +++ b/RCTCameraManager.h @@ -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 @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; diff --git a/RCTCameraManager.m b/RCTCameraManager.m index 3a17940..b7d0c52 100644 --- a/RCTCameraManager.m +++ b/RCTCameraManager.m @@ -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]; diff --git a/README.md b/README.md index 837a34d..5b9c09e 100644 --- a/README.md +++ b/README.md @@ -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 `` element, then you can use `this.refs.camera.capture(cb)`, etc. inside your component.