react-native-camera/index.ios.js

205 lines
5.1 KiB
JavaScript
Raw Normal View History

var React = require('react-native');
var { StyleSheet, requireNativeComponent, PropTypes, NativeModules, DeviceEventEmitter } = React;
2015-04-01 01:02:57 +00:00
var CAMERA_REF = 'camera';
var constants = {
Aspect: NativeModules.CameraManager.Aspect,
BarCodeType: NativeModules.CameraManager.BarCodeType,
Type: NativeModules.CameraManager.Type,
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,
TorchMode: NativeModules.CameraManager.TorchMode
};
2015-04-01 01:02:57 +00:00
var Camera = React.createClass({
propTypes: {
aspect: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number
]),
captureAudio: PropTypes.bool,
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
]),
defaultOnFocusComponent: PropTypes.bool,
onFocusChanged: PropTypes.func,
onZoomChanged: PropTypes.func
2015-04-01 01:02:57 +00:00
},
setNativeProps(props) {
this.refs[CAMERA_REF].setNativeProps(props);
2015-04-01 01:02:57 +00:00
},
getDefaultProps() {
return {
aspect: constants.Aspect.fill,
type: constants.Type.back,
orientation: constants.Orientation.auto,
captureAudio: true,
captureMode: constants.CaptureMode.still,
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
};
},
getInitialState() {
2015-04-01 01:02:57 +00:00
return {
isAuthorized: false,
isRecording: false
2015-04-01 01:02:57 +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);
},
componentWillUnmount() {
2015-04-19 22:53:30 +00:00
this.cameraBarCodeReadListener.remove();
if (this.state.isRecording) {
this.stopCapture();
}
2015-04-01 01:02:57 +00:00
},
render() {
var style = [styles.base, this.props.style];
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;
var legacyProps = {
aspect: {
2015-05-02 08:13:40 +00:00
Fill: 'fill',
Fit: 'fit',
Stretch: 'stretch'
},
orientation: {
2015-05-02 08:13:40 +00:00
LandscapeLeft: 'landscapeLeft',
LandscapeRight: 'landscapeRight',
Portrait: 'portrait',
PortraitUpsideDown: 'portraitUpsideDown'
},
type: {
2015-05-02 08:13:40 +00:00
Front: 'front',
Back: 'back'
}
};
if (typeof aspect === 'string') {
aspect = constants.Aspect[aspect];
}
if (typeof flashMode === 'string') {
flashMode = constants.FlashMode[flashMode];
}
if (typeof orientation === 'string') {
orientation = constants.Orientation[orientation];
}
if (typeof torchMode === 'string') {
torchMode = constants.TorchMode[torchMode];
}
if (typeof type === 'string') {
type = constants.Type[type];
}
2015-04-01 01:02:57 +00:00
var nativeProps = Object.assign({}, this.props, {
2015-04-01 01:02:57 +00:00
style,
aspect: aspect,
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
});
return <RCTCamera ref={CAMERA_REF} {... nativeProps} />;
2015-04-01 01:02:57 +00:00
},
2015-04-19 22:53:30 +00:00
_onBarCodeRead(e) {
this.props.onBarCodeRead && this.props.onBarCodeRead(e);
},
capture(options, cb) {
if (arguments.length == 1) {
cb = options;
options = {};
}
options = Object.assign({}, {
audio: this.props.captureAudio,
mode: this.props.captureMode,
target: this.props.captureTarget
}, options);
if (typeof options.mode === 'string') {
options.mode = constants.CaptureMode[options.mode];
}
if (options.mode === constants.CaptureMode.video) {
options.totalSeconds = (options.totalSeconds > -1 ? options.totalSeconds : -1);
options.preferredTimeScale = options.preferredTimeScale || 30;
this.setState({ isRecording: true });
}
if (typeof options.target === 'string') {
2015-05-04 19:27:36 +00:00
options.target = constants.CaptureTarget[options.target];
}
NativeModules.CameraManager.capture(options, cb);
},
stopCapture() {
if (this.state.isRecording) {
NativeModules.CameraManager.stopCapture();
this.setState({ isRecording: false });
}
}
2015-04-01 01:02:57 +00:00
});
var RCTCamera = requireNativeComponent('RCTCamera', Camera);
2015-04-01 01:02:57 +00:00
var styles = StyleSheet.create({
base: { },
2015-04-01 01:02:57 +00:00
});
Camera.constants = constants;
Camera.checkDeviceAuthorizationStatus = NativeModules.CameraManager.checkDeviceAuthorizationStatus
2015-04-01 01:02:57 +00:00
module.exports = Camera;