mirror of
https://github.com/status-im/react-native-camera.git
synced 2025-02-22 16:58:05 +00:00
- Removed event handler defaults.
- Unified Camera.js codebase and "symlinked" the iOS and Android files. - Some other code fixes from the ES6 upgrade
This commit is contained in:
parent
8f9fd98496
commit
208edfa3db
173
Camera.js
Normal file
173
Camera.js
Normal file
@ -0,0 +1,173 @@
|
|||||||
|
import React, {
|
||||||
|
Component,
|
||||||
|
NativeAppEventEmitter,
|
||||||
|
NativeModules,
|
||||||
|
PropTypes,
|
||||||
|
StyleSheet,
|
||||||
|
requireNativeComponent
|
||||||
|
} from 'react-native';
|
||||||
|
|
||||||
|
const CameraManager = NativeModules.CameraManager;
|
||||||
|
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];
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
Orientation: CameraManager.Orientation,
|
||||||
|
FlashMode: CameraManager.FlashMode,
|
||||||
|
TorchMode: CameraManager.TorchMode
|
||||||
|
};
|
||||||
|
|
||||||
|
static propTypes = {
|
||||||
|
aspect: PropTypes.oneOfType([
|
||||||
|
PropTypes.string,
|
||||||
|
PropTypes.number
|
||||||
|
]),
|
||||||
|
captureAudio: PropTypes.bool,
|
||||||
|
captureMode: PropTypes.oneOfType([
|
||||||
|
PropTypes.string,
|
||||||
|
PropTypes.number
|
||||||
|
]),
|
||||||
|
captureTarget: PropTypes.oneOfType([
|
||||||
|
PropTypes.string,
|
||||||
|
PropTypes.number
|
||||||
|
]),
|
||||||
|
defaultOnFocusComponent: PropTypes.bool,
|
||||||
|
flashMode: PropTypes.oneOfType([
|
||||||
|
PropTypes.string,
|
||||||
|
PropTypes.number
|
||||||
|
]),
|
||||||
|
onBarCodeRead: PropTypes.func,
|
||||||
|
onFocusChanged: PropTypes.func,
|
||||||
|
onZoomChanged: PropTypes.func,
|
||||||
|
orientation: PropTypes.oneOfType([
|
||||||
|
PropTypes.string,
|
||||||
|
PropTypes.number
|
||||||
|
]),
|
||||||
|
torchMode: PropTypes.oneOfType([
|
||||||
|
PropTypes.string,
|
||||||
|
PropTypes.number
|
||||||
|
]),
|
||||||
|
type: PropTypes.oneOfType([
|
||||||
|
PropTypes.string,
|
||||||
|
PropTypes.number
|
||||||
|
])
|
||||||
|
};
|
||||||
|
|
||||||
|
static defaultProps = {
|
||||||
|
aspect: Camera.constants.Aspect.fill,
|
||||||
|
type: Camera.constants.Type.back,
|
||||||
|
orientation: Camera.constants.Orientation.auto,
|
||||||
|
captureAudio: true,
|
||||||
|
captureMode: Camera.constants.CaptureMode.still,
|
||||||
|
captureTarget: Camera.constants.CaptureTarget.cameraRoll,
|
||||||
|
defaultOnFocusComponent: true,
|
||||||
|
flashMode: Camera.constants.FlashMode.off,
|
||||||
|
torchMode: Camera.constants.TorchMode.off
|
||||||
|
};
|
||||||
|
|
||||||
|
static checkDeviceAuthorizationStatus = CameraManager.checkDeviceAuthorizationStatus;
|
||||||
|
|
||||||
|
setNativeProps(props) {
|
||||||
|
this.refs[CAMERA_REF].setNativeProps(props);
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
this.state = {
|
||||||
|
isAuthorized: false,
|
||||||
|
isRecording: false
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async componentWillMount() {
|
||||||
|
const isAuthorized = await CameraManager.checkDeviceAuthorizationStatus();
|
||||||
|
this.setState({ isAuthorized });
|
||||||
|
|
||||||
|
this.cameraBarCodeReadListener = NativeAppEventEmitter.addListener('CameraBarCodeRead', this.props.onBarCodeRead);
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
...options
|
||||||
|
};
|
||||||
|
|
||||||
|
if (options.mode === 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 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
getFOV() {
|
||||||
|
return CameraManager.getFOV();
|
||||||
|
}
|
||||||
|
|
||||||
|
hasFlash() {
|
||||||
|
return CameraManager.hasFlash();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var RCTCamera = requireNativeComponent('RCTCamera', Camera);
|
||||||
|
|
||||||
|
var styles = StyleSheet.create({
|
||||||
|
base: {},
|
||||||
|
});
|
203
index.android.js
203
index.android.js
@ -1,201 +1,2 @@
|
|||||||
var React = require('react-native');
|
import Camera from './Camera';
|
||||||
var { View, StyleSheet, requireNativeComponent, PropTypes, NativeModules, DeviceEventEmitter } = React;
|
export default Camera;
|
||||||
|
|
||||||
var CAMERA_REF = 'camera';
|
|
||||||
|
|
||||||
var constants = {
|
|
||||||
Aspect: NativeModules.CameraModule.Aspect,
|
|
||||||
BarCodeType: NativeModules.CameraModule.BarCodeType,
|
|
||||||
Type: NativeModules.CameraModule.Type,
|
|
||||||
CaptureMode: NativeModules.CameraModule.CaptureMode,
|
|
||||||
CaptureTarget: NativeModules.CameraModule.CaptureTarget,
|
|
||||||
Orientation: NativeModules.CameraModule.Orientation,
|
|
||||||
FlashMode: NativeModules.CameraModule.FlashMode,
|
|
||||||
TorchMode: NativeModules.CameraModule.TorchMode
|
|
||||||
};
|
|
||||||
|
|
||||||
var Camera = React.createClass({
|
|
||||||
propTypes: {
|
|
||||||
aspect: PropTypes.oneOfType([
|
|
||||||
PropTypes.string,
|
|
||||||
PropTypes.number
|
|
||||||
]),
|
|
||||||
captureAudio: PropTypes.bool,
|
|
||||||
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
|
|
||||||
]),
|
|
||||||
flashMode: PropTypes.oneOfType([
|
|
||||||
PropTypes.string,
|
|
||||||
PropTypes.number
|
|
||||||
]),
|
|
||||||
torchMode: PropTypes.oneOfType([
|
|
||||||
PropTypes.string,
|
|
||||||
PropTypes.number
|
|
||||||
]),
|
|
||||||
defaultOnFocusComponent: PropTypes.bool,
|
|
||||||
onFocusChanged: PropTypes.func,
|
|
||||||
onZoomChanged: PropTypes.func,
|
|
||||||
...View.propTypes
|
|
||||||
},
|
|
||||||
|
|
||||||
setNativeProps(props) {
|
|
||||||
this.refs[CAMERA_REF].setNativeProps(props);
|
|
||||||
},
|
|
||||||
|
|
||||||
getDefaultProps() {
|
|
||||||
return {
|
|
||||||
aspect: constants.Aspect.fill,
|
|
||||||
type: constants.Type.back,
|
|
||||||
orientation: constants.Orientation.auto,
|
|
||||||
captureAudio: true,
|
|
||||||
captureMode: constants.CaptureMode.still,
|
|
||||||
captureTarget: constants.CaptureTarget.cameraRoll,
|
|
||||||
flashMode: constants.FlashMode.off,
|
|
||||||
torchMode: constants.TorchMode.off
|
|
||||||
};
|
|
||||||
},
|
|
||||||
|
|
||||||
getInitialState() {
|
|
||||||
return {
|
|
||||||
isAuthorized: false,
|
|
||||||
isRecording: false
|
|
||||||
};
|
|
||||||
},
|
|
||||||
|
|
||||||
componentWillMount() {
|
|
||||||
//// TODO: handle properly Android 6 new permissions style
|
|
||||||
this.state.isAuthorized = true;
|
|
||||||
this.setState(this.state);
|
|
||||||
this.cameraBarCodeReadListener = DeviceEventEmitter.addListener('CameraBarCodeRead', this._onBarCodeRead);
|
|
||||||
},
|
|
||||||
|
|
||||||
componentWillUnmount() {
|
|
||||||
this.cameraBarCodeReadListener.remove();
|
|
||||||
|
|
||||||
if (this.state.isRecording) {
|
|
||||||
this.stopCapture();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
render() {
|
|
||||||
var style = [styles.base, this.props.style];
|
|
||||||
|
|
||||||
var aspect = this.props.aspect,
|
|
||||||
type = this.props.type,
|
|
||||||
orientation = this.props.orientation,
|
|
||||||
flashMode = this.props.flashMode,
|
|
||||||
torchMode = this.props.torchMode;
|
|
||||||
|
|
||||||
var legacyProps = {
|
|
||||||
aspect: {
|
|
||||||
Fill: 'fill',
|
|
||||||
Fit: 'fit',
|
|
||||||
Stretch: 'stretch'
|
|
||||||
},
|
|
||||||
orientation: {
|
|
||||||
LandscapeLeft: 'landscapeLeft',
|
|
||||||
LandscapeRight: 'landscapeRight',
|
|
||||||
Portrait: 'portrait',
|
|
||||||
PortraitUpsideDown: 'portraitUpsideDown'
|
|
||||||
},
|
|
||||||
type: {
|
|
||||||
Front: 'front',
|
|
||||||
Back: 'back'
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
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 torchMode === 'string') {
|
|
||||||
torchMode = constants.TorchMode[torchMode];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof type === 'string') {
|
|
||||||
type = constants.Type[type];
|
|
||||||
}
|
|
||||||
|
|
||||||
var nativeProps = Object.assign({}, this.props, {
|
|
||||||
style,
|
|
||||||
aspect: aspect,
|
|
||||||
type: type,
|
|
||||||
orientation: orientation,
|
|
||||||
flashMode: flashMode,
|
|
||||||
torchMode: torchMode
|
|
||||||
});
|
|
||||||
|
|
||||||
return <RCTCameraView ref={CAMERA_REF} {... nativeProps} />;
|
|
||||||
},
|
|
||||||
|
|
||||||
_onBarCodeRead(e) {
|
|
||||||
this.props.onBarCodeRead && this.props.onBarCodeRead(e);
|
|
||||||
},
|
|
||||||
|
|
||||||
capture(options) {
|
|
||||||
options = Object.assign({}, {
|
|
||||||
audio: this.props.captureAudio,
|
|
||||||
mode: this.props.captureMode,
|
|
||||||
target: this.props.captureTarget,
|
|
||||||
type: this.props.type
|
|
||||||
}, 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') {
|
|
||||||
options.target = constants.CaptureTarget[options.target];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof options.type === 'string') {
|
|
||||||
options.type = constants.Type[options.type];
|
|
||||||
}
|
|
||||||
|
|
||||||
return NativeModules.CameraModule.capture(options);
|
|
||||||
},
|
|
||||||
|
|
||||||
stopCapture() {
|
|
||||||
if (this.state.isRecording) {
|
|
||||||
NativeModules.CameraManager.stopCapture();
|
|
||||||
this.setState({ isRecording: false });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
var RCTCameraView = requireNativeComponent('RCTCameraView', Camera);
|
|
||||||
|
|
||||||
var styles = StyleSheet.create({
|
|
||||||
base: {},
|
|
||||||
});
|
|
||||||
|
|
||||||
Camera.constants = constants;
|
|
||||||
module.exports = Camera;
|
|
178
index.ios.js
178
index.ios.js
@ -1,176 +1,2 @@
|
|||||||
import React, {
|
import Camera from './Camera';
|
||||||
Component,
|
export default Camera;
|
||||||
NativeAppEventEmitter,
|
|
||||||
NativeModules,
|
|
||||||
PropTypes,
|
|
||||||
StyleSheet,
|
|
||||||
requireNativeComponent
|
|
||||||
}
|
|
||||||
|
|
||||||
const CameraManager = NativeModules.CameraManager;
|
|
||||||
const CAMERA_REF = 'camera';
|
|
||||||
|
|
||||||
function convertStringProps(props) {
|
|
||||||
const newProps = { ...props };
|
|
||||||
if (typeof props.aspect === 'string') {
|
|
||||||
newProps.aspect = constants.Aspect[aspect];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof props.flashMode === 'string') {
|
|
||||||
newProps.flashMode = constants.FlashMode[flashMode];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof props.orientation === 'string') {
|
|
||||||
newProps.orientation = constants.Orientation[orientation];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof props.torchMode === 'string') {
|
|
||||||
newProps.torchMode = constants.TorchMode[torchMode];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof props.type === 'string') {
|
|
||||||
newProps.type = constants.Type[type];
|
|
||||||
}
|
|
||||||
|
|
||||||
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,
|
|
||||||
Orientation: CameraManager.Orientation,
|
|
||||||
FlashMode: CameraManager.FlashMode,
|
|
||||||
TorchMode: CameraManager.TorchMode
|
|
||||||
};
|
|
||||||
|
|
||||||
static propTypes = {
|
|
||||||
aspect: PropTypes.oneOfType([
|
|
||||||
PropTypes.string,
|
|
||||||
PropTypes.number
|
|
||||||
]),
|
|
||||||
captureAudio: PropTypes.bool,
|
|
||||||
captureMode: PropTypes.oneOfType([
|
|
||||||
PropTypes.string,
|
|
||||||
PropTypes.number
|
|
||||||
]),
|
|
||||||
captureTarget: PropTypes.oneOfType([
|
|
||||||
PropTypes.string,
|
|
||||||
PropTypes.number
|
|
||||||
]),
|
|
||||||
defaultOnFocusComponent: PropTypes.bool,
|
|
||||||
flashMode: PropTypes.oneOfType([
|
|
||||||
PropTypes.string,
|
|
||||||
PropTypes.number
|
|
||||||
]),
|
|
||||||
onBarCodeRead: PropTypes.func,
|
|
||||||
onFocusChanged: PropTypes.func,
|
|
||||||
onZoomChanged: PropTypes.func
|
|
||||||
orientation: PropTypes.oneOfType([
|
|
||||||
PropTypes.string,
|
|
||||||
PropTypes.number
|
|
||||||
]),
|
|
||||||
torchMode: PropTypes.oneOfType([
|
|
||||||
PropTypes.string,
|
|
||||||
PropTypes.number
|
|
||||||
]),
|
|
||||||
type: PropTypes.oneOfType([
|
|
||||||
PropTypes.string,
|
|
||||||
PropTypes.number
|
|
||||||
])
|
|
||||||
};
|
|
||||||
|
|
||||||
static defaultProps = {
|
|
||||||
aspect: Camera.constants.Aspect.fill,
|
|
||||||
type: Camera.constants.Type.back,
|
|
||||||
orientation: Camera.constants.Orientation.auto,
|
|
||||||
captureAudio: true,
|
|
||||||
captureMode: Camera.constants.CaptureMode.still,
|
|
||||||
captureTarget: Camera.constants.CaptureTarget.cameraRoll,
|
|
||||||
defaultOnFocusComponent: true,
|
|
||||||
flashMode: Camera.constants.FlashMode.off,
|
|
||||||
torchMode: Camera.constants.TorchMode.off,
|
|
||||||
onBarCodeRead: () => {},
|
|
||||||
onFocusChanged: () => {},
|
|
||||||
onZoomChanged: () => {}
|
|
||||||
};
|
|
||||||
|
|
||||||
static checkDeviceAuthorizationStatus = CameraManager.checkDeviceAuthorizationStatus;
|
|
||||||
|
|
||||||
setNativeProps(props) {
|
|
||||||
this.refs[CAMERA_REF].setNativeProps(props);
|
|
||||||
}
|
|
||||||
|
|
||||||
constructor() {
|
|
||||||
super();
|
|
||||||
this.state = {
|
|
||||||
isAuthorized: false,
|
|
||||||
isRecording: false
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
async componentWillMount() {
|
|
||||||
const isAuthorized = await CameraManager.checkDeviceAuthorizationStatus();
|
|
||||||
this.setState({ isAuthorized });
|
|
||||||
|
|
||||||
this.cameraBarCodeReadListener = NativeAppEventEmitter.addListener('CameraBarCodeRead', this.props.onBarCodeRead);
|
|
||||||
}
|
|
||||||
|
|
||||||
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,
|
|
||||||
...options
|
|
||||||
};
|
|
||||||
|
|
||||||
if (options.mode === 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 });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
getFOV() {
|
|
||||||
return CameraManager.getFOV();
|
|
||||||
}
|
|
||||||
|
|
||||||
hasFlash() {
|
|
||||||
return CameraManager.hasFlash();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
var RCTCamera = requireNativeComponent('RCTCamera', Camera);
|
|
||||||
|
|
||||||
var styles = StyleSheet.create({
|
|
||||||
base: {},
|
|
||||||
});
|
|
Loading…
x
Reference in New Issue
Block a user