-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
132 lines (123 loc) · 4.05 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
import React, { useState, useRef, useEffect } from 'react';
import * as Speech from 'expo-speech';
import styles, { cameraIconSize, closeIcon } from './styles';
import { View, Text, TouchableOpacity, SafeAreaView } from 'react-native';
import { Camera } from 'expo-camera';
import * as ImageManipulator from 'expo-image-manipulator';
import { Ionicons } from '@expo/vector-icons';
import deviceLocale from './components/language';
// Set your token and IRIS server url here
const authToken = 'YOUR_AUTH_TOKEN';
const serverUrl = 'https://IRIS_SERVER_URL/upload';
////////////////////////////////////////////////////
const resizeHeight = 1500;
let abortController;
export default function App() {
const [hasPermission, setHasPermission] = useState(null);
const [cameraType] = useState(Camera.Constants.Type.back);
const [isPreview, setIsPreview] = useState(false);
const [isCameraReady, setIsCameraReady] = useState(false);
const cameraRef = useRef();
useEffect(() => {
(async () => {
const { status } = await Camera.requestCameraPermissionsAsync();
setHasPermission(status === 'granted');
})();
}, []);
const onCameraReady = () => {
setIsCameraReady(true);
};
const prepareFormData = (imageUri) => {
const formData = new FormData();
formData.append('file', {
uri: imageUri,
name: 'image.jpg',
type: 'image/jpeg',
});
formData.append('lang', deviceLocale.split('_')[0]);
return formData;
};
const sendReadRequestAsync = (formData, onSuccess) => {
abortController = new AbortController();
fetch(serverUrl, {
method: 'POST',
signal: abortController.signal,
headers: { Authorization: 'Bearer ' + authToken },
body: formData,
})
.then((r) => r.json())
.then((data) => onSuccess(data))
.catch((e) => console.log('Request aborted or rejected'));
};
const takePicture = async () => {
if (cameraRef.current) {
const options = { quality: 0.8, base64: false, skipProcessing: false };
const data = await cameraRef.current.takePictureAsync(options);
const imageUri = data.uri;
if (!imageUri) throw Error('Camera error');
const resizedPhoto = await ImageManipulator.manipulateAsync(
imageUri,
[{ resize: { height: resizeHeight } }],
{ compress: 0.7, format: 'jpeg' }
);
const formData = prepareFormData(resizedPhoto.uri);
sendReadRequestAsync(formData, (response) =>
Speech.speak(response.text, {
language: deviceLocale,
onDone: cancelPreviewAndSpeech,
})
);
await cameraRef.current.pausePreview();
setIsPreview(true);
}
};
const renderCaptureControl = () => (
<View style={styles.control}>
<TouchableOpacity
activeOpacity={0.7}
disabled={!isCameraReady}
onPress={takePicture}
style={styles.capture}>
<Ionicons name="camera-sharp" size={cameraIconSize} color="black" />
</TouchableOpacity>
</View>
);
const cancelPreviewAndSpeech = async () => {
Speech.stop();
abortController.abort();
await cameraRef.current.resumePreview();
setIsPreview(false);
};
const renderCancelPreviewButton = () => (
<TouchableOpacity
onPress={cancelPreviewAndSpeech}
style={styles.closeButton}>
<Ionicons name="close-outline" size={closeIcon} color="#00000066" />
</TouchableOpacity>
);
// Main view
if (hasPermission === null) {
return <View />;
}
if (hasPermission === false) {
return <Text style={styles.text}>No access to camera</Text>;
}
return (
<SafeAreaView style={styles.container}>
<Camera
ref={cameraRef}
style={styles.container}
type={cameraType}
flashMode={Camera.Constants.FlashMode.off}
onCameraReady={onCameraReady}
onMountError={(error) => {
console.log('cammera error', error);
}}
/>
<View style={styles.container}>
{isPreview && renderCancelPreviewButton()}
{!isPreview && renderCaptureControl()}
</View>
</SafeAreaView>
);
}