-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera-capture.html
126 lines (108 loc) · 3.8 KB
/
camera-capture.html
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
<link rel="import" href="../polymer/polymer.html">
<!--
Element for capturing camera stream and use it to take pictures.
##### Example
<camera-capture autocapture></camera-capture>
##### Example with js
<button id="start">Start capturing</button>
<button id="stop">Stop capturing</button>
<video src="" id="video" autoplay></video>
<camera-capture id="camera" autocapture></camera-capture>
<script>
var camera = document.querySelector('#camera'),
start = document.querySelector('#start'),
stop = document.querySelector('#stop'),
video = document.querySelector('#video');
camera.addEventListener('start', function (evt) {
video.src = evt.detail;
});
start.addEventListener('click', camera.start.bind(camera));
stop.addEventListener('click', camera.stop.bind(camera));
</script>
@element camera-capture
@blurb Element for capturing camera stream and use it to take pictures.
@status alpha
@homepage http://artoale.com/camera-capture
-->
<polymer-element name="camera-capture" attributes="out autocapture">
<script>
(function() {
'use strict';
Polymer({
isStreaming: false,
created: function() {
this.autocapture = false;
},
ready: function() {
if (this.autocapture) {
this.start();
}
},
/**
* The `autocapture` attribute specifies if camera capturing should
* start automatically or not.
*
* @attribute autocapture
* @type boolean
* @default false
*/
/**
* The `start` event is fired when, after having permission granted by the user,
* camera caputuring begins.
*
* @event start
* @param {string} detail The objectURL blob of the current stream.
*
*/
/**
* The `error` event is fired en error occurs, e.g. if the user doesn't
* grant permission to use the camera.
*
* @event error
* @param {Object} detail The error occurred when trying to capture
*
*/
/**
* Start capturing from the camera
*
* @method start
* @return {boolean} false if camera was already capturing
*/
start: function() {
if (this.isStreaming) {
return false;
}
navigator.getMedia = (navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia);
navigator.getMedia({
video: true,
audio: false
},
function(stream) {
this._stream = stream;
this.isStreaming = true;
this.out = window.URL.createObjectURL(stream);
this.fire('start', this.out);
}.bind(this),
function(err) {
this.fire('error', err);
}.bind(this)
);
return true;
},
/**
* Stop the camera from streaming
*
* @method stop
* @return {boolean} The camera stream to interrupt
*/
stop: function() {
this.isStreaming = false;
this._stream.stop();
}
});
}());
</script>
</polymer-element>