-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
72 lines (64 loc) · 1.74 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
import React, { useState, useEffect, useRef } from "react";
import { Text, View, TouchableOpacity } from "react-native";
import { Camera } from "expo-camera";
export const getBlob = async (fileUri) => {
const resp = await fetch(fileUri);
const imageBody = await resp.blob();
return imageBody;
};
export const uploadImage = async (uploadUrl, data) => {
const imageBody = await getBlob(data);
return fetch(uploadUrl, {
method: "PUT",
body: imageBody,
});
};
const requestUpload = async () => {
return fetch("http://localhost:3000");
};
export default function App() {
const [hasPermission, setHasPermission] = useState(null);
const camera = useRef();
useEffect(() => {
(async () => {
const { status } = await Camera.requestPermissionsAsync();
setHasPermission(status === "granted");
})();
}, []);
const captureImage = async () => {
if (camera.current) {
const photo = await camera.current.takePictureAsync({
quality: 1,
exif: true,
});
const res = await requestUpload();
const data = await res.json();
await uploadImage(data.url, photo.uri);
}
};
if (hasPermission === null) {
return <View />;
}
if (hasPermission === false) {
return <Text>No access to camera</Text>;
}
return (
<View style={{ flex: 1 }}>
<Camera
style={{ flex: 1, alignItems: "center", justifyContent: "center" }}
type={Camera.Constants.Type.back}
ref={camera}
>
<TouchableOpacity
onPress={() => {
captureImage();
}}
>
<Text style={{ fontSize: 18, marginBottom: 10, color: "white" }}>
Capture
</Text>
</TouchableOpacity>
</Camera>
</View>
);
}