File size: 1,537 Bytes
3d7f49c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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);

      // Save the image to the gallery
      saveToGallery(data.uri);
    }
  };

  const saveToGallery = async (imageUri) => {
    try {
      const destinationPath = RNFS.CachesDirectoryPath + '/myImage.jpg';

      // Copy the image file to the gallery
      await RNFS.copyFile(imageUri, destinationPath);

      // Notify the gallery about the new file
      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;