react-native-camera/index.js

228 lines
6.1 KiB
JavaScript
Raw Normal View History

import React, { Component, PropTypes } from 'react';
2016-04-23 19:42:35 +00:00
import {
NativeAppEventEmitter,
NativeModules,
2016-03-27 00:27:40 +00:00
Platform,
StyleSheet,
requireNativeComponent,
View,
} from 'react-native';
const CameraManager = NativeModules.CameraManager || NativeModules.CameraModule;
const CAMERA_REF = 'camera';
function convertNativeProps(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];
}
if (typeof props.captureMode === 'string') {
newProps.captureMode = Camera.constants.CaptureMode[props.captureMode];
}
// do not register barCodeTypes if no barcode listener
if (typeof props.onBarCodeRead !== 'function') {
newProps.barCodeTypes = [];
}
return newProps;
}
export default class Camera extends Component {
2016-02-15 22:58:04 +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,
Orientation: CameraManager.Orientation,
FlashMode: CameraManager.FlashMode,
TorchMode: CameraManager.TorchMode
};
2016-02-15 22:58:04 +00:00
static propTypes = {
...View.propTypes,
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
]),
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,
onBarCodeRead: PropTypes.func,
onFocusChanged: PropTypes.func,
onZoomChanged: PropTypes.func,
mirrorImage: PropTypes.bool,
2016-04-09 00:45:42 +00:00
barCodeTypes: PropTypes.array,
orientation: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number
]),
playSoundOnCapture: PropTypes.bool,
torchMode: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number
]),
type: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number
])
};
2016-02-15 22:58:04 +00:00
static defaultProps = {
2016-02-15 22:58:04 +00:00
aspect: CameraManager.Aspect.fill,
type: CameraManager.Type.back,
orientation: CameraManager.Orientation.auto,
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,
defaultOnFocusComponent: true,
2016-02-15 22:58:04 +00:00
flashMode: CameraManager.FlashMode.off,
playSoundOnCapture: true,
torchMode: CameraManager.TorchMode.off,
mirrorImage: false,
barCodeTypes: Object.values(CameraManager.BarCodeType),
};
2016-02-15 22:58:04 +00:00
static checkDeviceAuthorizationStatus = CameraManager.checkDeviceAuthorizationStatus;
static checkVideoAuthorizationStatus = CameraManager.checkVideoAuthorizationStatus;
static checkAudioAuthorizationStatus = CameraManager.checkAudioAuthorizationStatus;
setNativeProps(props) {
this.refs[CAMERA_REF].setNativeProps(props);
}
2016-02-15 22:58:04 +00:00
constructor() {
super();
this.state = {
isAuthorized: false,
isRecording: false
};
}
async componentWillMount() {
this.cameraBarCodeReadListener = NativeAppEventEmitter.addListener('CameraBarCodeRead', this._onBarCodeRead);
let { captureMode } = convertNativeProps({captureMode: this.props.captureMode})
let hasVideoAndAudio = this.props.captureAudio && captureMode === Camera.constants.CaptureMode.video
let check = hasVideoAndAudio ? Camera.checkDeviceAuthorizationStatus : Camera.checkVideoAuthorizationStatus;
if (check) {
const isAuthorized = await check();
this.setState({ isAuthorized });
}
}
componentWillUnmount() {
this.cameraBarCodeReadListener.remove();
if (this.state.isRecording) {
this.stopCapture();
}
}
render() {
const style = [styles.base, this.props.style];
const nativeProps = convertNativeProps(this.props);
return <RCTCamera ref={CAMERA_REF} {...nativeProps} />;
}
_onBarCodeRead = (data) => {
if (this.props.onBarCodeRead) this.props.onBarCodeRead(data)
};
capture(options) {
const props = convertNativeProps(this.props);
options = {
audio: props.captureAudio,
barCodeTypes: props.barCodeTypes,
mode: props.captureMode,
playSoundOnCapture: props.playSoundOnCapture,
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: '',
...options
};
2016-02-01 04:47:59 +00:00
if (options.mode === Camera.constants.CaptureMode.video) {
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
getFOV() {
return CameraManager.getFOV();
}
hasFlash() {
2016-03-27 00:27:40 +00:00
if (Platform.OS === 'android') {
const props = convertNativeProps(this.props);
2016-03-27 00:27:40 +00:00
return CameraManager.hasFlash({
type: props.type
});
}
return CameraManager.hasFlash();
}
}
2016-06-01 17:25:59 +00:00
export const constants = Camera.constants;
2016-02-01 04:47:59 +00:00
const RCTCamera = requireNativeComponent('RCTCamera', Camera);
2016-02-01 04:47:59 +00:00
const styles = StyleSheet.create({
base: {},
});