-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
133 lines (116 loc) · 2.75 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
import React, { useEffect, useState } from "react";
import { Button, Image, StyleSheet, Text, View } from "react-native";
import * as ImagePicker from "expo-image-picker";
import { ImageEditor } from "expo-image-editor";
const SERVER_URL = "https://chem-app-api.herokuapp.com";
export default function App() {
const [image, setImage] = useState(null);
const [result, setResult] = useState(null);
const [editorVisible, setEditorVisible] = useState(false);
useEffect(() => {
console.log(image);
}, [image]);
const openCamera = async () => {
setResult(null);
const permissionResult = await ImagePicker.requestCameraPermissionsAsync();
if (!permissionResult.granted) {
console.log("You refused to allow the app to access the camera!");
return;
}
const result = await ImagePicker.launchCameraAsync();
if (result.cancelled) {
setImage(null);
} else {
setImage(result);
setEditorVisible(true);
}
};
const openPhotos = async () => {
setResult(null);
const result = await ImagePicker.launchImageLibraryAsync();
if (result.cancelled) {
setImage(null);
} else {
setImage(result);
setEditorVisible(true);
}
};
const uploadPhoto = async () => {
const data = new FormData();
const file = {
uri: image.uri,
name: "userPhoto.jpg",
type: "image/jpg",
};
data.append("file", file);
const response = await fetch(`${SERVER_URL}/pic`, {
method: "POST",
body: data,
});
const json = await response.json();
console.log(json)
setResult(json["result"]);
};
return (
<View style={styles.container}>
<Text style={styles.title}>Antimony Meter</Text>
<View style={styles.imageContainer}>
{image && <Image source={{ uri: image.uri }} style={styles.image} />}
</View>
<Text style={styles.result}>
Concentration: {Math.round(result * 10) / 10}
</Text>
<View style={styles.buttons}>
<Button title="Take Photo" onPress={openCamera} style={styles.button} />
<Button
title="Choose from Library"
onPress={openPhotos}
style={styles.button}
/>
</View>
<ImageEditor
visible={editorVisible}
onCloseEditor={() => {
setEditorVisible(false);
}}
imageUri={image ? image.uri : null}
onEditingComplete={(result) => {
setImage(result);
uploadPhoto();
}}
mode="crop-only"
/>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
title: {
fontSize: 32,
fontWeight: "bold",
position: "absolute",
top: "10%",
},
imageContainer: {
width: 300,
height: 300,
borderWidth: 2,
},
image: {
flex: 1,
resizeMode: "contain",
},
result: {
fontSize: 18,
fontWeight: "700",
},
buttons: {
marginTop: 100,
},
button: {},
});