Files

215 lines
5.4 KiB
JavaScript
Raw Permalink Normal View History

2016-05-29 12:55:51 -04:00
import React from 'react';
2018-01-07 09:06:20 -08:00
import { Image, StatusBar, StyleSheet, TouchableOpacity, View } from 'react-native';
2016-05-29 12:55:51 -04:00
import Camera from 'react-native-camera';
const styles = StyleSheet.create({
container: {
flex: 1,
},
preview: {
flex: 1,
justifyContent: 'flex-end',
alignItems: 'center',
},
overlay: {
position: 'absolute',
padding: 16,
right: 0,
left: 0,
alignItems: 'center',
},
topOverlay: {
top: 0,
flex: 1,
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
},
bottomOverlay: {
bottom: 0,
backgroundColor: 'rgba(0,0,0,0.4)',
2016-08-27 20:49:46 -05:00
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
2016-05-29 12:55:51 -04:00
},
captureButton: {
padding: 15,
backgroundColor: 'white',
borderRadius: 40,
},
typeButton: {
padding: 5,
},
flashButton: {
padding: 5,
},
2016-08-27 20:49:46 -05:00
buttonsSpace: {
width: 10,
},
2016-05-29 12:55:51 -04:00
});
export default class Example extends React.Component {
constructor(props) {
super(props);
this.camera = null;
this.state = {
camera: {
aspect: Camera.constants.Aspect.fill,
captureTarget: Camera.constants.CaptureTarget.cameraRoll,
type: Camera.constants.Type.back,
orientation: Camera.constants.Orientation.auto,
flashMode: Camera.constants.FlashMode.auto,
},
2018-01-07 09:06:20 -08:00
isRecording: false,
2016-05-29 12:55:51 -04:00
};
}
2016-12-21 17:35:44 +01:00
takePicture = () => {
2016-05-29 12:55:51 -04:00
if (this.camera) {
2018-01-07 09:06:20 -08:00
this.camera
.capture()
.then(data => console.log(data))
2016-05-29 12:55:51 -04:00
.catch(err => console.error(err));
}
2018-01-07 09:06:20 -08:00
};
2016-05-29 12:55:51 -04:00
2016-12-21 17:35:44 +01:00
startRecording = () => {
2016-08-27 20:49:46 -05:00
if (this.camera) {
2018-01-07 09:06:20 -08:00
this.camera
.capture({ mode: Camera.constants.CaptureMode.video })
.then(data => console.log(data))
.catch(err => console.error(err));
2016-08-27 20:49:46 -05:00
this.setState({
2018-01-07 09:06:20 -08:00
isRecording: true,
2016-08-27 20:49:46 -05:00
});
}
2018-01-07 09:06:20 -08:00
};
2016-08-27 20:49:46 -05:00
2016-12-21 17:35:44 +01:00
stopRecording = () => {
2016-08-27 20:49:46 -05:00
if (this.camera) {
this.camera.stopCapture();
this.setState({
2018-01-07 09:06:20 -08:00
isRecording: false,
2016-08-27 20:49:46 -05:00
});
}
2018-01-07 09:06:20 -08:00
};
2016-08-27 20:49:46 -05:00
2016-12-21 17:35:44 +01:00
switchType = () => {
2016-05-29 12:55:51 -04:00
let newType;
const { back, front } = Camera.constants.Type;
if (this.state.camera.type === back) {
newType = front;
} else if (this.state.camera.type === front) {
newType = back;
}
this.setState({
camera: {
...this.state.camera,
type: newType,
},
});
2018-01-07 09:06:20 -08:00
};
2016-05-29 12:55:51 -04:00
get typeIcon() {
let icon;
const { back, front } = Camera.constants.Type;
if (this.state.camera.type === back) {
icon = require('./assets/ic_camera_rear_white.png');
} else if (this.state.camera.type === front) {
icon = require('./assets/ic_camera_front_white.png');
}
return icon;
}
2016-12-21 17:35:44 +01:00
switchFlash = () => {
2016-05-29 12:55:51 -04:00
let newFlashMode;
const { auto, on, off } = Camera.constants.FlashMode;
if (this.state.camera.flashMode === auto) {
newFlashMode = on;
} else if (this.state.camera.flashMode === on) {
newFlashMode = off;
} else if (this.state.camera.flashMode === off) {
newFlashMode = auto;
}
this.setState({
camera: {
...this.state.camera,
flashMode: newFlashMode,
},
});
2018-01-07 09:06:20 -08:00
};
2016-05-29 12:55:51 -04:00
get flashIcon() {
let icon;
const { auto, on, off } = Camera.constants.FlashMode;
if (this.state.camera.flashMode === auto) {
icon = require('./assets/ic_flash_auto_white.png');
} else if (this.state.camera.flashMode === on) {
icon = require('./assets/ic_flash_on_white.png');
} else if (this.state.camera.flashMode === off) {
icon = require('./assets/ic_flash_off_white.png');
}
return icon;
}
render() {
return (
<View style={styles.container}>
2018-01-07 09:06:20 -08:00
<StatusBar animated hidden />
2016-05-29 12:55:51 -04:00
<Camera
2018-01-07 09:06:20 -08:00
ref={cam => {
2016-05-29 12:55:51 -04:00
this.camera = cam;
}}
style={styles.preview}
aspect={this.state.camera.aspect}
captureTarget={this.state.camera.captureTarget}
type={this.state.camera.type}
flashMode={this.state.camera.flashMode}
2017-04-23 14:01:04 -07:00
onFocusChanged={() => {}}
onZoomChanged={() => {}}
2016-05-29 12:55:51 -04:00
defaultTouchToFocus
2016-09-13 13:48:48 -03:00
mirrorImage={false}
permissionDialogTitle="Sample title"
permissionDialogMessage="Sample dialog message"
2016-05-29 12:55:51 -04:00
/>
<View style={[styles.overlay, styles.topOverlay]}>
2018-01-07 09:06:20 -08:00
<TouchableOpacity style={styles.typeButton} onPress={this.switchType}>
<Image source={this.typeIcon} />
2016-05-29 12:55:51 -04:00
</TouchableOpacity>
2018-01-07 09:06:20 -08:00
<TouchableOpacity style={styles.flashButton} onPress={this.switchFlash}>
<Image source={this.flashIcon} />
2016-05-29 12:55:51 -04:00
</TouchableOpacity>
</View>
<View style={[styles.overlay, styles.bottomOverlay]}>
2018-01-07 09:06:20 -08:00
{(!this.state.isRecording && (
<TouchableOpacity style={styles.captureButton} onPress={this.takePicture}>
<Image source={require('./assets/ic_photo_camera_36pt.png')} />
2016-08-27 20:49:46 -05:00
</TouchableOpacity>
2018-01-07 09:06:20 -08:00
)) ||
null}
2016-08-27 20:49:46 -05:00
<View style={styles.buttonsSpace} />
2018-01-07 09:06:20 -08:00
{(!this.state.isRecording && (
<TouchableOpacity style={styles.captureButton} onPress={this.startRecording}>
<Image source={require('./assets/ic_videocam_36pt.png')} />
</TouchableOpacity>
)) || (
<TouchableOpacity style={styles.captureButton} onPress={this.stopRecording}>
<Image source={require('./assets/ic_stop_36pt.png')} />
</TouchableOpacity>
)}
2016-05-29 12:55:51 -04:00
</View>
</View>
);
}
}