2015-09-08 11:12:22 +00:00
|
|
|
var React = require('react-native');
|
|
|
|
var { StyleSheet, requireNativeComponent, PropTypes, NativeModules, DeviceEventEmitter } = React;
|
2015-04-01 01:02:57 +00:00
|
|
|
|
2015-09-08 11:12:22 +00:00
|
|
|
var CAMERA_REF = 'camera';
|
|
|
|
|
2015-05-01 08:16:32 +00:00
|
|
|
var constants = {
|
|
|
|
Aspect: NativeModules.CameraManager.Aspect,
|
2015-07-02 20:09:35 +00:00
|
|
|
BarCodeType: NativeModules.CameraManager.BarCodeType,
|
2015-05-01 21:33:23 +00:00
|
|
|
Type: NativeModules.CameraManager.Type,
|
2015-05-01 08:16:32 +00:00
|
|
|
CaptureMode: NativeModules.CameraManager.CaptureMode,
|
|
|
|
CaptureTarget: NativeModules.CameraManager.CaptureTarget,
|
2015-05-30 16:36:55 +00:00
|
|
|
Orientation: NativeModules.CameraManager.Orientation,
|
2015-06-15 18:58:36 +00:00
|
|
|
FlashMode: NativeModules.CameraManager.FlashMode,
|
2015-12-22 21:45:17 +00:00
|
|
|
TorchMode: NativeModules.CameraManager.TorchMode
|
2015-05-01 08:16:32 +00:00
|
|
|
};
|
|
|
|
|
2015-04-01 01:02:57 +00:00
|
|
|
var Camera = React.createClass({
|
|
|
|
propTypes: {
|
2015-05-01 21:33:23 +00:00
|
|
|
aspect: PropTypes.oneOfType([
|
|
|
|
PropTypes.string,
|
|
|
|
PropTypes.number
|
|
|
|
]),
|
2015-07-10 03:11:50 +00:00
|
|
|
captureAudio: PropTypes.bool,
|
2015-05-01 21:33:23 +00:00
|
|
|
captureMode: PropTypes.oneOfType([
|
|
|
|
PropTypes.string,
|
|
|
|
PropTypes.number
|
|
|
|
]),
|
|
|
|
captureTarget: PropTypes.oneOfType([
|
|
|
|
PropTypes.string,
|
|
|
|
PropTypes.number
|
|
|
|
]),
|
|
|
|
type: PropTypes.oneOfType([
|
|
|
|
PropTypes.string,
|
|
|
|
PropTypes.number
|
|
|
|
]),
|
|
|
|
orientation: PropTypes.oneOfType([
|
|
|
|
PropTypes.string,
|
|
|
|
PropTypes.number
|
2015-05-30 16:36:55 +00:00
|
|
|
]),
|
|
|
|
flashMode: PropTypes.oneOfType([
|
|
|
|
PropTypes.string,
|
|
|
|
PropTypes.number
|
2015-06-15 18:58:36 +00:00
|
|
|
]),
|
|
|
|
torchMode: PropTypes.oneOfType([
|
|
|
|
PropTypes.string,
|
|
|
|
PropTypes.number
|
2015-08-11 13:23:06 +00:00
|
|
|
]),
|
2015-08-29 08:24:05 +00:00
|
|
|
defaultOnFocusComponent: PropTypes.bool,
|
2015-08-11 13:23:06 +00:00
|
|
|
onFocusChanged: PropTypes.func,
|
|
|
|
onZoomChanged: PropTypes.func
|
2015-04-01 01:02:57 +00:00
|
|
|
},
|
|
|
|
|
2015-09-08 11:12:22 +00:00
|
|
|
setNativeProps(props) {
|
|
|
|
this.refs[CAMERA_REF].setNativeProps(props);
|
2015-04-01 01:02:57 +00:00
|
|
|
},
|
|
|
|
|
2015-05-01 08:16:32 +00:00
|
|
|
getDefaultProps() {
|
|
|
|
return {
|
|
|
|
aspect: constants.Aspect.fill,
|
2015-05-01 21:33:23 +00:00
|
|
|
type: constants.Type.back,
|
|
|
|
orientation: constants.Orientation.auto,
|
2015-07-10 03:11:50 +00:00
|
|
|
captureAudio: true,
|
2015-05-01 08:16:32 +00:00
|
|
|
captureMode: constants.CaptureMode.still,
|
2015-07-11 00:02:58 +00:00
|
|
|
captureTarget: constants.CaptureTarget.cameraRoll,
|
2015-05-30 16:36:55 +00:00
|
|
|
flashMode: constants.FlashMode.off,
|
2015-06-15 18:58:36 +00:00
|
|
|
torchMode: constants.TorchMode.off
|
2015-05-01 08:16:32 +00:00
|
|
|
};
|
2015-04-30 07:10:21 +00:00
|
|
|
},
|
|
|
|
|
2015-05-01 08:16:32 +00:00
|
|
|
getInitialState() {
|
2015-04-01 01:02:57 +00:00
|
|
|
return {
|
2015-07-02 20:04:25 +00:00
|
|
|
isAuthorized: false,
|
|
|
|
isRecording: false
|
2015-04-01 01:02:57 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2015-05-01 08:16:32 +00:00
|
|
|
componentWillMount() {
|
2015-04-01 01:02:57 +00:00
|
|
|
NativeModules.CameraManager.checkDeviceAuthorizationStatus((function(err, isAuthorized) {
|
|
|
|
this.state.isAuthorized = isAuthorized;
|
|
|
|
this.setState(this.state);
|
|
|
|
}).bind(this));
|
2015-04-19 22:53:30 +00:00
|
|
|
this.cameraBarCodeReadListener = DeviceEventEmitter.addListener('CameraBarCodeRead', this._onBarCodeRead);
|
|
|
|
},
|
|
|
|
|
2015-05-01 08:16:32 +00:00
|
|
|
componentWillUnmount() {
|
2015-04-19 22:53:30 +00:00
|
|
|
this.cameraBarCodeReadListener.remove();
|
2015-12-22 21:45:17 +00:00
|
|
|
|
2015-07-02 20:04:25 +00:00
|
|
|
if (this.state.isRecording) {
|
2015-09-17 23:47:45 +00:00
|
|
|
this.stopCapture();
|
2015-07-02 20:04:25 +00:00
|
|
|
}
|
2015-04-01 01:02:57 +00:00
|
|
|
},
|
|
|
|
|
2015-05-01 08:16:32 +00:00
|
|
|
render() {
|
2015-09-08 11:12:22 +00:00
|
|
|
var style = [styles.base, this.props.style];
|
2015-04-01 02:27:18 +00:00
|
|
|
|
2015-05-01 08:16:32 +00:00
|
|
|
var aspect = this.props.aspect,
|
|
|
|
type = this.props.type,
|
2015-05-30 16:36:55 +00:00
|
|
|
orientation = this.props.orientation,
|
2015-06-15 18:58:36 +00:00
|
|
|
flashMode = this.props.flashMode,
|
|
|
|
torchMode = this.props.torchMode;
|
2015-05-01 08:16:32 +00:00
|
|
|
|
2015-05-01 21:33:23 +00:00
|
|
|
var legacyProps = {
|
|
|
|
aspect: {
|
2015-05-02 08:13:40 +00:00
|
|
|
Fill: 'fill',
|
|
|
|
Fit: 'fit',
|
|
|
|
Stretch: 'stretch'
|
2015-05-01 21:33:23 +00:00
|
|
|
},
|
|
|
|
orientation: {
|
2015-05-02 08:13:40 +00:00
|
|
|
LandscapeLeft: 'landscapeLeft',
|
|
|
|
LandscapeRight: 'landscapeRight',
|
|
|
|
Portrait: 'portrait',
|
|
|
|
PortraitUpsideDown: 'portraitUpsideDown'
|
2015-05-01 21:33:23 +00:00
|
|
|
},
|
|
|
|
type: {
|
2015-05-02 08:13:40 +00:00
|
|
|
Front: 'front',
|
|
|
|
Back: 'back'
|
2015-05-01 21:33:23 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (typeof aspect === 'string') {
|
|
|
|
aspect = constants.Aspect[aspect];
|
|
|
|
}
|
2015-12-22 21:45:17 +00:00
|
|
|
|
2015-07-02 20:07:32 +00:00
|
|
|
if (typeof flashMode === 'string') {
|
|
|
|
flashMode = constants.FlashMode[flashMode];
|
|
|
|
}
|
2015-05-01 21:33:23 +00:00
|
|
|
|
|
|
|
if (typeof orientation === 'string') {
|
|
|
|
orientation = constants.Orientation[orientation];
|
2015-05-01 08:16:32 +00:00
|
|
|
}
|
2015-12-22 21:45:17 +00:00
|
|
|
|
2015-07-02 20:36:47 +00:00
|
|
|
if (typeof torchMode === 'string') {
|
|
|
|
torchMode = constants.TorchMode[torchMode];
|
|
|
|
}
|
2015-05-01 08:16:32 +00:00
|
|
|
|
2015-05-01 21:33:23 +00:00
|
|
|
if (typeof type === 'string') {
|
|
|
|
type = constants.Type[type];
|
2015-05-01 08:16:32 +00:00
|
|
|
}
|
2015-04-01 01:02:57 +00:00
|
|
|
|
2015-09-22 03:10:43 +00:00
|
|
|
var nativeProps = Object.assign({}, this.props, {
|
2015-04-01 01:02:57 +00:00
|
|
|
style,
|
2015-04-01 02:27:18 +00:00
|
|
|
aspect: aspect,
|
2015-04-08 20:51:31 +00:00
|
|
|
type: type,
|
2015-05-30 16:36:55 +00:00
|
|
|
orientation: orientation,
|
|
|
|
flashMode: flashMode,
|
2015-06-15 18:58:36 +00:00
|
|
|
torchMode: torchMode
|
2015-04-01 01:02:57 +00:00
|
|
|
});
|
|
|
|
|
2015-09-08 11:12:22 +00:00
|
|
|
return <RCTCamera ref={CAMERA_REF} {... nativeProps} />;
|
2015-04-01 01:02:57 +00:00
|
|
|
},
|
2015-04-02 09:40:03 +00:00
|
|
|
|
2015-04-19 22:53:30 +00:00
|
|
|
_onBarCodeRead(e) {
|
|
|
|
this.props.onBarCodeRead && this.props.onBarCodeRead(e);
|
|
|
|
},
|
|
|
|
|
2015-05-01 08:16:32 +00:00
|
|
|
capture(options, cb) {
|
2015-04-02 09:40:03 +00:00
|
|
|
|
2015-05-01 08:16:32 +00:00
|
|
|
if (arguments.length == 1) {
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
options = Object.assign({}, {
|
2015-07-10 03:11:50 +00:00
|
|
|
audio: this.props.captureAudio,
|
2015-05-01 08:16:32 +00:00
|
|
|
mode: this.props.captureMode,
|
|
|
|
target: this.props.captureTarget
|
|
|
|
}, options);
|
2015-05-01 04:04:16 +00:00
|
|
|
|
2015-05-01 21:33:23 +00:00
|
|
|
if (typeof options.mode === 'string') {
|
|
|
|
options.mode = constants.CaptureMode[options.mode];
|
|
|
|
}
|
2015-12-22 21:45:17 +00:00
|
|
|
|
2015-07-02 20:04:25 +00:00
|
|
|
if (options.mode === constants.CaptureMode.video) {
|
|
|
|
options.totalSeconds = (options.totalSeconds > -1 ? options.totalSeconds : -1);
|
|
|
|
options.preferredTimeScale = options.preferredTimeScale || 30;
|
|
|
|
this.setState({ isRecording: true });
|
|
|
|
}
|
2015-05-01 21:33:23 +00:00
|
|
|
|
|
|
|
if (typeof options.target === 'string') {
|
2015-05-04 19:27:36 +00:00
|
|
|
options.target = constants.CaptureTarget[options.target];
|
2015-05-01 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
2015-05-01 08:16:32 +00:00
|
|
|
NativeModules.CameraManager.capture(options, cb);
|
2015-06-14 17:16:09 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
stopCapture() {
|
2015-07-10 03:11:50 +00:00
|
|
|
if (this.state.isRecording) {
|
|
|
|
NativeModules.CameraManager.stopCapture();
|
|
|
|
this.setState({ isRecording: false });
|
|
|
|
}
|
2015-04-02 09:40:03 +00:00
|
|
|
}
|
2015-05-01 04:04:16 +00:00
|
|
|
|
2015-04-01 01:02:57 +00:00
|
|
|
});
|
|
|
|
|
2015-09-08 11:12:22 +00:00
|
|
|
var RCTCamera = requireNativeComponent('RCTCamera', Camera);
|
2015-04-01 01:02:57 +00:00
|
|
|
|
|
|
|
var styles = StyleSheet.create({
|
2015-04-08 20:51:31 +00:00
|
|
|
base: { },
|
2015-04-01 01:02:57 +00:00
|
|
|
});
|
|
|
|
|
2015-05-01 08:16:32 +00:00
|
|
|
Camera.constants = constants;
|
2015-12-22 21:45:17 +00:00
|
|
|
Camera.checkDeviceAuthorizationStatus = NativeModules.CameraManager.checkDeviceAuthorizationStatus
|
|
|
|
|
2015-04-01 01:02:57 +00:00
|
|
|
module.exports = Camera;
|