-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.html
185 lines (180 loc) · 6.91 KB
/
camera.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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>camera</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://code.getmdl.io/1.3.0/material.blue_grey-pink.min.css" />
<style type="text/css">
video.mirror {
transform: rotateY(180deg);
-webkit-transform:rotateY(180deg); /* Safari and Chrome */
-moz-transform:rotateY(180deg); /* Firefox */
}
</style>
</head>
<body>
<div id="the-app">
<div class="mdl-layout mdl-js-layout mdl-layout--fixed-header mdl-layout--no-drawer-button">
<header class="mdl-layout__header">
<div class="mdl-layout__header-row">
<span class="mdl-layout-title">Camera</span>
<div class="mdl-layout-spacer"></div>
<nav class="mdl-navigation mdl-layout--large-screen-only">
<a class="mdl-navigation__link" href="https://aaron-tay.github.io/">by aaron-tay</a>
</nav>
</div>
</header>
<main class="mdl-layout__content">
<div class="page-content">
<div class="mdl-grid">
<div class="mdl-cell mdl-cell--12-col">
<h3>
About
</h3>
<p>
This is a basic example of using the HTML5 Camera
<p>
I constructed is using a variety of tools such as
<a href="https://vuejs.org/" target="_blank">VueJS</a>,
<a href="https://getmdl.io" target="_blank">MDL</a>,
<a href="https://lodash.com" target="_blank">Lodash</a>,
<a href="https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia" target="_blank">MDN.doc getUserMedia</a> and
<a href="https://developer.mozilla.org/en/docs/Web/HTML/Element/video" target="_blank">MDN.doc video</a>
</p>
<p>
If you're interested in the source, inspect it, or visit my
<a href="https://github.com/aaron-tay/aaron-tay.github.io">
GitHub
</a>.
</p>
</div>
</div>
<div class="mdl-cell mdl-cell--12-col">
<label class="mdl-switch mdl-js-switch mdl-js-ripple-effect" for="debug-toggle">
<input type="checkbox" id="debug-toggle" class="mdl-switch__input" v-model="debugging">
<span class="mdl-switch__label">Debug Mode</span>
</label>
</div>
<div class="mdl-cell mdl-cell--12-col">
<button class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--accent" @click="startCamera">
start
</button>
<button class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect" @click="switchCameras" :disabled="numberOfCameras <= 1">
switch
</button>
<button class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--colored" @click="stopCamera">
stop
</button>
<video autoplay :class="{ 'mirror': facingMode === 'user' }"></video>
</div>
<div class="mdl-cell mdl-cell--12-col" v-show="debugging">
<div v-show="debugging">
{{ errors }} <br />
{{ mediaDevices.support }} <br />
{{ mediaDevices.devices }} <br />
</div>
</div>
</div>
</main>
</div>
</div>
<script src="https://code.getmdl.io/1.3.0/material.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.0/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
<script>
const FACING_MODES = {
USER: 'user',
ENVIRONMENT: 'environment',
}
const lodash = _;
const app = new Vue({
el: '#the-app',
data() {
return {
hasCamera: false,
localMediaStream: null,
debugging: false,
errors: {},
facingMode: FACING_MODES.USER,
mediaDevices : {},
};
},
created() {
const vm = this;
this.$set(this.mediaDevices, 'support', navigator.mediaDevices.getSupportedConstraints());
navigator.mediaDevices.enumerateDevices().then((devices) => {
vm.$set(vm.mediaDevices, 'devices', devices);
})
const canUseCamera = (!!navigator.mediaDevices.getUserMedia);
if (canUseCamera) {
this.hasCamera = true;
} else {
alert('getUserMedia() is not supported in your browser');
}
},
computed: {
isPlaying() {
return this.localMediaStream !== null;
},
isStopped() {
return this.localMediaStream === null;
},
numberOfCameras() {
if (lodash.isEmpty(this.mediaDevices)) { return 0; }
const cameras = lodash.filter(this.mediaDevices.devices, device => device.kind === 'videoinput');
return lodash.size(cameras);
},
},
methods: {
switchCameras() {
if (this.numberOfCameras <= 1) { return; }
this.stopCamera();
if (this.facingMode === FACING_MODES.USER) {
this.facingMode = FACING_MODES.ENVIRONMENT;
} else {
this.facingMode = FACING_MODES.USER;
}
this.startCamera();
},
stopCamera() {
const video = document.querySelector('video');
video.pause();
video.src = null;
this.localMediaStream.getTracks()[0].stop();
this.localMediaStream = null;
},
startCamera() {
const vm = this;
const mediaConstraints = {
video: {
width: {
ideal: 320,
},
height: {
ideal: 240,
},
facingMode: this.facingMode,
},
};
navigator.mediaDevices.getUserMedia(mediaConstraints).then((localMediaStream) => {
const video = document.querySelector('video');
video.src = window.URL.createObjectURL(localMediaStream);
vm.localMediaStream = localMediaStream;
// Note: onloadedmetadata doesn't fire in Chrome when using it with getUserMedia.
// See crbug.com/110938.
video.onloadedmetadata = (e) => {
// Ready to go. Do some stuff.
console.log(e);
vm.$set(vm, 'errors', e);
};
}).catch(vm.errorCallback);
},
},
});
</script>
</body>
</html>