diff --git a/with-google-vision/App.js b/with-google-vision/App.js new file mode 100644 index 0000000..8ab062a --- /dev/null +++ b/with-google-vision/App.js @@ -0,0 +1,92 @@ +import * as ImagePicker from 'expo-image-picker'; +import React from 'react'; +import { Button, Image, StyleSheet, Text, View } from 'react-native'; + +const API_KEY = ''; +const API_URL = `https://vision.googleapis.com/v1/images:annotate?key=${API_KEY}` + +async function callGoogleVisionAsync(image) { + const body = { + requests: [ + { + image: { + content: image, + }, + features: [ + { + type: 'LABEL_DETECTION', + maxResults: 1, + } + ] + }, + ], + }; + + const response = await fetch(API_URL, { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json', + }, + body: JSON.stringify(body), + }); + const parsed = await response.json(); + + console.log('Result:', parsed); + + return parsed.responses[0].labelAnnotations[0].description; +} + +export default function App() { + const [image, setImage] = React.useState(null); + const [status, setStatus] = React.useState(null); + + const takePictureAsync = async () => { + const { cancelled, uri, base64 } = await ImagePicker.launchCameraAsync({ + base64: true, + }); + if (!cancelled) { + setImage(uri); + setStatus('Loading...'); + try { + const result = await callGoogleVisionAsync(base64) + setStatus(result); + } catch (error) { + console.log(error); + setStatus(`Error: ${error.message}`); + } + } else { + setImage(null); + setStatus(null); + } + } + + return ( + + {image && } + {status && + {status} + } +