-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.tsx
147 lines (138 loc) · 4.73 KB
/
App.tsx
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
import React from 'react';
import { Text, View, Alert, TouchableOpacity, StyleSheet, Dimensions, ActivityIndicator, SafeAreaView, ScrollView } from 'react-native';
import Constants from 'expo-constants';
import * as Permissions from 'expo-permissions';
import { Camera } from 'expo-camera';
import * as ImageManipulator from 'expo-image-manipulator';
import { Ionicons } from '@expo/vector-icons';
import { decode as atob, encode as btoa } from 'base-64';
import * as AWS from 'aws-sdk';
import credential from './config';
export default class CameraExample extends React.Component {
camera: any;
state = {
hasCameraPermission: null,
type: Camera.Constants.Type.back,
loading: false,
response: []
};
async componentDidMount() {
const { status } = await Permissions.askAsync(Permissions.CAMERA);
this.setState({ hasCameraPermission: status === 'granted' });
}
takePicture = async () => {
if (this.camera) {
this.camera.takePictureAsync().then(
res => {
this.onProcess(res.uri);
this.setState({
loading: true
})
}
)
}
};
onProcess = async (photo) => {
const compressed = await ImageManipulator.manipulateAsync(
photo,
[{ resize: { width: 200 } }],
{ base64: true }
);
let imageBytes = this.getBinary(compressed.base64);
let params = {
Image: {
Bytes: imageBytes
}
};
let rekognition = new AWS.Rekognition(credential);
const getResult = rekognition.detectText(params).promise();
getResult.then((data) => {
this.setState({
loading: false,
response: data.TextDetections.map(e => e.DetectedText)
});
}).catch((err) => {
this.setState({
loading: false,
response: []
});
});
}
getBinary(base64Image) {
let binaryImg = atob(base64Image);
let length = binaryImg.length;
let ab = new ArrayBuffer(length);
let ua = new Uint8Array(ab);
for (let i = 0; i < length; i++) {
ua[i] = binaryImg.charCodeAt(i);
}
return ab;
}
render() {
const { hasCameraPermission } = this.state;
if (hasCameraPermission === null) {
return <View />;
} else if (hasCameraPermission === false) {
return <Text>No access to camera</Text>;
} else {
return (
<SafeAreaView style={styles.container}>
<ScrollView showsVerticalScrollIndicator={false}>
{
this.state.loading === true &&
<View style={{ backgroundColor: '#fff', padding: 15, height: Dimensions.get('window').height, justifyContent: 'center', flexDirection: 'column' }}>
<ActivityIndicator size="large" color="#999" />
<Text style={{ marginTop: 15, fontSize: 18, textAlign: 'center' }}>Getting Text Results...</Text>
</View>
}
{
this.state.loading === false && this.state.response.length > 0 &&
<View style={{ padding: 15 }}>
<Text style={{ fontWeight: 'bold', textAlign: 'center', marginBottom: 15, fontSize: 22 }}>Predicted Results</Text>
<Text style={{ textAlign: 'center', fontSize: 18, color: 'blue', marginTop: 0, marginBottom: 15, padding: 10 }} onPress={() => {
this.setState({
response: []
})
}}>Clear Results</Text>
{
this.state.response.map((data, i) => (
< Text key={i} style={{ marginTop: 5, padding: 5, fontSize: 16 }}>
{(i + 1) + ' . '} {data}
</Text>
))
}
</View>
}
{
this.state.loading === false && this.state.response.length === 0 &&
<View style={{ display: 'flex', height: Dimensions.get('window').height, justifyContent: 'space-around', alignItems: 'center', flexDirection: 'column' }}>
<Camera
autoFocus={true}
ref={ref => {
this.camera = ref;
}}
style={{ height: 320, width: 220 }} type={this.state.type}>
</Camera>
<View>
<TouchableOpacity
onPress={this.takePicture}
style={{ alignSelf: 'center' }}
>
<Ionicons name="ios-radio-button-on" size={70} color="black" />
</TouchableOpacity>
</View>
</View>
}
</ScrollView>
</SafeAreaView>
);
}
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
marginTop: Constants.statusBarHeight,
backgroundColor: '#fff'
}
});