aboutsummaryrefslogtreecommitdiff
path: root/src/utils/camera.ts
diff options
context:
space:
mode:
authorShravya Ramesh <shravs1208@gmail.com>2021-06-25 16:50:00 -0700
committerShravya Ramesh <shravs1208@gmail.com>2021-06-25 16:50:00 -0700
commit727c6384a2a07c42cd132d02da8c7dbb5757ea4f (patch)
tree8019a333d344c93f5f08614b317c23b5c5db8ad4 /src/utils/camera.ts
parentf596a0246a9b9453df3a93c8c3fc5c9137bb50fc (diff)
Refactor code, Fix orientation bug
Diffstat (limited to 'src/utils/camera.ts')
-rw-r--r--src/utils/camera.ts67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/utils/camera.ts b/src/utils/camera.ts
new file mode 100644
index 00000000..fc2471e5
--- /dev/null
+++ b/src/utils/camera.ts
@@ -0,0 +1,67 @@
+import CameraRoll from '@react-native-community/cameraroll';
+import {useNavigation} from '@react-navigation/native';
+import {Dispatch, RefObject, SetStateAction} from 'react';
+import {Alert} from 'react-native';
+import {Orientation, RNCamera} from 'react-native-camera';
+import ImagePicker from 'react-native-image-crop-picker';
+import {ScreenType} from 'src/types';
+import {ERROR_UPLOAD} from '../constants/strings';
+
+/*
+ * Captures a photo and pauses to shoe the preview of the picture taken
+ */
+export const takePicture = (
+ cameraRef: RefObject<RNCamera>,
+ setShowSaveButton: Dispatch<SetStateAction<boolean>>,
+ setCapturedImage: Dispatch<SetStateAction<string>>,
+) => {
+ if (cameraRef !== null) {
+ cameraRef.current?.pausePreview();
+ const options = {
+ forceUpOrientation: true,
+ quality: 0.5,
+ base64: true,
+ };
+ cameraRef.current?.takePictureAsync(options).then((response) => {
+ setShowSaveButton(true);
+ setCapturedImage(response.uri);
+ });
+ }
+};
+
+export const downloadImage = (capturedImageURI: string) => {
+ CameraRoll.save(capturedImageURI, {album: 'Recents', type: 'photo'})
+ .then((_res) => Alert.alert('Saved to device!'))
+ .catch((_err) => Alert.alert('Failed to save to device!'));
+};
+
+export const navigateToImagePicker = (
+ screenType: ScreenType,
+ title: string,
+) => {
+ const navigation = useNavigation();
+ ImagePicker.openPicker({
+ smartAlbums: [
+ 'Favorites',
+ 'RecentlyAdded',
+ 'SelfPortraits',
+ 'Screenshots',
+ 'UserLibrary',
+ ],
+ mediaType: 'any',
+ })
+ .then((picture) => {
+ if ('path' in picture) {
+ navigation.navigate('ZoomInCropper', {
+ screenType,
+ title,
+ image: picture,
+ });
+ }
+ })
+ .catch((err) => {
+ if (err.code && err.code !== 'E_PICKER_CANCELLED') {
+ Alert.alert(ERROR_UPLOAD);
+ }
+ });
+};