提交 a5c14b5c authored 作者: 吴强's avatar 吴强

Add Camera Screen

上级 c6aa0b65
import React from 'react'; import React from 'react';
import { import {
View, View,
Image,
StatusBar, StatusBar,
StyleSheet, StyleSheet,
TouchableOpacity, Image,
Dimensions,
} from 'react-native'; } from 'react-native';
import Icon from 'react-native-vector-icons/MaterialIcons'; import Icon from 'react-native-vector-icons/MaterialIcons';
import Camera from 'react-native-camera'; import Camera from 'react-native-camera';
...@@ -12,13 +12,15 @@ import Camera from 'react-native-camera'; ...@@ -12,13 +12,15 @@ import Camera from 'react-native-camera';
const styles = StyleSheet.create({ const styles = StyleSheet.create({
container: { container: {
flex: 1, flex: 1,
marginTop: 50,
}, },
preview: { preview: {
flex: 1, flex: 1,
justifyContent: 'flex-end', justifyContent: 'flex-end',
alignItems: 'center', alignItems: 'center',
}, },
previewPic: {
flex: 1,
},
overlay: { overlay: {
position: 'absolute', position: 'absolute',
padding: 16, padding: 16,
...@@ -62,38 +64,28 @@ class CameraComponent extends React.Component { ...@@ -62,38 +64,28 @@ class CameraComponent extends React.Component {
orientation: Camera.constants.Orientation.auto, orientation: Camera.constants.Orientation.auto,
}, },
isRecording: false, isRecording: false,
imageURI: '',
}; };
this.switchCam = this.switchCam.bind(this); this.switchCam = this.switchCam.bind(this);
this.recording = this.recording.bind(this); this.recording = this.recording.bind(this);
this.takePicture = this.takePicture.bind(this);
} }
get typeIcon() { typeIcon= () => {
let icon;
const { back, front } = Camera.constants.Type; const { back, front } = Camera.constants.Type;
if (this.state.camera.type === back) { if (this.state.camera.type === back) {
Icon.getImageSource('camera_front', 20, 'white').then((source) => { console.info(source); icon = source.uri; }); return 'camera-front';
console.info(icon);
// icon = require('./assets/ic_camera_rear_white.png'); // icon = require('./assets/ic_camera_rear_white.png');
} else if (this.state.camera.type === front) { } else if (this.state.camera.type === front) {
Icon.getImageSource('camera_rear', 20, 'white').then((source) => { console.info(source); icon = source.uri; }); return 'camera-rear';
// icon = require('./assets/ic_camera_front_white.png');
console.info(icon);
} }
return icon;
} }
get camButton() { camButton= () => {
let img;
if (!this.state.isRecording) { if (!this.state.isRecording) {
Icon.getImageSource('videocam', 36, 'white').then((source) => { console.info(source); img = source.uri; }); return 'videocam';
// img = require('./assets/ic_videocam_36pt.png');
console.info(img);
} else { } else {
// img = require('./assets/ic_stop_36pt.png'); return 'stop';
Icon.getImageSource('stop', 36, 'white').then((source) => { console.info(source); img = source.uri; });
console.info(img);
} }
return img;
} }
switchCam() { switchCam() {
let newType; let newType;
...@@ -131,34 +123,47 @@ class CameraComponent extends React.Component { ...@@ -131,34 +123,47 @@ class CameraComponent extends React.Component {
console.log('stopped '); console.log('stopped ');
} }
} }
takePicture() {
const options = { mode: Camera.constants.CaptureMode.still };
this.camera.capture({ metadata: options })
.then((data) => { console.log(data); this.setState({ imageURI: data.path }); })
.catch(err => console.error(err));
}
render() { render() {
return ( if (this.state.imageURI === '') {
<View style={styles.container}> return (
<StatusBar animated hidden={false} /> <View style={styles.container}>
<Camera <StatusBar animated hidden />
ref={(cam) => { <Camera
this.camera = cam; ref={(cam) => {
}} this.camera = cam;
style={styles.preview} }}
aspect={this.state.camera.aspect} style={styles.preview}
captureTarget={this.state.camera.captureTarget} aspect={this.state.camera.aspect}
type={this.state.camera.type} captureTarget={this.state.camera.captureTarget}
defaultTouchToFocus type={this.state.camera.type}
mirrorImage={false} defaultTouchToFocus
/> mirrorImage={false}
<View style={[styles.overlay, styles.topOverlay]}> />
<TouchableOpacity style={styles.typeButton} onPress={this.switchCam}> <View style={[styles.overlay, styles.topOverlay]}>
<Image source={this.typeIcon} /> <Icon.Button name={this.typeIcon()} size={36} backgroundColor="transparent" onPress={this.switchCam} />
</TouchableOpacity> <Icon.Button name={this.typeIcon()} size={36} backgroundColor="transparent" onPress={this.switchCam} />
</View> </View>
<View style={[styles.overlay, styles.bottomOverlay]}> <View style={[styles.overlay, styles.bottomOverlay]}>
<TouchableOpacity style={styles.captureButton} onPress={this.recording}> <Icon.Button name="photo-camera" size={48} backgroundColor="transparent" onPress={this.takePicture} />
<Image source={this.camButton} /> <Icon.Button name={this.camButton()} size={48} backgroundColor="transparent" onPress={this.recording} />
</TouchableOpacity> </View>
</View> </View>
</View> );
); } else {
return (
<Image
style={{ height: Dimensions.get('window').height, width: Dimensions.get('window').width }}
source={{ uri: this.state.imageURI, isStatic: true }}
/>
);
}
} }
} }
export default CameraComponent; export default CameraComponent;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论