diff options
author | Ivan Chen <ivan@tagg.id> | 2021-06-29 17:06:19 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-29 17:06:19 -0400 |
commit | 66c974161b59f1e3570e2a4a42334fabc16c2129 (patch) | |
tree | 7a1546101e2966b7bb38bf92bca4be64b1ccd7b6 /src/utils/camera.ts | |
parent | 53bdc94cf0491e348b7d4ad61e51ce1844423773 (diff) | |
parent | d4b210518eaffd3bf1320ca7ce7fa4a6d611528f (diff) |
Merge pull request #476 from shravyaramesh/tma937-tagg-camera
[TMA-937] Tagg Camera
Diffstat (limited to 'src/utils/camera.ts')
-rw-r--r-- | src/utils/camera.ts | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/src/utils/camera.ts b/src/utils/camera.ts new file mode 100644 index 00000000..73461ad7 --- /dev/null +++ b/src/utils/camera.ts @@ -0,0 +1,65 @@ +import CameraRoll from '@react-native-community/cameraroll'; +import {Dispatch, RefObject, SetStateAction} from 'react'; +import {Alert} from 'react-native'; +import {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, + writeExif: false, + }; + 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 = ( + navigation: any, + screenType: ScreenType, + title: string, +) => { + ImagePicker.openPicker({ + smartAlbums: [ + 'Favorites', + 'RecentlyAdded', + 'SelfPortraits', + 'Screenshots', + 'UserLibrary', + ], + mediaType: 'photo', + }) + .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); + } + }); +}; |