Update react_native.tsx
Browse files- react_native.tsx +50 -0
react_native.tsx
CHANGED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React, { useRef } from 'react';
|
| 2 |
+
import { View, TouchableOpacity, Text } from 'react-native';
|
| 3 |
+
import { RNCamera } from 'react-native-camera';
|
| 4 |
+
import RNFS from 'react-native-fs';
|
| 5 |
+
|
| 6 |
+
const CameraScreen = () => {
|
| 7 |
+
const cameraRef = useRef(null);
|
| 8 |
+
|
| 9 |
+
const takePicture = async () => {
|
| 10 |
+
if (cameraRef.current) {
|
| 11 |
+
const options = { quality: 0.5, base64: true };
|
| 12 |
+
const data = await cameraRef.current.takePictureAsync(options);
|
| 13 |
+
|
| 14 |
+
// Save the image to the gallery
|
| 15 |
+
saveToGallery(data.uri);
|
| 16 |
+
}
|
| 17 |
+
};
|
| 18 |
+
|
| 19 |
+
const saveToGallery = async (imageUri) => {
|
| 20 |
+
try {
|
| 21 |
+
const destinationPath = RNFS.CachesDirectoryPath + '/myImage.jpg';
|
| 22 |
+
|
| 23 |
+
// Copy the image file to the gallery
|
| 24 |
+
await RNFS.copyFile(imageUri, destinationPath);
|
| 25 |
+
|
| 26 |
+
// Notify the gallery about the new file
|
| 27 |
+
RNFS.scanFile(destinationPath);
|
| 28 |
+
} catch (error) {
|
| 29 |
+
console.error('Error saving image to gallery:', error);
|
| 30 |
+
}
|
| 31 |
+
};
|
| 32 |
+
|
| 33 |
+
return (
|
| 34 |
+
<View style={{ flex: 1 }}>
|
| 35 |
+
<RNCamera
|
| 36 |
+
ref={cameraRef}
|
| 37 |
+
style={{ flex: 1 }}
|
| 38 |
+
type={RNCamera.Constants.Type.back}
|
| 39 |
+
captureAudio={false}
|
| 40 |
+
/>
|
| 41 |
+
<View style={{ flex: 0, flexDirection: 'row', justifyContent: 'center' }}>
|
| 42 |
+
<TouchableOpacity onPress={takePicture} style={{ flex: 0, backgroundColor: '#fff', borderRadius: 5, padding: 15, paddingHorizontal: 20, alignSelf: 'center', margin: 20 }}>
|
| 43 |
+
<Text style={{ fontSize: 14 }}>Take Picture</Text>
|
| 44 |
+
</TouchableOpacity>
|
| 45 |
+
</View>
|
| 46 |
+
</View>
|
| 47 |
+
);
|
| 48 |
+
};
|
| 49 |
+
|
| 50 |
+
export default CameraScreen;
|