aboutsummaryrefslogtreecommitdiff
path: root/src/utils/camera.ts
blob: c9dec29275c0269adb69d81cd1956465b72708c6 (plain)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import CameraRoll from '@react-native-community/cameraroll';
import {RefObject} from 'react';
import {Alert} from 'react-native';
import {
  RecordOptions,
  RecordResponse,
  RNCamera,
  TakePictureOptions,
  TakePictureResponse,
} from 'react-native-camera';
import {ProcessingManager} from 'react-native-video-processing';
import ImagePicker, {ImageOrVideo} from 'react-native-image-crop-picker';
import {
  ERROR_UPLOAD,
  ERROR_UPLOAD_EXCEED_MAX_VIDEO_DURATION,
} from '../constants/strings';
import {MAX_VIDEO_RECORDING_DURATION} from '../constants';

/*
 * Captures a photo and pauses to show the preview of the picture taken
 */
export const takePicture = (
  cameraRef: RefObject<RNCamera>,
  callback: (pic: TakePictureResponse) => void,
) => {
  cameraRef.current?.pausePreview();
  if (cameraRef !== null) {
    const options: TakePictureOptions = {
      forceUpOrientation: true,
      orientation: 'portrait',
      writeExif: false,
    };
    cameraRef.current?.takePictureAsync(options).then((pic) => {
      callback(pic);
    });
  }
};

export const takeVideo = (
  cameraRef: RefObject<RNCamera>,
  callback: (vid: RecordResponse) => void,
) => {
  if (cameraRef !== null) {
    const options: RecordOptions = {
      orientation: 'portrait',
      maxDuration: MAX_VIDEO_RECORDING_DURATION,
      quality: '1080p',
    };
    cameraRef.current?.recordAsync(options).then((vid) => {
      callback(vid);
    });
  }
};

export const saveImageToGallery = (
  capturedImageURI: string,
  type: 'photo' | 'video',
) => {
  CameraRoll.save(capturedImageURI, {album: 'Recents', type: type})
    .then((_res) => Alert.alert('Saved to device!'))
    .catch((_err) => Alert.alert('Failed to save to device!'));
};

export const navigateToMediaPicker = (
  callback: (media: ImageOrVideo) => void,
) => {
  ImagePicker.openPicker({
    smartAlbums: [
      'Favorites',
      'RecentlyAdded',
      'SelfPortraits',
      'Screenshots',
      'UserLibrary',
      'Videos',
    ],
    mediaType: 'any',
    compressVideoPreset: 'Passthrough',
  })
    .then((media) => {
      if (
        'duration' in media &&
        media.duration !== null &&
        media.duration > MAX_VIDEO_RECORDING_DURATION * 1000
      ) {
        Alert.alert(ERROR_UPLOAD_EXCEED_MAX_VIDEO_DURATION);
        return;
      }
      callback(media);
    })
    .catch((err) => {
      if (err.code && err.code !== 'E_PICKER_CANCELLED') {
        Alert.alert(ERROR_UPLOAD);
      }
    });
};

export const showGIFFailureAlert = (onSuccess: () => void) =>
  Alert.alert(
    'Warning',
    'The app currently cannot handle GIFs, and will only save a static image.',
    [
      {
        text: 'Cancel',
        onPress: () => {},
        style: 'cancel',
      },
      {
        text: 'Post',
        onPress: onSuccess,
        style: 'default',
      },
    ],
    {
      cancelable: true,
      onDismiss: () =>
        Alert.alert(
          'This alert was dismissed by tapping outside of the alert dialog.',
        ),
    },
  );

export const trimVideo = (
  sourceUri: string,
  handleData: (data: any) => any,
  ends: {
    start: number;
    end: number;
  },
) => {
  ProcessingManager.trim(sourceUri, {
    startTime: ends.start / 2, //needed divide by 2 for bug in module
    endTime: ends.end,
    quality: 'passthrough',
  }).then((data: any) => handleData(data));
};

export const cropVideo = (
  sourceUri: string,
  handleData: (data: any) => any,
  videoCropValues?: {
    cropWidth?: number;
    cropHeight?: number;
    cropOffsetX?: number;
    cropOffsetY?: number;
  },
  muted?: boolean,
) => {
  ProcessingManager.crop(sourceUri, {
    cropWidth: videoCropValues
      ? videoCropValues.cropWidth
        ? Math.round(videoCropValues.cropWidth)
        : 100
      : 100,
    cropHeight: videoCropValues
      ? videoCropValues.cropHeight
        ? Math.round(videoCropValues.cropHeight)
        : 100
      : 100,
    cropOffsetX: videoCropValues
      ? videoCropValues.cropOffsetX
        ? Math.round(videoCropValues.cropOffsetX)
        : 0
      : 0,
    cropOffsetY: videoCropValues
      ? videoCropValues.cropOffsetY
        ? Math.round(videoCropValues.cropOffsetY)
        : 0
      : 0,
    quality: 'passthrough',
  }).then((data: any) => {
    if (muted) {
      removeAudio(data, handleData);
    } else {
      handleData(data);
    }
  });
};

export const removeAudio = (
  sourceUri: string,
  handleData: (data: any) => any,
) => {
  ProcessingManager.compress(sourceUri, {removeAudio: true}).then((data: any) =>
    handleData(data),
  );
};