-
Notifications
You must be signed in to change notification settings - Fork 2
Description
import React, { useState,useRef } from 'react'; import { View, Button, Image } from 'react-native'; import { Camera } from 'expo-camera'; import * as ImageManipulator from 'expo-image-manipulator'; export default function CameraScreen() { const [hasPermission, setHasPermission] = useState(null); const [photoUri, setPhotoUri] = useState(null); const cameraRef = useRef(null); React.useEffect(() => { (async () => { const { status } = await Camera.requestCameraPermissionsAsync(); setHasPermission(status === 'granted'); })(); }, []); const takePicture = async () => { if (cameraRef.current) { const photo = await cameraRef.current.takePictureAsync(); setPhotoUri(photo.uri); uploadToServer(photo.uri); } }; const uploadToServer = async (uri) => { const formData = new FormData(); formData.append('image', { uri, name: 'problem.jpg', type: 'image/jpeg', }); const response = await fetch('http://your-server-address:5000/solve', { method: 'POST', body: formData, headers: { 'Content-Type': 'multipart/form-data', }, }); const result = await response.json(); alert(Solution: ${result.solution}); }; return ( <View style={{ flex: 1 }}> {hasPermission && ( <Camera style={{ flex: 1 }} ref={cameraRef}> )} {photoUri && <Image source={{ uri: photoUri }} style={{ width: 300, height: 300 }} />} ); }