This repository has been archived by the owner on Apr 18, 2019. It is now read-only.
/
capture.js
380 lines (340 loc) · 12 KB
/
capture.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
* PhoneGap is available under *either* the terms of the modified BSD license *or* the
* MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
*
* Copyright (c) 2005-2011, Nitobi Software Inc.
* Copyright (c) 2011, IBM Corporation
* Copyright (c) 2011, Research In Motion Limited
*/
/**
* MediaFileData error.
*/
function MediaFileDataError() {
this.code = 0;
};
MediaFileDataError.UNKNOWN_ERROR = 0;
MediaFileDataError.TIMEOUT_ERROR = 1;
/**
* Represents media file properties.
*/
var MediaFile = MediaFile || (function() {
/**
* Constructor.
*/
function MediaFile() {
MediaFile.__super__.constructor.apply(this, arguments);
};
// extend File
PhoneGap.extend(MediaFile, File);
/**
* Media file data.
* codecs {DOMString} The actual format of the audio and video content.
* bitrate {Number} The average bitrate of the content. In the case of an image, this attribute has value 0.
* height {Number} The height of the image or video in pixels. In the case of a sound clip, this attribute has value 0.
* width {Number The width of the image or video in pixels. In the case of a sound clip, this attribute has value 0.
* duration {Number} The length of the video or sound clip in seconds. In the case of an image, this attribute has value 0.
*/
function MediaFileData() {
this.codecs = null;
this.bitrate = 0;
this.height = 0;
this.width = 0;
this.duration = 0;
};
/**
* Obtains the format data of the media file.
*/
MediaFile.prototype.getFormatData = function(successCallback, errorCallback) {
// there is no API (WebWorks or native) that provides this info
try {
successCallback(new MediaFileData());
}
catch (e) {
console.log('Unable to invoke success callback: ' + e);
}
};
return MediaFile;
}());
/**
* Media capture error.
*/
function CaptureError() {
this.code = 0;
};
// Camera or microphone failed to capture image or sound.
CaptureError.CAPTURE_INTERNAL_ERR = 0;
// Camera application or audio capture application is currently serving other capture request.
CaptureError.CAPTURE_APPLICATION_BUSY = 1;
// Invalid use of the API (e.g. limit parameter has value less than one).
CaptureError.CAPTURE_INVALID_ARGUMENT = 2;
// User exited camera application or audio capture application before capturing anything.
CaptureError.CAPTURE_NO_MEDIA_FILES = 3;
// The requested capture operation is not supported.
CaptureError.CAPTURE_NOT_SUPPORTED = 20;
/**
* Encapsulates a set of parameters that the capture device supports.
*/
function ConfigurationData() {
// The ASCII-encoded string in lower case representing the media type.
this.type;
// The height attribute represents height of the image or video in pixels.
// In the case of a sound clip this attribute has value 0.
this.height = 0;
// The width attribute represents width of the image or video in pixels.
// In the case of a sound clip this attribute has value 0
this.width = 0;
};
/**
* Encapsulates all image capture operation configuration options.
*/
function CaptureImageOptions() {
// Upper limit of images user can take. Value must be equal or greater than 1.
this.limit = 1;
// The selected image mode. Must match with one of the elements in supportedImageModes array.
this.mode;
};
/**
* Encapsulates all video capture operation configuration options.
*/
function CaptureVideoOptions() {
// Upper limit of videos user can record. Value must be equal or greater than 1.
this.limit = 1;
// Maximum duration of a single video clip in seconds.
this.duration;
// The selected video mode. Must match with one of the elements in supportedVideoModes array.
this.mode;
};
/**
* Encapsulates all audio capture operation configuration options.
*/
function CaptureAudioOptions() {
// Upper limit of sound clips user can record. Value must be equal or greater than 1.
this.limit = 1;
// Maximum duration of a single sound clip in seconds.
this.duration;
// The selected audio mode. Must match with one of the elements in supportedAudioModes array.
this.mode;
};
/**
* navigator.device.capture
*/
(function() {
/**
* Check that navigator.device.capture has not been initialized.
*/
if (navigator.device && typeof navigator.device.capture !== 'undefined') {
return;
}
/**
* Identification string for the capture plugin.
*/
var captureId = 'navigator.device.capture';
/**
* Media capture object.
*/
function Capture() {
var self = this,
// let PhoneGap know we're ready after retrieving all of the
// supported capture modes
addCaptureModes = function(type, modes) {
self[type] = modes;
if (typeof self.supportedAudioModes !== 'undefined'
&& typeof self.supportedImageModes !== 'undefined'
&& typeof self.supportedVideoModes !== 'undefined') {
PhoneGap.initializationComplete(captureId);
}
};
// populate supported capture modes
PhoneGap.exec(function(modes) {
addCaptureModes('supportedAudioModes', parseArray(modes));
}, function(error) {
console.log('Unable to retrieve supported audio modes: ' + error);
addCaptureModes('supportedAudioModes', []);
}, 'MediaCapture', 'getSupportedAudioModes', []);
PhoneGap.exec(function(modes) {
addCaptureModes('supportedImageModes', parseArray(modes));
}, function(error) {
console.log('Unable to retrieve supported image modes: ' + error);
addCaptureModes('supportedImageModes', []);
}, 'MediaCapture', 'getSupportedImageModes', []);
PhoneGap.exec(function(modes) {
addCaptureModes('supportedVideoModes', parseArray(modes));
}, function(error) {
console.log('Unable to retrieve supported video modes: ' + error);
addCaptureModes('supportedVideoModes', []);
}, 'MediaCapture', 'getSupportedVideoModes', []);
};
/**
* Utility function to parse JSON array.
*/
var parseArray = function(array) {
var result = [];
// get objects from JSONArray
try {
result = JSON.parse(array);
}
catch (e) {
console.log('unable to parse JSON: ' + e);
return result;
}
return result;
};
/**
* Utility function to create MediaFile objects from JSON.
*/
var getMediaFiles = function(array) {
var mediaFiles = [], file, objs, obj, len, i, j;
objs = parseArray(array);
for (i = 0; len = objs.length, i < len; i += 1) {
obj = objs[i];
file = new MediaFile();
for (j in obj) {
file[j] = obj[j];
}
mediaFiles.push(file);
}
return mediaFiles;
};
/**
* Static method for invoking error callbacks.
*
* @param error CaptureError code
* @param errorCallback error callback to invoke
*/
Capture.onError = function(error, errorCallback) {
var err = new CaptureError();
err.code = error;
try {
errorCallback(err);
} catch (e) {
console.log('Error invoking callback: ' + e);
}
};
/**
* Launch camera application and start an operation to record images.
*
* @param successCallback
* invoked with a list of MediaFile objects containing captured
* image file properties
* @param errorCallback
* invoked with a CaptureError if capture is unsuccessful
* @param options
* {CaptureVideoOptions} options for capturing video
*/
Capture.prototype.captureImage = function(successCallback, errorCallback, options) {
var limit = 1,
mode = null;
if (options) {
if (typeof options.limit === 'number' && options.limit > limit) {
limit = options.limit;
}
if (options.mode) {
mode = options.mode;
}
}
PhoneGap.exec(
function(mediaFiles) {
successCallback(getMediaFiles(mediaFiles));
},
function(error) {
Capture.onError(error, errorCallback);
},
'MediaCapture',
'captureImage',
[limit, mode]
);
};
/**
* Launch video recorder application and start an operation to record video
* clips.
*
* @param successCallback
* invoked with a list of MediaFile objects containing captured
* video file properties
* @param errorCallback
* invoked with a CaptureError if capture is unsuccessful
* @param options
* {CaptureVideoOptions} options for capturing video
*/
Capture.prototype.captureVideo = function(successCallback, errorCallback, options) {
var limit = 1,
duration = 0,
mode = null;
if (options) {
if (typeof options.limit === 'number' && options.limit > limit) {
limit = options.limit;
}
if (typeof options.duration === 'number' && options.duration > 0) {
duration = options.duration;
}
if (options.mode) {
mode = options.mode;
}
}
PhoneGap.exec(
function(mediaFiles) {
successCallback(getMediaFiles(mediaFiles));
},
function(error) {
Capture.onError(error, errorCallback);
},
'MediaCapture',
'captureVideo',
[limit, duration, mode]);
};
/**
* Launch audio recorder application and start an operation to record audio
* clip(s).
* param
* @param successCallback
* invoked with a list of MediaFile objects containing captured
* audio file properties
* @param errorCallback
* invoked with a CaptureError if capture is unsuccessful
* @param options
* {CaptureAudioOptions} options for capturing audio
*/
Capture.prototype.captureAudio = function(successCallback, errorCallback, options) {
var limit = 1,
duration = 0,
mode = null;
if (options) {
if (typeof options.limit === 'number' && options.limit > limit) {
limit = options.limit;
}
if (typeof options.duration === 'number' && options.duration > 0) {
duration = options.duration;
}
if (options.mode) {
mode = options.mode;
}
}
PhoneGap.exec(
function(mediaFiles) {
successCallback(getMediaFiles(mediaFiles));
},
function(error) {
Capture.onError(error, errorCallback);
},
'MediaCapture',
'captureAudio',
[limit, duration, mode]);
};
/**
* Cancels all pending capture operations.
*/
Capture.prototype.cancelCaptures = function() {
PhoneGap.exec(null, null, 'MediaCapture', 'stopCaptures', []);
}
/**
* Define navigator.device.capture object.
*/
PhoneGap.addConstructor(function() {
PhoneGap.waitForInitialization(captureId);
//No way to guarantee order of constructors so just incase this goes first
if (typeof navigator.device === 'undefined') {
navigator.device = {};
}
navigator.device.capture = new Capture();
});
}());