-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
174 lines (162 loc) · 3.77 KB
/
App.js
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import {
Component
} from 'react';
import {
StyleSheet,
Image,
View,
TouchableOpacity,
Alert,
} from 'react-native';
import axios from 'axios';
import Spinner from 'react-native-spinkit';
import Camera from 'react-native-camera';
import config from './config/config.json'
export default class App extends Component {
state = {
loading: false,
autoFocus: 'on',
};
async takePicture() {
if (!this.state.loading) {
this.setState({
loading: true
});
try {
const data = await this.camera.capture()
let result = await this.checkForLabels(data.data);
let filteredResult = this.filterLabelsList(result.responses[0], 0.3);
this.displayResult(filteredResult);
this.setState({
loading: false
});
}
catch(err) {
console.error(err)
}
} else {
console.log('NO GO' + this.state.loading)
}
}
displayResult(filteredResult) {
if (filteredResult.length) {
Alert.alert(filteredResult[0].description);
}
}
filterLabelsList(response, minConfidence = 0) {
let resultArr = [];
response.textAnnotations.forEach((label) => {
resultArr.push(label);
});
return resultArr;
}
async checkForLabels(base64) {
try {
const labels = await axios({
method: 'POST',
url: config.googleCloud.api,
params: {
key: config.googleCloud.apiKey
},
data: {
requests: [{
image: {
content: base64
},
features: [{
type: "TEXT_DETECTION"
}]
}]
}
})
console.log(labels)
return labels.data
}
catch(err) {
console.error(err)
};
}
renderCamera() {
return (<Camera
ref={ref => {
this.camera = ref;
}}
style={{
flex: 1,
}}
captureTarget={Camera.constants.CaptureTarget.memory}
autoFocus={this.state.autoFocus}
permissionDialogTitle={'Permission to use camera'}
permissionDialogMessage={'We need your permission to use your camera phone'}
>
<View style={styles.buttons}>
{!this.state.loading ?
<TouchableOpacity
style={[styles.flipButton, { flex: 0.3, alignSelf: 'flex-end' }]}
onPress={this.takePicture.bind(this)}
>
<Image style={styles.camButton} source={require('./assets/camera.png')} />
</TouchableOpacity>
:
<Spinner
style={{alignSelf: 'flex-end' }}
isVisible={true}
size={70}
type={'Bounce'}
color={'white'}/>
}
</View>
</Camera>)
}
render() {
return <View style={styles.container}>
{this.renderCamera()}
</View>;
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
paddingTop: 10,
backgroundColor: '#000',
},
navigation: {
flex: 1,
},
buttons: {
flex: 1,
backgroundColor: 'transparent',
flexDirection: 'row',
justifyContent: 'space-around',
bottom: 1
},
camButton: {
width: 60,
height: 60
},
flipButton: {
flex: 0.3,
height: 60,
marginHorizontal: 2,
marginBottom: 10,
marginTop: 20,
borderRadius: 8,
borderColor: 'white',
borderWidth: 0,
padding: 5,
alignItems: 'center',
justifyContent: 'center',
},
item: {
margin: 4,
backgroundColor: 'indianred',
height: 35,
width: 80,
borderRadius: 5,
alignItems: 'center',
justifyContent: 'center',
},
row: {
flexDirection: 'row',
},
});