-
Notifications
You must be signed in to change notification settings - Fork 12
/
camera-ui-client.js
86 lines (76 loc) · 2.41 KB
/
camera-ui-client.js
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
/**
* Helper function for uploading base 64 image to server.
* @summary Take JPEG image dataURI and return blob of the given content type
* @returns {Blob}
*/
MeteorCameraUI.dataURIToBlob = function (dataURI) {
// Check that input exists
if (!dataURI) {
return;
}
//
var splitedDataURI = dataURI.split(',');
// Split the image base64 from dataURI
if (!splitedDataURI || splitedDataURI.length < 2) {
return;
}
var base64 = splitedDataURI[1];
return MeteorCameraUI.b64toBlob(base64, 'image/jpeg');
};
/**
* Helper function for uploading base 64 image to server.
* @summary Take base 64 data and return blob of the given content type
* @param b64Data
* @param contentType
* @param sliceSize
* @returns {Blob}
*/
MeteorCameraUI.b64toBlob = function (b64Data, contentType, sliceSize) {
contentType = contentType || '';
sliceSize = sliceSize || 512;
var byteCharacters = atob(b64Data);
var byteArrays = [];
for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
var slice = byteCharacters.slice(offset, offset + sliceSize);
var byteNumbers = new Array(slice.length);
for (var i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
var blob;
try {
blob = new Blob(byteArrays, {type: contentType});
}
catch (e) {
// TypeError old chrome and FF
window.BlobBuilder = window.BlobBuilder ||
window.WebKitBlobBuilder ||
window.MozBlobBuilder ||
window.MSBlobBuilder;
if (e.name == 'TypeError' && window.BlobBuilder) {
var bb = new BlobBuilder();
bb.append(byteArrays);
blob = bb.getBlob(contentType);
}
else if (e.name == "InvalidStateError") {
// InvalidStateError (tested on FF13 WinXP)
blob = new Blob(byteArrays, {type: contentType});
}
else {
// We're screwed, blob constructor unsupported entirely
console.error('b64toBlob: blob constructor unsupported entirely');
}
}
return blob;
};
/**
* @summary Just use regular MeteorCamera.getPicture method
* @param {Object} options Options - Optional
* @param {Number} options.height The minimum height of the image
* @param {Number} options.width The minimum width of the image
* @param {Number} options.quality [description]
* @type {Function}
*/
MeteorCameraUI.getPictureNoUI = MeteorCamera.getPicture;