-
Notifications
You must be signed in to change notification settings - Fork 374
Expand file tree
/
Copy pathtakePhoto.ts
More file actions
115 lines (106 loc) · 3.79 KB
/
takePhoto.ts
File metadata and controls
115 lines (106 loc) · 3.79 KB
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
import { Image, Platform } from 'react-native';
import mime from 'mime';
let ImagePicker;
try {
ImagePicker = require('expo-image-picker');
} catch (e) {
// do nothing
}
if (!ImagePicker) {
console.log(
'expo-image-picker is not installed. Installing this package will enable capturing photos and videos(for iOS) through the app, and thereby send it.',
);
}
type Size = {
height?: number;
width?: number;
};
// Media type mapping for iOS and Android
const mediaTypeMap = {
image: 'images',
mixed: ['images', 'videos'],
video: 'videos',
};
export const takePhoto = ImagePicker
? async ({ compressImageQuality = 1, mediaType = Platform.OS === 'ios' ? 'mixed' : 'image' }) => {
try {
const permissionCheck = await ImagePicker.getCameraPermissionsAsync();
const canRequest = permissionCheck.canAskAgain;
let permissionGranted = permissionCheck.granted;
if (!permissionGranted) {
if (canRequest) {
const response = await ImagePicker.requestCameraPermissionsAsync();
permissionGranted = response.granted;
} else {
return { askToOpenSettings: true, cancelled: true };
}
}
if (permissionGranted) {
const result = await ImagePicker.launchCameraAsync({
mediaTypes: mediaTypeMap[mediaType],
quality: Math.min(Math.max(0, compressImageQuality), 1),
});
if (!result || !result.assets || !result.assets.length || result.canceled) {
return { cancelled: true };
}
// since we only support single photo upload for now we will only be focusing on 0'th element.
const photo = result.assets[0];
if (!photo) {
return { cancelled: true };
}
const mimeType =
photo.mimeType || mime.getType(photo.uri) || (photo.duration ? 'video/*' : 'image/*');
if (mimeType.includes('video')) {
const clearFilter = new RegExp('[.:]', 'g');
const date = new Date().toISOString().replace(clearFilter, '_');
return {
...photo,
cancelled: false,
duration: photo.duration, // in milliseconds
name: 'video_recording_' + date + '.' + photo.uri.split('.').pop(),
size: photo.fileSize,
type: mimeType,
uri: photo.uri,
};
} else {
if (photo && photo.height && photo.width && photo.uri) {
let size: Size = {};
if (Platform.OS === 'android') {
const getSize = (): Promise<Size> =>
new Promise((resolve) => {
Image.getSize(photo.uri, (width, height) => {
resolve({ height, width });
});
});
try {
const { height, width } = await getSize();
size.height = height;
size.width = width;
} catch (e) {
console.warn('Error get image size of picture caputred from camera ', e);
}
} else {
size = {
height: photo.height,
width: photo.width,
};
}
const clearFilter = new RegExp('[.:]', 'g');
const date = new Date().toISOString().replace(clearFilter, '_');
return {
cancelled: false,
name: 'image_' + date + '.' + photo.uri.split('.').pop(),
size: photo.fileSize,
type: mimeType,
uri: photo.uri,
...size,
};
}
}
}
} catch (error) {
console.log(error);
}
return { cancelled: true };
}
: null;