mirror of
https://github.com/status-im/react-native-camera.git
synced 2025-02-24 17:58:20 +00:00
* 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
229 lines
6.2 KiB
JavaScript
229 lines
6.2 KiB
JavaScript
import React, { Component, PropTypes } from 'react';
|
|
import {
|
|
NativeAppEventEmitter,
|
|
NativeModules,
|
|
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];
|
|
}
|
|
|
|
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 {
|
|
|
|
static constants = {
|
|
Aspect: CameraManager.Aspect,
|
|
BarCodeType: CameraManager.BarCodeType,
|
|
Type: CameraManager.Type,
|
|
CaptureMode: CameraManager.CaptureMode,
|
|
CaptureTarget: CameraManager.CaptureTarget,
|
|
CaptureQuality: CameraManager.CaptureQuality,
|
|
Orientation: CameraManager.Orientation,
|
|
FlashMode: CameraManager.FlashMode,
|
|
TorchMode: CameraManager.TorchMode
|
|
};
|
|
|
|
static propTypes = {
|
|
...View.propTypes,
|
|
aspect: PropTypes.oneOfType([
|
|
PropTypes.string,
|
|
PropTypes.number
|
|
]),
|
|
captureAudio: PropTypes.bool,
|
|
captureMode: PropTypes.oneOfType([
|
|
PropTypes.string,
|
|
PropTypes.number
|
|
]),
|
|
captureQuality: PropTypes.oneOfType([
|
|
PropTypes.string,
|
|
PropTypes.number
|
|
]),
|
|
captureTarget: PropTypes.oneOfType([
|
|
PropTypes.string,
|
|
PropTypes.number
|
|
]),
|
|
defaultOnFocusComponent: PropTypes.bool,
|
|
flashMode: PropTypes.oneOfType([
|
|
PropTypes.string,
|
|
PropTypes.number
|
|
]),
|
|
keepAwake: PropTypes.bool,
|
|
onBarCodeRead: PropTypes.func,
|
|
onFocusChanged: PropTypes.func,
|
|
onZoomChanged: PropTypes.func,
|
|
mirrorImage: PropTypes.bool,
|
|
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
|
|
])
|
|
};
|
|
|
|
static defaultProps = {
|
|
aspect: CameraManager.Aspect.fill,
|
|
type: CameraManager.Type.back,
|
|
orientation: CameraManager.Orientation.auto,
|
|
captureAudio: true,
|
|
captureMode: CameraManager.CaptureMode.still,
|
|
captureTarget: CameraManager.CaptureTarget.cameraRoll,
|
|
captureQuality: CameraManager.CaptureQuality.high,
|
|
defaultOnFocusComponent: true,
|
|
flashMode: CameraManager.FlashMode.off,
|
|
playSoundOnCapture: true,
|
|
torchMode: CameraManager.TorchMode.off,
|
|
mirrorImage: false,
|
|
barCodeTypes: Object.values(CameraManager.BarCodeType),
|
|
};
|
|
|
|
static checkDeviceAuthorizationStatus = CameraManager.checkDeviceAuthorizationStatus;
|
|
static checkVideoAuthorizationStatus = CameraManager.checkVideoAuthorizationStatus;
|
|
static checkAudioAuthorizationStatus = CameraManager.checkAudioAuthorizationStatus;
|
|
|
|
setNativeProps(props) {
|
|
this.refs[CAMERA_REF].setNativeProps(props);
|
|
}
|
|
|
|
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,
|
|
quality: props.captureQuality,
|
|
type: props.type,
|
|
title: '',
|
|
description: '',
|
|
...options
|
|
};
|
|
|
|
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 });
|
|
return CameraManager.stopCapture();
|
|
}
|
|
return Promise.resolve("Not Recording.");
|
|
}
|
|
|
|
getFOV() {
|
|
return CameraManager.getFOV();
|
|
}
|
|
|
|
hasFlash() {
|
|
if (Platform.OS === 'android') {
|
|
const props = convertNativeProps(this.props);
|
|
return CameraManager.hasFlash({
|
|
type: props.type
|
|
});
|
|
}
|
|
return CameraManager.hasFlash();
|
|
}
|
|
}
|
|
|
|
export const constants = Camera.constants;
|
|
|
|
const RCTCamera = requireNativeComponent('RCTCamera', Camera);
|
|
|
|
const styles = StyleSheet.create({
|
|
base: {},
|
|
});
|