|
import React, { useRef } from 'react'; |
|
import { View, TouchableOpacity, Text } from 'react-native'; |
|
import { RNCamera } from 'react-native-camera'; |
|
import RNFS from 'react-native-fs'; |
|
|
|
const CameraScreen = () => { |
|
const cameraRef = useRef(null); |
|
|
|
const takePicture = async () => { |
|
if (cameraRef.current) { |
|
const options = { quality: 0.5, base64: true }; |
|
const data = await cameraRef.current.takePictureAsync(options); |
|
|
|
|
|
saveToGallery(data.uri); |
|
} |
|
}; |
|
|
|
const saveToGallery = async (imageUri) => { |
|
try { |
|
const destinationPath = RNFS.CachesDirectoryPath + '/myImage.jpg'; |
|
|
|
|
|
await RNFS.copyFile(imageUri, destinationPath); |
|
|
|
|
|
RNFS.scanFile(destinationPath); |
|
} catch (error) { |
|
console.error('Error saving image to gallery:', error); |
|
} |
|
}; |
|
|
|
return ( |
|
<View style={{ flex: 1 }}> |
|
<RNCamera |
|
ref={cameraRef} |
|
style={{ flex: 1 }} |
|
type={RNCamera.Constants.Type.back} |
|
captureAudio={false} |
|
/> |
|
<View style={{ flex: 0, flexDirection: 'row', justifyContent: 'center' }}> |
|
<TouchableOpacity onPress={takePicture} style={{ flex: 0, backgroundColor: '#fff', borderRadius: 5, padding: 15, paddingHorizontal: 20, alignSelf: 'center', margin: 20 }}> |
|
<Text style={{ fontSize: 14 }}>Take Picture</Text> |
|
</TouchableOpacity> |
|
</View> |
|
</View> |
|
); |
|
}; |
|
|
|
export default CameraScreen; |
|
|