react-native-camera/index.js

264 lines
7.2 KiB
JavaScript
Raw Normal View History

import React, { Component, PropTypes } from 'react';
2016-04-23 19:42:35 +00:00
import {
2016-09-14 23:02:26 +00:00
DeviceEventEmitter, // android
NativeAppEventEmitter, // ios
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];
}
if (typeof props.captureTarget === 'string') {
newProps.captureTarget = Camera.constants.CaptureTarget[props.captureTarget];
}
// do not register barCodeTypes if no barcode listener
if (typeof props.onBarCodeRead !== 'function') {
newProps.barCodeTypes = [];
}
newProps.barcodeScannerEnabled = typeof props.onBarCodeRead === 'function'
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,
barcodeScannerEnabled: PropTypes.bool,
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: false,
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() {
2016-09-14 23:02:26 +00:00
this._addOnBarCodeReadListener()
2016-09-14 23:02:26 +00:00
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() {
2016-09-14 23:02:26 +00:00
this._removeOnBarCodeReadListener()
if (this.state.isRecording) {
this.stopCapture();
}
}
2016-09-14 23:02:26 +00:00
componentWillReceiveProps(newProps) {
const { onBarCodeRead } = this.props
if (onBarCodeRead && !newProps.onBarCodeRead) {
this._addOnBarCodeReadListener(newProps)
}
}
_addOnBarCodeReadListener(props) {
const { onBarCodeRead } = props || this.props
this._removeOnBarCodeReadListener()
if (onBarCodeRead) {
this.cameraBarCodeReadListener = Platform.select({
ios: NativeAppEventEmitter.addListener('CameraBarCodeRead', this._onBarCodeRead),
android: DeviceEventEmitter.addListener('CameraBarCodeReadAndroid', this._onBarCodeRead)
})
}
}
_removeOnBarCodeReadListener() {
const listener = this.cameraBarCodeReadListener
if (listener) {
listener.remove()
}
}
render() {
const style = [styles.base, this.props.style];
const nativeProps = convertNativeProps(this.props);
return <RCTCamera ref={CAMERA_REF} {...nativeProps} />;
}
_onBarCodeRead = (data) => {
2016-09-14 23:02:26 +00:00
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: '',
mirrorImage: props.mirrorImage,
...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) {
this.setState({ isRecording: false });
Android support for recording video (#262) * Initial commit with Android video support * stopCapture now works * Bug fixes and parameter enhancements. README updated. * Modified stopCapture parameter count to match iOS * fixed promise bug on stopCapture * Update RCTCameraModule.java In Android preview and recording sizes are different, which can cause an error. This fix detects the difference and chooses a recording resolution that matches. * Update RCTCameraModule.java * Update RCTCamera.java Creating video functions in style/convention of existing * Update RCTCameraModule.java Use new functions for adjusting video capture size and quality * Update RCTCameraModule.java Fixes issue where file not video playable (readable) on older devices * Update AndroidManifest.xml Since we're reading and writing video and pictures, need permissions for it. * Fixed upside down camera (on some platforms), and misc bugs and crashes * Added camera-roll and capture to memory support, new options, and support for duration, filesize, and metadata * To make merge nicer, temporarily reverting "Added camera-roll and capture to memory support, new options, and support for duration, filesize, and metadata" This reverts commit 9ea1ad409c7e6121cf0197172e752b7523d4b092. * Fixed merge & brought back all improvements from 9ea1ad4 * Fixed logic for video -> camera roll * Updates * Uncommenting setProfile * Fix support for React Native 0.25 * Renamed Camera to index * * Fix after merge android recording * * Fixed android camera roll file saving * Added recording to example * * Android promise rejections with exceptions * Fixed preview, video and photo sizes * Android recording result in new, javascript object, format * * Removed example.index.android.js as there is Example project * * Readme for example * don't force a specific codec * always use cache dir * * Using MediaScannerConnection instead of ACTION_MEDIA_SCANNER_SCAN_FILE intent * * As described in https://github.com/lwansbrough/react-native-camera/pull/262#issuecomment-239622268: - fixed video the wrong direction and recoder start fail at "low,medium" on the nexus 5 x
2016-08-28 01:49:46 +00:00
return CameraManager.stopCapture();
}
Android support for recording video (#262) * Initial commit with Android video support * stopCapture now works * Bug fixes and parameter enhancements. README updated. * Modified stopCapture parameter count to match iOS * fixed promise bug on stopCapture * Update RCTCameraModule.java In Android preview and recording sizes are different, which can cause an error. This fix detects the difference and chooses a recording resolution that matches. * Update RCTCameraModule.java * Update RCTCamera.java Creating video functions in style/convention of existing * Update RCTCameraModule.java Use new functions for adjusting video capture size and quality * Update RCTCameraModule.java Fixes issue where file not video playable (readable) on older devices * Update AndroidManifest.xml Since we're reading and writing video and pictures, need permissions for it. * Fixed upside down camera (on some platforms), and misc bugs and crashes * Added camera-roll and capture to memory support, new options, and support for duration, filesize, and metadata * To make merge nicer, temporarily reverting "Added camera-roll and capture to memory support, new options, and support for duration, filesize, and metadata" This reverts commit 9ea1ad409c7e6121cf0197172e752b7523d4b092. * Fixed merge & brought back all improvements from 9ea1ad4 * Fixed logic for video -> camera roll * Updates * Uncommenting setProfile * Fix support for React Native 0.25 * Renamed Camera to index * * Fix after merge android recording * * Fixed android camera roll file saving * Added recording to example * * Android promise rejections with exceptions * Fixed preview, video and photo sizes * Android recording result in new, javascript object, format * * Removed example.index.android.js as there is Example project * * Readme for example * don't force a specific codec * always use cache dir * * Using MediaScannerConnection instead of ACTION_MEDIA_SCANNER_SCAN_FILE intent * * As described in https://github.com/lwansbrough/react-native-camera/pull/262#issuecomment-239622268: - fixed video the wrong direction and recoder start fail at "low,medium" on the nexus 5 x
2016-08-28 01:49:46 +00:00
return Promise.resolve("Not Recording.");
}
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: {},
});