Skip to content

Commit

Permalink
Adds torchMode property
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Keller committed Jun 15, 2015
1 parent 7922512 commit 6d3af38
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
13 changes: 11 additions & 2 deletions Camera.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -44,6 +45,10 @@ var Camera = React.createClass({
flashMode: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number
]),
torchMode: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number
])
},

Expand All @@ -62,6 +67,7 @@ var Camera = React.createClass({
captureMode: constants.CaptureMode.still,
captureTarget: constants.CaptureTarget.memory,
flashMode: constants.FlashMode.off,
torchMode: constants.TorchMode.off
};
},

Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -152,6 +159,7 @@ var Camera = React.createClass({
type: type,
orientation: orientation,
flashMode: flashMode,
torchMode: torchMode
});

return <RCTCamera {... nativeProps} />
Expand Down Expand Up @@ -192,6 +200,7 @@ var RCTCamera = createReactNativeComponentClass({
type: true,
orientation: true,
flashMode: true,
torchMode: true
}),
uiViewClassName: 'RCTCamera',
});
Expand Down
5 changes: 5 additions & 0 deletions RCTCamera.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ - (void)setFlashMode:(NSInteger)flashMode
[self.manager changeFlashMode:flashMode];
}

- (void)setTorchMode:(NSInteger)torchMode
{
[self.manager changeTorchMode:torchMode];
}

- (id)initWithManager:(RCTCameraManager*)manager
{

Expand Down
7 changes: 7 additions & 0 deletions RCTCameraManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down
23 changes: 23 additions & 0 deletions RCTCameraManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ - (UIView *)view
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
{
Expand Down Expand Up @@ -55,6 +56,11 @@ - (NSDictionary *)constantsToExport
@"off": @(RCTCameraFlashModeOff),
@"on": @(RCTCameraFlashModeOn),
@"auto": @(RCTCameraFlashModeAuto)
},
@"TorchMode": @{
@"off": @(RCTCameraTorchModeOff),
@"on": @(RCTCameraTorchModeOn),
@"auto": @(RCTCameraTorchModeAuto)
}
};
}
Expand Down Expand Up @@ -185,6 +191,23 @@ - (id)init {
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];
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 6d3af38

Please sign in to comment.