http://www.hangge.com/blog/cache/detail_1618.html
1
|
npm install react-native-camera
@latest
--save
|
1
|
react-native link react-native-camera
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
import
React
, {
Component
} from
'react'
;
import
{
AppRegistry
,
Dimensions
,
StyleSheet
,
Text
,
TouchableHighlight
,
View
} from
'react-native'
;
import
Camera
from
'react-native-camera'
;
class
App
extends
Component
{
//构造函数
constructor(props) {
super
(props);
this.state = {
cameraType:
Camera
.constants.
Type
.back
};
}
//渲染
render() {
return
(
<
View
style={styles.container}>
<
Camera
ref={(cam) => {
this.camera = cam;
}}
style={styles.preview}
type={this.state.cameraType}
aspect={
Camera
.constants.
Aspect
.fill}>
<
Text
style={styles.button} onPress={this.switchCamera.bind(this)}>[切换摄像头]
Text
>
<
Text
style={styles.button} onPress={this.takePicture.bind(this)}>[拍照]
Text
>
Camera
>
View
>
);
}
//切换前后摄像头
switchCamera() {
var
state = this.state;
if
(state.cameraType ===
Camera
.constants.
Type
.back) {
state.cameraType =
Camera
.constants.
Type
.front;
}
else
{
state.cameraType =
Camera
.constants.
Type
.back;
}
this.setState(state);
}
//拍摄照片
takePicture() {
this.camera.capture()
.then(function(data){
alert(
"拍照成功!图片保存地址:\n"
+data.path)
})
.catch(err => console.error(err));
}
}
const styles =
StyleSheet
.create({
container: {
flex: 1,
flexDirection:
'row'
,
},
preview: {
flex: 1,
justifyContent:
'space-between'
,
alignItems:
'flex-end'
,
flexDirection:
'row'
,
},
toolBar: {
width: 200,
margin: 40,
backgroundColor:
'#000000'
,
justifyContent:
'space-between'
,
},
button: {
flex: 0,
backgroundColor:
'#fff'
,
borderRadius: 5,
color:
'#000'
,
padding: 10,
margin: 40,
}
});
AppRegistry
.registerComponent(
'ReactDemo'
, () =>
App
);
|