Add flash mode setting

This commit is contained in:
guodong 2015-05-31 00:36:55 +08:00
parent ebe995a177
commit 3dd33d842e
5 changed files with 55 additions and 9 deletions

View File

@ -15,7 +15,8 @@ var constants = {
Type: NativeModules.CameraManager.Type, Type: NativeModules.CameraManager.Type,
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
}; };
var Camera = React.createClass({ var Camera = React.createClass({
@ -39,6 +40,10 @@ var Camera = React.createClass({
orientation: PropTypes.oneOfType([ orientation: PropTypes.oneOfType([
PropTypes.string, PropTypes.string,
PropTypes.number PropTypes.number
]),
flashMode: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number
]) ])
}, },
@ -55,7 +60,8 @@ var Camera = React.createClass({
type: constants.Type.back, type: constants.Type.back,
orientation: constants.Orientation.auto, orientation: constants.Orientation.auto,
captureMode: constants.CaptureMode.still, 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, 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;
var legacyProps = { var legacyProps = {
aspect: { aspect: {
@ -99,12 +106,18 @@ var Camera = React.createClass({
type: { type: {
Front: 'front', Front: 'front',
Back: 'back' Back: 'back'
},
flashMode: {
Off: 'off',
On: 'on',
Auto: 'auto'
} }
}; };
var foundLegacyAspect = legacyProps.aspect[aspect]; var foundLegacyAspect = legacyProps.aspect[aspect];
var foundLegacyOrientation = legacyProps.orientation[orientation]; var foundLegacyOrientation = legacyProps.orientation[orientation];
var foundLegacyType = legacyProps.type[type]; var foundLegacyType = legacyProps.type[type];
var foundLegacyFlashMode = legacyProps.flashMode[flashMode];
if (__DEV__) { if (__DEV__) {
if (foundLegacyAspect) { if (foundLegacyAspect) {
@ -137,7 +150,8 @@ var Camera = React.createClass({
style, style,
aspect: aspect, aspect: aspect,
type: type, type: type,
orientation: orientation orientation: orientation,
flashMode: flashMode,
}); });
return <RCTCamera {... nativeProps} /> return <RCTCamera {... nativeProps} />
@ -176,7 +190,8 @@ var RCTCamera = createReactNativeComponentClass({
validAttributes: merge(ReactNativeViewAttributes.UIView, { validAttributes: merge(ReactNativeViewAttributes.UIView, {
aspect: true, aspect: true,
type: true, type: true,
orientation: true orientation: true,
flashMode: true,
}), }),
uiViewClassName: 'RCTCamera', uiViewClassName: 'RCTCamera',
}); });

View File

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

View File

@ -32,6 +32,12 @@ typedef NS_ENUM(NSInteger, RCTCameraType) {
RCTCameraTypeBack = AVCaptureDevicePositionBack RCTCameraTypeBack = AVCaptureDevicePositionBack
}; };
typedef NS_ENUM(NSInteger, RCTCameraFlashMode) {
RCTCameraFlashModeOff = AVCaptureFlashModeOff,
RCTCameraFlashModeOn = AVCaptureFlashModeOn,
RCTCameraFlashModeAuto = AVCaptureFlashModeAuto
};
@interface RCTCameraManager : RCTViewManager<AVCaptureMetadataOutputObjectsDelegate> @interface RCTCameraManager : RCTViewManager<AVCaptureMetadataOutputObjectsDelegate>
@property (nonatomic) dispatch_queue_t sessionQueue; @property (nonatomic) dispatch_queue_t sessionQueue;
@ -46,6 +52,7 @@ typedef NS_ENUM(NSInteger, RCTCameraType) {
- (void)changeAspect:(NSString *)aspect; - (void)changeAspect:(NSString *)aspect;
- (void)changeCamera:(NSInteger)camera; - (void)changeCamera:(NSInteger)camera;
- (void)changeOrientation:(NSInteger)orientation; - (void)changeOrientation:(NSInteger)orientation;
- (void)changeFlashMode:(NSInteger)flashMode;
- (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

@ -20,6 +20,7 @@ RCT_EXPORT_MODULE();
RCT_EXPORT_VIEW_PROPERTY(aspect, NSInteger); 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);
- (NSDictionary *)constantsToExport - (NSDictionary *)constantsToExport
{ {
@ -47,6 +48,11 @@ RCT_EXPORT_VIEW_PROPERTY(orientation, NSInteger);
@"landscapeRight": @(RCTCameraOrientationLandscapeRight), @"landscapeRight": @(RCTCameraOrientationLandscapeRight),
@"portrait": @(RCTCameraOrientationPortrait), @"portrait": @(RCTCameraOrientationPortrait),
@"portraitUpsideDown": @(RCTCameraOrientationPortraitUpsideDown) @"portraitUpsideDown": @(RCTCameraOrientationPortraitUpsideDown)
},
@"FlashMode": @{
@"off": @(RCTCameraFlashModeOff),
@"on": @(RCTCameraFlashModeOn),
@"auto": @(RCTCameraFlashModeAuto)
} }
}; };
} }
@ -110,7 +116,7 @@ RCT_EXPORT_VIEW_PROPERTY(orientation, NSInteger);
[strongSelf.session startRunning]; [strongSelf.session startRunning];
}); });
}]]; }]];
[self.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) { RCT_EXPORT_METHOD(changeCamera:(NSInteger)camera) {
AVCaptureDevice *currentCaptureDevice = [self.captureDeviceInput device]; AVCaptureDevice *currentCaptureDevice = [self.captureDeviceInput device];
@ -178,7 +188,7 @@ RCT_EXPORT_METHOD(changeOrientation:(NSInteger)orientation) {
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];
if (captureMode == RCTCameraCaptureModeStill) { if (captureMode == RCTCameraCaptureModeStill) {
[self captureStill:captureTarget callback:callback]; [self captureStill:captureTarget callback:callback];
} }
@ -196,7 +206,7 @@ RCT_EXPORT_METHOD(capture:(NSDictionary *)options callback:(RCTResponseSenderBlo
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer]; NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
UIImage *image = [UIImage imageWithData:imageData]; UIImage *image = [UIImage imageWithData:imageData];
UIImage *rotatedImage = [image resizedImage:CGSizeMake(image.size.width, image.size.height) interpolationQuality:kCGInterpolationDefault]; UIImage *rotatedImage = [image resizedImage:CGSizeMake(image.size.width, image.size.height) interpolationQuality:kCGInterpolationDefault];
NSString *responseString; NSString *responseString;
if (target == RCTCameraCaptureTargetMemory) { if (target == RCTCameraCaptureTargetMemory) {

View File

@ -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.* *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 ## 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.
@ -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! These are some features I think would be important/beneficial to have included with this module. Pull requests welcome!
- [ ] Video support - [ ] Video support
- [ ] Flash mode setting - [x] Flash mode setting
- [x] Automatic orientation adjustment - [x] Automatic orientation adjustment
- [ ] Tap to focus - [ ] Tap to focus
- [ ] Optional facial recognition (w/ ability to style box around face) - [ ] Optional facial recognition (w/ ability to style box around face)