|
| 1 | +import * as ImagePicker from 'expo-image-picker'; |
| 2 | +import React from 'react'; |
| 3 | +import { Button, Image, StyleSheet, Text, View } from 'react-native'; |
| 4 | + |
| 5 | +const API_KEY = 'ADD_YOUR_KEY_HERE'; |
| 6 | +const API_URL = `https://vision.googleapis.com/v1/images:annotate?key=${API_KEY}`; |
| 7 | + |
| 8 | +async function callGoogleVisionAsync(image) { |
| 9 | + const body = { |
| 10 | + requests: [ |
| 11 | + { |
| 12 | + image: { |
| 13 | + content: image, |
| 14 | + }, |
| 15 | + features: [ |
| 16 | + { |
| 17 | + type: 'LABEL_DETECTION', |
| 18 | + maxResults: 1, |
| 19 | + }, |
| 20 | + ], |
| 21 | + }, |
| 22 | + ], |
| 23 | + }; |
| 24 | + |
| 25 | + const response = await fetch(API_URL, { |
| 26 | + method: 'POST', |
| 27 | + headers: { |
| 28 | + Accept: 'application/json', |
| 29 | + 'Content-Type': 'application/json', |
| 30 | + }, |
| 31 | + body: JSON.stringify(body), |
| 32 | + }); |
| 33 | + const result = await response.json(); |
| 34 | + console.log('callGoogleVisionAsync -> result', result); |
| 35 | + |
| 36 | + return result.responses[0].labelAnnotations[0].description; |
| 37 | +} |
| 38 | + |
| 39 | +export default function App() { |
| 40 | + const [image, setImage] = React.useState(null); |
| 41 | + const [status, setStatus] = React.useState(null); |
| 42 | + const [permissions, setPermissions] = React.useState(false); |
| 43 | + |
| 44 | + const askPermissionsAsync = async () => { |
| 45 | + let permissionResult = await ImagePicker.requestCameraPermissionsAsync(); |
| 46 | + |
| 47 | + if (permissionResult.granted === false) { |
| 48 | + alert('Permission to access camera roll is required!'); |
| 49 | + return; |
| 50 | + } else { |
| 51 | + setPermissions(true); |
| 52 | + } |
| 53 | + }; |
| 54 | + |
| 55 | + const takePictureAsync = async () => { |
| 56 | + const { cancelled, uri, base64 } = await ImagePicker.launchCameraAsync({ |
| 57 | + base64: true, |
| 58 | + }); |
| 59 | + |
| 60 | + if (!cancelled) { |
| 61 | + setImage(uri); |
| 62 | + setStatus('Loading...'); |
| 63 | + try { |
| 64 | + const result = await callGoogleVisionAsync(base64); |
| 65 | + setStatus(result); |
| 66 | + } catch (error) { |
| 67 | + setStatus(`Error: ${error.message}`); |
| 68 | + } |
| 69 | + } else { |
| 70 | + setImage(null); |
| 71 | + setStatus(null); |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + return ( |
| 76 | + <View style={styles.container}> |
| 77 | + {permissions === false ? ( |
| 78 | + <Button onPress={askPermissionsAsync} title="Ask permissions" /> |
| 79 | + ) : ( |
| 80 | + <> |
| 81 | + {image && <Image style={styles.image} source={{ uri: image }} />} |
| 82 | + {status && <Text style={styles.text}>{status}</Text>} |
| 83 | + <Button onPress={takePictureAsync} title="Take a Picture" /> |
| 84 | + </> |
| 85 | + )} |
| 86 | + </View> |
| 87 | + ); |
| 88 | +} |
| 89 | + |
| 90 | +const styles = StyleSheet.create({ |
| 91 | + container: { |
| 92 | + flex: 1, |
| 93 | + backgroundColor: 'white', |
| 94 | + alignItems: 'center', |
| 95 | + justifyContent: 'center', |
| 96 | + }, |
| 97 | + image: { |
| 98 | + width: 300, |
| 99 | + height: 300, |
| 100 | + }, |
| 101 | + text: { |
| 102 | + margin: 5, |
| 103 | + }, |
| 104 | +}); |
0 commit comments