2016-02-01 04:04:52 +00:00
|
|
|
import React, {
|
|
|
|
Component,
|
|
|
|
NativeAppEventEmitter,
|
|
|
|
NativeModules,
|
2016-03-27 00:27:40 +00:00
|
|
|
Platform,
|
2016-02-01 04:04:52 +00:00
|
|
|
PropTypes,
|
|
|
|
StyleSheet,
|
2016-02-02 11:13:57 +00:00
|
|
|
requireNativeComponent,
|
|
|
|
View,
|
2016-02-01 04:04:52 +00:00
|
|
|
} from 'react-native';
|
|
|
|
|
2016-02-01 05:03:15 +00:00
|
|
|
const CameraManager = NativeModules.CameraManager || NativeModules.CameraModule;
|
2016-02-01 04:04:52 +00:00
|
|
|
const CAMERA_REF = 'camera';
|
|
|
|
|
|
|
|
function convertStringProps(props) {
|
|
|
|
const newProps = { ...props };
|
|
|
|
if (typeof props.aspect === 'string') {
|
|
|
|
newProps.aspect = Camera.constants.Aspect[props.aspect];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof props.flashMode === 'string') {
|
|
|
|
newProps.flashMode = Camera.constants.FlashMode[props.flashMode];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof props.orientation === 'string') {
|
|
|
|
newProps.orientation = Camera.constants.Orientation[props.orientation];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof props.torchMode === 'string') {
|
|
|
|
newProps.torchMode = Camera.constants.TorchMode[props.torchMode];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof props.type === 'string') {
|
|
|
|
newProps.type = Camera.constants.Type[props.type];
|
|
|
|
}
|
2016-02-15 22:58:04 +00:00
|
|
|
|
2016-03-20 19:31:23 +00:00
|
|
|
if (typeof props.captureQuality === 'string') {
|
|
|
|
newProps.captureQuality = Camera.constants.CaptureQuality[props.captureQuality];
|
|
|
|
}
|
|
|
|
|
2016-02-01 04:04:52 +00:00
|
|
|
return newProps;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class Camera extends Component {
|
2016-02-15 22:58:04 +00:00
|
|
|
|
2016-02-01 04:04:52 +00:00
|
|
|
static constants = {
|
|
|
|
Aspect: CameraManager.Aspect,
|
|
|
|
BarCodeType: CameraManager.BarCodeType,
|
|
|
|
Type: CameraManager.Type,
|
|
|
|
CaptureMode: CameraManager.CaptureMode,
|
|
|
|
CaptureTarget: CameraManager.CaptureTarget,
|
2016-03-20 19:31:23 +00:00
|
|
|
CaptureQuality: CameraManager.CaptureQuality,
|
2016-02-01 04:04:52 +00:00
|
|
|
Orientation: CameraManager.Orientation,
|
|
|
|
FlashMode: CameraManager.FlashMode,
|
|
|
|
TorchMode: CameraManager.TorchMode
|
|
|
|
};
|
2016-02-15 22:58:04 +00:00
|
|
|
|
2016-02-01 04:04:52 +00:00
|
|
|
static propTypes = {
|
2016-02-02 11:13:57 +00:00
|
|
|
...View.propTypes,
|
2016-02-01 04:04:52 +00:00
|
|
|
aspect: PropTypes.oneOfType([
|
|
|
|
PropTypes.string,
|
|
|
|
PropTypes.number
|
|
|
|
]),
|
|
|
|
captureAudio: PropTypes.bool,
|
|
|
|
captureMode: PropTypes.oneOfType([
|
|
|
|
PropTypes.string,
|
|
|
|
PropTypes.number
|
|
|
|
]),
|
2016-03-20 19:31:23 +00:00
|
|
|
captureQuality: PropTypes.oneOfType([
|
|
|
|
PropTypes.string,
|
|
|
|
PropTypes.number
|
|
|
|
]),
|
2016-02-01 04:04:52 +00:00
|
|
|
captureTarget: PropTypes.oneOfType([
|
|
|
|
PropTypes.string,
|
|
|
|
PropTypes.number
|
|
|
|
]),
|
|
|
|
defaultOnFocusComponent: PropTypes.bool,
|
|
|
|
flashMode: PropTypes.oneOfType([
|
|
|
|
PropTypes.string,
|
|
|
|
PropTypes.number
|
|
|
|
]),
|
2016-02-08 12:08:10 +00:00
|
|
|
keepAwake: PropTypes.bool,
|
2016-02-01 04:04:52 +00:00
|
|
|
onBarCodeRead: PropTypes.func,
|
|
|
|
onFocusChanged: PropTypes.func,
|
|
|
|
onZoomChanged: PropTypes.func,
|
2016-02-24 17:34:43 +00:00
|
|
|
mirrorImage: PropTypes.bool,
|
2016-02-01 04:04:52 +00:00
|
|
|
orientation: PropTypes.oneOfType([
|
|
|
|
PropTypes.string,
|
|
|
|
PropTypes.number
|
|
|
|
]),
|
|
|
|
torchMode: PropTypes.oneOfType([
|
|
|
|
PropTypes.string,
|
|
|
|
PropTypes.number
|
|
|
|
]),
|
|
|
|
type: PropTypes.oneOfType([
|
|
|
|
PropTypes.string,
|
|
|
|
PropTypes.number
|
|
|
|
])
|
|
|
|
};
|
2016-02-15 22:58:04 +00:00
|
|
|
|
2016-02-01 04:04:52 +00:00
|
|
|
static defaultProps = {
|
2016-02-15 22:58:04 +00:00
|
|
|
aspect: CameraManager.Aspect.fill,
|
|
|
|
type: CameraManager.Type.back,
|
|
|
|
orientation: CameraManager.Orientation.auto,
|
2016-02-01 04:04:52 +00:00
|
|
|
captureAudio: true,
|
2016-02-15 22:58:04 +00:00
|
|
|
captureMode: CameraManager.CaptureMode.still,
|
|
|
|
captureTarget: CameraManager.CaptureTarget.cameraRoll,
|
2016-03-20 19:31:23 +00:00
|
|
|
captureQuality: CameraManager.CaptureQuality.high,
|
2016-02-01 04:04:52 +00:00
|
|
|
defaultOnFocusComponent: true,
|
2016-02-15 22:58:04 +00:00
|
|
|
flashMode: CameraManager.FlashMode.off,
|
2016-02-24 17:34:43 +00:00
|
|
|
torchMode: CameraManager.TorchMode.off,
|
|
|
|
mirrorImage: false,
|
2016-02-01 04:04:52 +00:00
|
|
|
};
|
2016-02-15 22:58:04 +00:00
|
|
|
|
2016-02-01 04:04:52 +00:00
|
|
|
static checkDeviceAuthorizationStatus = CameraManager.checkDeviceAuthorizationStatus;
|
2016-04-03 18:19:31 +00:00
|
|
|
static checkVideoAuthorizationStatus = CameraManager.checkVideoAuthorizationStatus;
|
2016-03-17 14:07:39 +00:00
|
|
|
static checkAudioAuthorizationStatus = CameraManager.checkAudioAuthorizationStatus;
|
2016-02-01 04:04:52 +00:00
|
|
|
|
|
|
|
setNativeProps(props) {
|
|
|
|
this.refs[CAMERA_REF].setNativeProps(props);
|
|
|
|
}
|
2016-02-15 22:58:04 +00:00
|
|
|
|
2016-02-01 04:04:52 +00:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.state = {
|
|
|
|
isAuthorized: false,
|
|
|
|
isRecording: false
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async componentWillMount() {
|
2016-02-15 23:15:24 +00:00
|
|
|
this.cameraBarCodeReadListener = NativeAppEventEmitter.addListener('CameraBarCodeRead', this.props.onBarCodeRead);
|
|
|
|
|
2016-03-17 17:12:02 +00:00
|
|
|
let check = this.props.captureAudio ? Camera.checkDeviceAuthorizationStatus : Camera.checkVideoAuthorizationStatus;
|
|
|
|
|
|
|
|
if (check) {
|
|
|
|
const isAuthorized = await check();
|
2016-02-01 05:05:47 +00:00
|
|
|
this.setState({ isAuthorized });
|
|
|
|
}
|
2016-02-01 04:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.cameraBarCodeReadListener.remove();
|
|
|
|
|
|
|
|
if (this.state.isRecording) {
|
|
|
|
this.stopCapture();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const style = [styles.base, this.props.style];
|
|
|
|
const nativeProps = convertStringProps(this.props);
|
|
|
|
|
|
|
|
return <RCTCamera ref={CAMERA_REF} {...nativeProps} />;
|
|
|
|
}
|
|
|
|
|
|
|
|
capture(options) {
|
|
|
|
const props = convertStringProps(this.props);
|
|
|
|
options = {
|
|
|
|
audio: props.captureAudio,
|
|
|
|
mode: props.captureMode,
|
|
|
|
target: props.captureTarget,
|
2016-03-20 19:31:23 +00:00
|
|
|
quality: props.captureQuality,
|
2016-02-11 15:51:13 +00:00
|
|
|
type: props.type,
|
2016-02-11 17:21:28 +00:00
|
|
|
title: '',
|
|
|
|
description: '',
|
2016-02-01 04:04:52 +00:00
|
|
|
...options
|
|
|
|
};
|
|
|
|
|
2016-02-01 04:47:59 +00:00
|
|
|
if (options.mode === Camera.constants.CaptureMode.video) {
|
2016-02-01 04:04:52 +00:00
|
|
|
options.totalSeconds = (options.totalSeconds > -1 ? options.totalSeconds : -1);
|
|
|
|
options.preferredTimeScale = options.preferredTimeScale || 30;
|
|
|
|
this.setState({ isRecording: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
return CameraManager.capture(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
stopCapture() {
|
|
|
|
if (this.state.isRecording) {
|
|
|
|
CameraManager.stopCapture();
|
|
|
|
this.setState({ isRecording: false });
|
|
|
|
}
|
|
|
|
}
|
2016-02-15 22:58:04 +00:00
|
|
|
|
2016-02-01 04:04:52 +00:00
|
|
|
getFOV() {
|
|
|
|
return CameraManager.getFOV();
|
|
|
|
}
|
|
|
|
|
|
|
|
hasFlash() {
|
2016-03-27 00:27:40 +00:00
|
|
|
if (Platform.OS === 'android') {
|
|
|
|
const props = convertStringProps(this.props);
|
|
|
|
return CameraManager.hasFlash({
|
|
|
|
type: props.type
|
|
|
|
});
|
|
|
|
}
|
2016-02-01 04:04:52 +00:00
|
|
|
return CameraManager.hasFlash();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-01 04:47:59 +00:00
|
|
|
const RCTCamera = requireNativeComponent('RCTCamera', Camera);
|
2016-02-01 04:04:52 +00:00
|
|
|
|
2016-02-01 04:47:59 +00:00
|
|
|
const styles = StyleSheet.create({
|
2016-02-01 04:04:52 +00:00
|
|
|
base: {},
|
|
|
|
});
|