feat: implement plant health check functionality with AI analysis, credit management, and UI integration
This commit is contained in:
@@ -7,6 +7,7 @@ import { Ionicons } from '@expo/vector-icons';
|
||||
import { CameraView, useCameraPermissions } from 'expo-camera';
|
||||
import { useSafeAreaInsets } from 'react-native-safe-area-context';
|
||||
import * as ImagePicker from 'expo-image-picker';
|
||||
import * as ImageManipulator from 'expo-image-manipulator';
|
||||
import * as Haptics from 'expo-haptics';
|
||||
import { usePostHog } from 'posthog-react-native';
|
||||
import { useApp } from '../context/AppContext';
|
||||
@@ -175,6 +176,20 @@ export default function ScannerScreen() {
|
||||
};
|
||||
}, [isAnalyzing, scanLineProgress, scanPulse]);
|
||||
|
||||
const resizeForAnalysis = async (uri: string): Promise<string> => {
|
||||
if (uri.startsWith('data:')) return uri;
|
||||
try {
|
||||
const result = await ImageManipulator.manipulateAsync(
|
||||
uri,
|
||||
[{ resize: { width: 1024 } }],
|
||||
{ compress: 0.6, format: ImageManipulator.SaveFormat.JPEG, base64: true },
|
||||
);
|
||||
return result.base64 ? `data:image/jpeg;base64,${result.base64}` : result.uri;
|
||||
} catch {
|
||||
return uri;
|
||||
}
|
||||
};
|
||||
|
||||
const analyzeImage = async (imageUri: string, galleryImageUri?: string) => {
|
||||
if (isAnalyzing) return;
|
||||
|
||||
@@ -342,7 +357,7 @@ export default function ScannerScreen() {
|
||||
const takePicture = async () => {
|
||||
if (!cameraRef.current || isAnalyzing) return;
|
||||
await Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium);
|
||||
const photo = await cameraRef.current.takePictureAsync({ base64: true, quality: 0.7 });
|
||||
const photo = await cameraRef.current.takePictureAsync({ base64: true, quality: 0.5 });
|
||||
if (photo) {
|
||||
const analysisUri = photo.base64
|
||||
? `data:image/jpeg;base64,${photo.base64}`
|
||||
@@ -358,17 +373,14 @@ export default function ScannerScreen() {
|
||||
|
||||
const result = await ImagePicker.launchImageLibraryAsync({
|
||||
mediaTypes: ['images'],
|
||||
quality: 0.7,
|
||||
base64: true,
|
||||
quality: 1,
|
||||
base64: false,
|
||||
});
|
||||
if (!result.canceled && result.assets[0]) {
|
||||
const asset = result.assets[0];
|
||||
const uri = asset.base64
|
||||
? `data:image/jpeg;base64,${asset.base64}`
|
||||
: asset.uri;
|
||||
|
||||
setSelectedImage(uri);
|
||||
analyzeImage(uri, asset.uri || uri);
|
||||
const analysisUri = await resizeForAnalysis(asset.uri);
|
||||
setSelectedImage(asset.uri);
|
||||
analyzeImage(analysisUri, asset.uri);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user