-
Notifications
You must be signed in to change notification settings - Fork 453
Expand file tree
/
Copy pathFileAPI.Camera.js
More file actions
248 lines (198 loc) · 4.57 KB
/
FileAPI.Camera.js
File metadata and controls
248 lines (198 loc) · 4.57 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
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
/**
* @class FileAPI.Camera
* @author RubaXa <trash@rubaxa.org>
* @support Chrome 21+, FF 18+, Opera 12+
*/
/*global window, FileAPI, jQuery */
/** @namespace LocalMediaStream -- https://developer.mozilla.org/en-US/docs/WebRTC/MediaStream_API#LocalMediaStream */
(function (window, api){
"use strict";
var
URL = window.URL || window.webkitURL,
document = window.document,
navigator = window.navigator,
getMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia,
html5 = !!getMedia
;
// Support "media"
api.support.media = html5;
var Camera = function (video){
this.video = video;
};
Camera.prototype = {
isActive: function (){
return !!this._active;
},
/**
* Start camera streaming
* @param {Function} callback
*/
start: function (callback){
var
_this = this
, video = _this.video
, _successId
, _failId
, _complete = function (err){
_this._active = !err;
clearTimeout(_failId);
clearTimeout(_successId);
// api.event.off(video, 'loadedmetadata', _complete);
callback && callback(err, _this);
}
;
getMedia.call(navigator, { video: true }, function (stream/**LocalMediaStream*/){
// Success
_this.stream = stream;
// api.event.on(video, 'loadedmetadata', function (){
// _complete(null);
// });
// Set camera stream
video.src = URL.createObjectURL(stream);
// Note: onloadedmetadata doesn't fire in Chrome when using it with getUserMedia.
// See crbug.com/110938.
_successId = setInterval(function (){
if( _detectVideoSignal(video) ){
_complete(null);
}
}, 1000);
_failId = setTimeout(function (){
_complete('timeout');
}, 5000);
// Go-go-go!
video.play();
}, _complete/*error*/);
},
/**
* Stop camera streaming
*/
stop: function (){
try {
this._active = false;
this.video.pause();
this.stream.stop();
} catch( err ){ }
},
/**
* Create screenshot
* @return {FileAPI.Camera.Shot}
*/
shot: function (){
return new Shot(this.video);
}
};
/**
* Get camera element from container
*
* @static
* @param {HTMLElement} el
* @return {Camera}
*/
Camera.get = function (el){
return new Camera(el.firstChild);
};
/**
* Publish camera element into container
*
* @static
* @param {HTMLElement} el
* @param {Object} options
* @param {Function} [callback]
*/
Camera.publish = function (el, options, callback){
if( typeof options == 'function' ){
callback = options;
options = {};
}
// Dimensions of "camera"
options = api.extend({}, {
width: '100%'
, height: '100%'
, start: true
}, options);
if( el.jquery ){
// Extract first element, from jQuery collection
el = el[0];
}
var doneFn = function (err){
if( err ){
callback(err);
}
else {
// Get camera
var cam = Camera.get(el);
if( options.start ){
cam.start(callback);
}
else {
callback(null, cam);
}
}
};
el.style.width = _px(options.width);
el.style.height = _px(options.height);
if( api.html5 && html5 ){
// Create video element
var video = document.createElement('video');
// Set dimensions
video.style.width = _px(options.width);
video.style.height = _px(options.height);
// Clean container
if( window.jQuery ){
jQuery(el).empty();
} else {
el.innerHTML = '';
}
// Add "camera" to container
el.appendChild(video);
// end
doneFn();
}
else {
Camera.fallback(el, options, doneFn);
}
};
Camera.fallback = function (el, options, callback){
callback('not_support_camera');
};
/**
* @class FileAPI.Camera.Shot
*/
var Shot = function (video){
var canvas = video.nodeName ? api.Image.toCanvas(video) : video;
var shot = api.Image(canvas);
shot.type = 'image/png';
shot.width = canvas.width;
shot.height = canvas.height;
shot.size = canvas.width * canvas.height * 4;
return shot;
};
/**
* Add "px" postfix, if value is a number
*
* @private
* @param {*} val
* @return {String}
*/
function _px(val){
return val >= 0 ? val + 'px' : val;
}
/**
* @private
* @param {HTMLVideoElement} video
* @return {Boolean}
*/
function _detectVideoSignal(video){
var canvas = document.createElement('canvas'), ctx, res = false;
try {
ctx = canvas.getContext('2d');
ctx.drawImage(video, 0, 0, 1, 1);
res = ctx.getImageData(0, 0, 1, 1).data[4] != 255;
}
catch( e ){}
return res;
}
// @export
Camera.Shot = Shot;
api.Camera = Camera;
})(window, FileAPI);