-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathcamera.js
129 lines (119 loc) · 4.04 KB
/
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
// This file was adapted from infragram-js:
// http://github.com/p-v-o-s/infragram-js.
module.exports = function Camera(options) {
var canvas,
ctx;
// Initialize getUserMedia with options
function initialize() {
getUserMedia(webRtcOptions, success, deviceError);
// iOS Safari 11 compatibility: https://github.com/webrtc/adapter/issues/685
webRtcOptions.videoEl.setAttribute('autoplay', 'autoplay');
webRtcOptions.videoEl.setAttribute('playsinline', 'playsinline');
window.webcam = webRtcOptions; // this is weird but maybe used for flash fallback?
canvas = options.canvas || document.getElementById("image");
ctx = canvas.getContext("2d");
// Trigger a snapshot w/ button
// -- move this to interface.js?
$("#snapshot").show();
$("#live-video").show();
$("#webcam").show();
}
// webRtcOptions contains the configuration information for the shim
// it allows us to specify the width and height of the video
// output we"re working with, the location of the fallback swf,
// events that are triggered onCapture and onSave (for the fallback)
// and so on.
var webRtcOptions = options.webRtcOptions || {
"audio": false,
"video": true,
// the element (by id) you wish to use for
// displaying the stream from a camera
el: "webcam",
extern: null,
append: true,
// height and width of the output stream
// container
width: 640,
height: 480,
// the recommended mode to be used is
// "callback " where a callback is executed
// once data is available
mode: "callback",
// a debugger callback is available if needed
debug: function() {},
// callback for capturing the fallback stream
onCapture: function onWebRtcCapture() {
return window.webcam.save();
},
// callback for saving the stream, useful for
// relaying data further.
onSave: onSaveGetUserMedia,
onLoad: function onLoadGetUserMedia() {}
}
function onSaveGetUserMedia(data) {
var col, h, i, img, j, ref, tmp, w, pos = 0;
col = data.split("");
img = camera.image;
tmp = null;
w = webRtcOptions.width;
h = webRtcOptions.height;
for (i = j = 0, ref = w - 1; 0 <= ref ? j <= ref : j >= ref; i = 0 <= ref ? ++j : --j) {
tmp = parseInt(col[i], 10);
img.data[pos + 0] = (tmp >> 16) & 0xff;
img.data[pos + 1] = (tmp >> 8) & 0xff;
img.data[pos + 2] = tmp & 0xff;
img.data[pos + 3] = 0xff;
pos += 4;
}
if (pos >= 4 * w * h) {
ctx.putImageData(img, 0, 0);
return pos = 0;
}
}
function success(stream) {
var video;
if (webRtcOptions.context === "webrtc") {
video = webRtcOptions.videoEl;
if (navigator.mozGetUserMedia) {
video.mozSrcObject = stream;
} else {
video.srcObject = stream;
}
return video.onerror = function(e) {
return stream.stop();
}
} else {
}
}
function deviceError(error) {
alert("No camera available.");
console.log(error);
return console.error("An error occurred: [CODE " + error.code + "]");
}
// not doing anything now... for copying to a 2nd canvas
function getSnapshot() {
var video;
// If the current context is WebRTC/getUserMedia (something
// passed back from the shim to avoid doing further feature
// detection), we handle getting video/images for our canvas
// from our HTML5 <video> element.
if (webRtcOptions.context === "webrtc") {
video = document.getElementsByTagName("video")[0];
options.processor.updateImage(video);
return $("#webcam").hide();
// Otherwise, if the context is Flash, we ask the shim to
// directly call window.webcam, where our shim is located
// and ask it to capture for us.
} else if (webRtcOptions.context === "flash") {
return window.webcam.capture();
} else {
console.log("No context was supplied to getSnapshot()");
}
}
return {
getSnapshot: getSnapshot,
initialize: initialize,
onSaveGetUserMedia: onSaveGetUserMedia,
webRtcOptions: webRtcOptions
}
}