-
Notifications
You must be signed in to change notification settings - Fork 1
/
API.Camera.js
267 lines (231 loc) · 8.14 KB
/
API.Camera.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
/**
* Copyright (c) 2014 by Center Open Middleware. All Rights Reserved.
* Titanium Appcelerator 3.3.0GA
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
"use strict";
/** Response indicating the operation status and result
* @typedef CameraOptions
* @property {String} [autohide=true] Specifies if the camera should be hidden automatically after the media capture is completed.
* @property {String} [cancel] Function to call if the user presses the cancel button.
* @property {String} [error] Function to call upon receiving an error.
* @property {String} [saveToPhotoGallery=false] Specifies if the media should be saved to the photo gallery upon successful capture.
* @property {String} [success] Function to call when the camera is closed after a successful capture/selection.
* @property {String} [type="PHOTO"] "PHOTO" or "VIDEO" */
/** Response indicating the operation status and result
* @typedef ResponseCallback
* @property {String} status It should be "SUCCESS" or "FAILURE"
* @property {String} data It should be the resulted data. */
/**
* @callback ShowCameraCallback
* @param {ResponseCallback} response */
/* FYI: http://docs.appcelerator.com/titanium/3.0/#!/api/Titanium.Media*/
var Camera = function (APIReferences) {
var Yaast = {
API: APIReferences
};
var checkOptions = function checkOptions(options) {
if (!options) {
throw new TypeError();
}
var isVoidObject = options instanceof Object &&
Object.keys(options).length === 0;
if (isVoidObject) {
return;
}
var option;
var acceptedOptions = [
"autohide",
"cancel",
"error",
"saveToPhotoGallery",
"success",
"type"
];
var typeIsValid = options.hasOwnProperty('type') &&
(options.type === "PHOTO" || options.type === "VIDEO");
if (!typeIsValid) {
throw {
name: "ParameterError",
message: "'type' option is not valid. It should be 'PHOTO' or 'VIDEO'"
};
}
// only acceptedOptions can be present.
var isAccepted = function isAccepted(option) {
return acceptedOptions.some(function (element, index, array) {
return element === option;
});
};
for (option in options) {
if (!isAccepted(option)) {
throw {
name: "ParameterError",
message: "An option is not valid"
};
}
}
};
var setDefaultOptions = function setDefaultOptions (options) {
var setOption = function setOption (name, value) {
if (!options.hasOwnProperty(name)) {
options[name] = value;
}
};
setOption("autohide", true);
setOption("saveToPhotoGallery", false);
setOption("type", "PHOTO");
};
var showCameraAndroidPhoto = function showCameraAndroidPhoto(callback, options) {
var showCameraOptions = {
success: function (e) {
callback({
status: "SUCCESS",
data: Ti.Utils.base64encode(e.media).toString()
});
},
error: function (e) {
callback({
status: "ERROR"
});
},
cancel: function (e) {
callback({
status: "CANCEL"
});
},
allowEditing: false,
autoHide: options.autoHide,
saveToPhotoGallery: options.saveToPhotoGallery,
mediaTypes: [Ti.Media.MEDIA_TYPE_PHOTO]
};
Ti.Media.showCamera(showCameraOptions);
};
var showCameraAndroidVideo = function showCameraAndroidVideo(callback, options) {
var intent = Titanium.Android.createIntent({
action: 'android.media.action.VIDEO_CAPTURE'
});
Titanium.Android.currentActivity.startActivityForResult(intent, function(e) {
if (e.error) {
callback({
status: "ERROR",
data: null
});
} else if (e.resultCode === Titanium.Android.RESULT_OK) {
var videoUri = e.intent.data;
callback({
status: "SUCCESS",
data: videoUri
});
} else {
callback({
status: "CANCEL",
data: null
});
}
});
};
var showCameraAndroid = function showCameraAndroid(callback, options) {
if (options.type === "PHOTO") {
showCameraAndroidPhoto(callback, options);
} else {
showCameraAndroidVideo(callback, options);
}
};
var showCameraIos = function showCameraIos (callback, options) {
var showCameraOptions = {
success: function (e) {
callback({
status: "SUCCESS",
data: Ti.Utils.base64encode(e.media).toString()
});
},
error: function (e) {
callback({
status: "ERROR"
});
},
cancel: function (e) {
callback({
status: "CANCEL"
});
},
allowEditing: false,
autoHide: options.autoHide,
saveToPhotoGallery: options.saveToPhotoGallery
};
if (options.type === "PHOTO"){
showCameraOptions.mediaTypes = [Ti.Media.MEDIA_TYPE_PHOTO];
} else {
showCameraOptions.mediaTypes = [Ti.Media.MEDIA_TYPE_VIDEO];
showCameraOptions.videoMaximumDuration = 10000;
showCameraOptions.videoQuality = Titanium.Media.QUALITY_HIGH;
}
Ti.Media.showCamera(showCameraOptions);
};
/** It returns the result of Ti.Media native call.
* @private
* @param {String} funcName : The function name.
* @return Object : Native result. */
var returnFunction = function returnFunction(funcName){
var result;
try {
result = Ti.Media[funcName]();
} catch (e) {
e.details = {
method: funcName,
failure: "Native call failed"
};
throw e;
}
return result;
};
/** It allows to take pictures from native camera.
* @version 1.0.0
* @alias API.Camera
* @namespace */
var self = {};
/** Gets the value of the availableCameras property.
* @method
* @return {Number[]} : CAMERA_FRONT, CAMERA_REAR or both*/
self.getAvailableCameras = function getAvailableCameras() {
return returnFunction("getAvailableCameras");
};
/** Gets the value of the isCameraSupported property.
* @method
* @return {Boolean} */
self.isCameraSupported = function isCameraSupported() {
return returnFunction("getIsCameraSupported");
};
/** Shows the native camera controls. A photo can be taken and it will be
* returned in the first parameter of the callback.
* @method
* @param {ShowCameraCallback} callback
* @param {CameraOptions} [options] Additional options that should be passed as
* parameter of native call */
self.showCamera = function showCamera(callback, options) {
try {
checkOptions(options);
} catch (e) {
e.details = {
method: "showCamera",
failure: "'options' parameter is wrong"
};
throw e;
}
setDefaultOptions(options);
if (Yaast.API.HW.System.isApple()) {
showCameraIos(callback, options);
} else {
showCameraAndroid(callback, options);
}
};
/** Hide the native camera application
* @method
* */
self.hideCamera = function hideCamera() {
Titanium.Media.hideCamera();
};
return self;
};
module.exports = Camera;