From 3dd33d842e8fdfc00537a1b9dc164618a0a4174b Mon Sep 17 00:00:00 2001 From: guodong Date: Sun, 31 May 2015 00:36:55 +0800 Subject: [PATCH] Add flash mode setting --- Camera.ios.js | 25 ++++++++++++++++++++----- RCTCamera.m | 5 +++++ RCTCameraManager.h | 7 +++++++ RCTCameraManager.m | 16 +++++++++++++--- README.md | 11 ++++++++++- 5 files changed, 55 insertions(+), 9 deletions(-) diff --git a/Camera.ios.js b/Camera.ios.js index 2b68701..5ab85fe 100644 --- a/Camera.ios.js +++ b/Camera.ios.js @@ -15,7 +15,8 @@ var constants = { Type: NativeModules.CameraManager.Type, CaptureMode: NativeModules.CameraManager.CaptureMode, CaptureTarget: NativeModules.CameraManager.CaptureTarget, - Orientation: NativeModules.CameraManager.Orientation + Orientation: NativeModules.CameraManager.Orientation, + FlashMode: NativeModules.CameraManager.FlashMode }; var Camera = React.createClass({ @@ -39,6 +40,10 @@ var Camera = React.createClass({ orientation: PropTypes.oneOfType([ PropTypes.string, PropTypes.number + ]), + flashMode: PropTypes.oneOfType([ + PropTypes.string, + PropTypes.number ]) }, @@ -55,7 +60,8 @@ var Camera = React.createClass({ type: constants.Type.back, orientation: constants.Orientation.auto, captureMode: constants.CaptureMode.still, - captureTarget: constants.CaptureTarget.memory + captureTarget: constants.CaptureTarget.memory, + flashMode: constants.FlashMode.off, }; }, @@ -82,7 +88,8 @@ var Camera = React.createClass({ var aspect = this.props.aspect, type = this.props.type, - orientation = this.props.orientation; + orientation = this.props.orientation, + flashMode = this.props.flashMode; var legacyProps = { aspect: { @@ -99,12 +106,18 @@ var Camera = React.createClass({ type: { Front: 'front', Back: 'back' + }, + flashMode: { + Off: 'off', + On: 'on', + Auto: 'auto' } }; var foundLegacyAspect = legacyProps.aspect[aspect]; var foundLegacyOrientation = legacyProps.orientation[orientation]; var foundLegacyType = legacyProps.type[type]; + var foundLegacyFlashMode = legacyProps.flashMode[flashMode]; if (__DEV__) { if (foundLegacyAspect) { @@ -137,7 +150,8 @@ var Camera = React.createClass({ style, aspect: aspect, type: type, - orientation: orientation + orientation: orientation, + flashMode: flashMode, }); return @@ -176,7 +190,8 @@ var RCTCamera = createReactNativeComponentClass({ validAttributes: merge(ReactNativeViewAttributes.UIView, { aspect: true, type: true, - orientation: true + orientation: true, + flashMode: true, }), uiViewClassName: 'RCTCamera', }); diff --git a/RCTCamera.m b/RCTCamera.m index ed861ff..372eba7 100644 --- a/RCTCamera.m +++ b/RCTCamera.m @@ -48,6 +48,11 @@ } } +- (void)setFlashMode:(NSInteger)flashMode +{ + [self.manager changeFlashMode:flashMode]; +} + - (id)initWithManager:(RCTCameraManager*)manager { diff --git a/RCTCameraManager.h b/RCTCameraManager.h index 6293eb2..887d890 100644 --- a/RCTCameraManager.h +++ b/RCTCameraManager.h @@ -32,6 +32,12 @@ typedef NS_ENUM(NSInteger, RCTCameraType) { RCTCameraTypeBack = AVCaptureDevicePositionBack }; +typedef NS_ENUM(NSInteger, RCTCameraFlashMode) { + RCTCameraFlashModeOff = AVCaptureFlashModeOff, + RCTCameraFlashModeOn = AVCaptureFlashModeOn, + RCTCameraFlashModeAuto = AVCaptureFlashModeAuto +}; + @interface RCTCameraManager : RCTViewManager @property (nonatomic) dispatch_queue_t sessionQueue; @@ -46,6 +52,7 @@ typedef NS_ENUM(NSInteger, RCTCameraType) { - (void)changeAspect:(NSString *)aspect; - (void)changeCamera:(NSInteger)camera; - (void)changeOrientation:(NSInteger)orientation; +- (void)changeFlashMode:(NSInteger)flashMode; - (AVCaptureDevice *)deviceWithMediaType:(NSString *)mediaType preferringPosition:(AVCaptureDevicePosition)position; - (void)capture:(NSDictionary*)options callback:(RCTResponseSenderBlock)callback; diff --git a/RCTCameraManager.m b/RCTCameraManager.m index fd5349c..c77a0f3 100644 --- a/RCTCameraManager.m +++ b/RCTCameraManager.m @@ -20,6 +20,7 @@ RCT_EXPORT_MODULE(); RCT_EXPORT_VIEW_PROPERTY(aspect, NSInteger); RCT_EXPORT_VIEW_PROPERTY(type, NSInteger); RCT_EXPORT_VIEW_PROPERTY(orientation, NSInteger); +RCT_EXPORT_VIEW_PROPERTY(flashMode, NSInteger); - (NSDictionary *)constantsToExport { @@ -47,6 +48,11 @@ RCT_EXPORT_VIEW_PROPERTY(orientation, NSInteger); @"landscapeRight": @(RCTCameraOrientationLandscapeRight), @"portrait": @(RCTCameraOrientationPortrait), @"portraitUpsideDown": @(RCTCameraOrientationPortraitUpsideDown) + }, + @"FlashMode": @{ + @"off": @(RCTCameraFlashModeOff), + @"on": @(RCTCameraFlashModeOn), + @"auto": @(RCTCameraFlashModeAuto) } }; } @@ -110,7 +116,7 @@ RCT_EXPORT_VIEW_PROPERTY(orientation, NSInteger); [strongSelf.session startRunning]; }); }]]; - + [self.session startRunning]; }); } @@ -126,6 +132,10 @@ RCT_EXPORT_METHOD(checkDeviceAuthorizationStatus:(RCTResponseSenderBlock) callba }]; } +RCT_EXPORT_METHOD(changeFlashMode:(NSInteger)flashMode) { + AVCaptureDevice *currentCaptureDevice = [self.captureDeviceInput device]; + [self setFlashMode:flashMode forDevice:currentCaptureDevice]; +} RCT_EXPORT_METHOD(changeCamera:(NSInteger)camera) { AVCaptureDevice *currentCaptureDevice = [self.captureDeviceInput device]; @@ -178,7 +188,7 @@ RCT_EXPORT_METHOD(changeOrientation:(NSInteger)orientation) { RCT_EXPORT_METHOD(capture:(NSDictionary *)options callback:(RCTResponseSenderBlock)callback) { NSInteger captureMode = [[options valueForKey:@"mode"] intValue]; NSInteger captureTarget = [[options valueForKey:@"target"] intValue]; - + if (captureMode == RCTCameraCaptureModeStill) { [self captureStill:captureTarget callback:callback]; } @@ -196,7 +206,7 @@ RCT_EXPORT_METHOD(capture:(NSDictionary *)options callback:(RCTResponseSenderBlo NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer]; UIImage *image = [UIImage imageWithData:imageData]; UIImage *rotatedImage = [image resizedImage:CGSizeMake(image.size.width, image.size.height) interpolationQuality:kCGInterpolationDefault]; - + NSString *responseString; if (target == RCTCameraCaptureTargetMemory) { diff --git a/README.md b/README.md index fb1cddb..837a34d 100644 --- a/README.md +++ b/README.md @@ -149,6 +149,15 @@ Event contains `data` (the data in the barcode) and `bounds` (the rectangle whic *TODO: Only emit one event for each barcode scanned.* +#### `flashMode` + +Values: +`Camera.constants.FlashMode.on`, +`Camera.constants.FlashMode.off`, +`Camera.constants.FlashMode.auto` + +Use the `flashMode` property to specify the camera flash 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. @@ -164,7 +173,7 @@ This component supports subviews, so if you wish to use the camera view as a bac These are some features I think would be important/beneficial to have included with this module. Pull requests welcome! - [ ] Video support -- [ ] Flash mode setting +- [x] Flash mode setting - [x] Automatic orientation adjustment - [ ] Tap to focus - [ ] Optional facial recognition (w/ ability to style box around face)