import React, { useState } from 'react';
import { View, Text, Button, Image, ScrollView, TouchableOpacity } from 'react-native';
import { createWorker } from 'tesseract.js';
import { SafeAreaView } from 'react-native-safe-area-context';
import * as ImagePicker from 'expo-image-picker';
const CapturePhotos = () => {
const [images, setImages] = useState([]);
const [extractedText, setExtractedText] = useState('');
const pickImage = async () => {
const result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.Images,
aspect: [4, 3],
quality: 1,
allowsMultipleSelection: true,
})
if (!result.cancelled) {
console.log(result.assets[0].uri);
setImages([...images, result.assets[0].uri]);
}
};
const processImageWithOCR = async (uri) => {
const worker = await createWorker(
'eng'
);
const { data: { text } } = await worker.recognize(uri);
console.log(text);
return data.text;
};
return (
<SafeAreaView className="flex-1">
<View className="p-4">
{/* Button to pick images */}
<TouchableOpacity
className="bg-blue-500 rounded-md p-3 mb-4"
onPress={pickImage}
>
<Text className="text-white text-center">Capture Page</Text>
</TouchableOpacity>
<ScrollView className="mt-4">
{images.map((uri, index) => (
<Image key={index} source={{ uri }} className="w-24 h-36 mb-4" resizeMode='contain' />
))}
</ScrollView>
{/* Button to process OCR */}
<TouchableOpacity
className="bg-green-500 rounded-md p-3 mt-4"
onPress={processImageWithOCR}
>
<Text className="text-white text-center">Process Images</Text>
</TouchableOpacity>
{/* Display extracted text */}
{/* <ScrollView className="mt-4">
<Text className="text-base">{extractedText}</Text>
</ScrollView> */}
</View>
</SafeAreaView>
);
};
export default CapturePhotos;