react-native-camera/Camera.ios.js

229 lines
6.2 KiB
JavaScript
Raw Normal View History

2015-04-01 01:02:57 +00:00
var React = require('React');
2015-04-19 22:53:30 +00:00
var DeviceEventEmitter = require('RCTDeviceEventEmitter');
2015-04-01 01:02:57 +00:00
var NativeModules = require('NativeModules');
var ReactNativeViewAttributes = require('ReactNativeViewAttributes');
2015-04-01 01:02:57 +00:00
var StyleSheet = require('StyleSheet');
var createReactNativeComponentClass = require('createReactNativeComponentClass');
2015-04-01 01:02:57 +00:00
var PropTypes = require('ReactPropTypes');
var StyleSheetPropType = require('StyleSheetPropType');
var NativeMethodsMixin = require('NativeMethodsMixin');
var flattenStyle = require('flattenStyle');
var merge = require('merge');
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
]),
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-04-01 01:02:57 +00:00
},
mixins: [NativeMethodsMixin],
viewConfig: {
uiViewClassName: 'UIView',
validAttributes: ReactNativeViewAttributes.UIView
2015-04-01 01:02:57 +00:00
},
getDefaultProps() {
return {
aspect: constants.Aspect.fill,
type: constants.Type.back,
orientation: constants.Orientation.auto,
captureMode: constants.CaptureMode.still,
2015-05-30 16:36:55 +00:00
captureTarget: constants.CaptureTarget.memory,
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.stopRecording();
}
2015-04-01 01:02:57 +00:00
},
render() {
2015-04-01 01:02:57 +00:00
var style = flattenStyle([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'
}
};
var foundLegacyAspect = legacyProps.aspect[aspect];
var foundLegacyOrientation = legacyProps.orientation[orientation];
var foundLegacyType = legacyProps.type[type];
if (__DEV__) {
2015-05-02 08:13:40 +00:00
if (foundLegacyAspect) {
console.log(`RCTCamera: aspect="${aspect}" is deprecated, use aspect={Camera.constants.Aspect.${foundLegacyAspect}} instead.`);
2015-05-02 08:13:40 +00:00
aspect = constants.Aspect[foundLegacyAspect];
}
2015-05-02 08:13:40 +00:00
if (foundLegacyOrientation) {
console.log(`RCTCamera: orientation="${orientation}" is deprecated, use orientation={Camera.constants.Orientation.${foundLegacyOrientation}} instead.`);
2015-05-02 08:13:40 +00:00
orientation = constants.Orientation[foundLegacyOrientation];
}
2015-05-02 08:13:40 +00:00
if (foundLegacyType) {
console.log(`RCTCamera: type="${type}" is deprecated, use type={Camera.constants.Type.${foundLegacyType}} instead.`);
2015-05-02 08:13:40 +00:00
type = constants.Type[foundLegacyType];
}
}
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 type === 'string') {
type = constants.Type[type];
}
2015-04-01 01:02:57 +00:00
var nativeProps = merge(this.props, {
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 {... nativeProps} />
},
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({}, {
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() {
this.setState({ isRecording: false });
NativeModules.CameraManager.stopCapture();
}
2015-04-01 01:02:57 +00:00
});
var RCTCamera = createReactNativeComponentClass({
validAttributes: merge(ReactNativeViewAttributes.UIView, {
aspect: true,
type: true,
2015-05-30 16:36:55 +00:00
orientation: true,
flashMode: true,
2015-06-15 18:58:36 +00:00
torchMode: true
}),
2015-04-01 01:02:57 +00:00
uiViewClassName: 'RCTCamera',
});
var styles = StyleSheet.create({
base: { },
2015-04-01 01:02:57 +00:00
});
Camera.constants = constants;
2015-04-01 01:02:57 +00:00
module.exports = Camera;