|
'use strict';
import React, { Component } from 'react';
import {
Alert,
Dimensions,
StyleSheet,
Text,
TouchableOpacity,
View
} from 'react-native';
import { RNCamera } from 'react-native-camera';
export default class ReactNativeCamera extends Component {
render() {
let showDialog=true;
const handleBarcode = (data) => {
if(showDialog) {
Alert.alert(data.type.toString(),
data.data.toString(),
[
{text: 'OK', onPress: () => showDialog=true},
]
)
showDialog=false;
}
}
return (
<View style={styles.container}>
<RNCamera
ref={ref => {
this.camera = ref;
}}
style = {styles.preview}
type={RNCamera.Constants.Type.back}
flashMode={RNCamera.Constants.FlashMode.on}
permissionDialogTitle={'Permission to use camera'}
permissionDialogMessage={'We need your permission to use your camera phone'}
onBarCodeRead={handleBarcode}
/>
<View style={{flex: 0, flexDirection: 'row', justifyContent: 'center',}}>
<TouchableOpacity
onPress={this.takePicture.bind(this)}
onLongPress={this.recordVideo.bind(this)}
style = {styles.capture}
>
<Text style={{fontSize: 14, color: 'black'}}> SNAP </Text>
</TouchableOpacity>
</View>
</View>
);
}
takePicture = async function() {
if (this.camera) {
const options = { quality: 0.5, base64: true };
const data = await this.camera.takePictureAsync(options);
console.log(data.uri);
Alert.alert('拍照完成',
data.uri,
[
{text: 'OK'},
]
)
}
};
recordVideo = async function() {
if (this.camera) {
const options = { maxDuration: 5};
const data = await this.camera.recordAsync(options);
console.log("video:" + data.uri);
Alert.alert('录相完成',
data.uri,
[
{text: 'OK'},
]
)
}
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
backgroundColor: 'black'
},
preview: {
flex: 1,
justifyContent: 'flex-end',
alignItems: 'center'
},
capture: {
flex: 0,
backgroundColor: '#fff',
borderRadius: 5,
padding: 15,
paddingHorizontal: 20,
alignSelf: 'center',
margin: 20
}
});
|