forked from smronju/vue-webcam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVueWebcam.js
executable file
·120 lines (104 loc) · 2.57 KB
/
VueWebcam.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
// TODO:
// 1. Enable mirror option
// 2. Improve options handling
// 3. Error handling
import Vue from 'vue';
const WebcamComponent = Vue.extend({
render: function (h) {
return h('video', {
ref: 'video',
attrs: {
width: this.width,
height: this.height,
src: this.src,
autoplay: this.autoplay
}
});
},
props: {
autoplay: {
type: Boolean,
default: true
},
width: {
type: Number,
default: 400
},
height: {
type: Number,
default: 300
},
mirror: {
type: Boolean,
default: true
},
screenshotFormat: {
type: String,
default: 'image/jpeg'
}
},
data () {
return {
video: '',
src: '',
stream: '',
hasUserMedia: false,
styleObject: {
transform: 'scale(-1, 1)',
filter: 'FlipH'
}
};
},
methods: {
getPhoto () {
if (!this.hasUserMedia) return null;
const canvas = this.getCanvas();
return canvas.toDataURL(this.screenshotFormat);
},
getCanvas () {
if (!this.hasUserMedia) return null;
const video = this.$refs.video;
if (!this.ctx) {
const canvas = document.createElement('canvas');
canvas.height = video.clientHeight;
canvas.width = video.clientWidth;
this.canvas = canvas;
this.ctx = canvas.getContext('2d');
/*if (this.mirror) {
const context = canvas.getContext('2d');
context.translate(canvas.width, 0);
context.scale(-1, 1);
this.ctx = context;
} else {
this.ctx = canvas.getContext('2d');
}*/
}
const { ctx, canvas } = this;
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
return canvas;
}
},
mounted: function () {
this.video = this.$refs.video;
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia || navigator.oGetUserMedia;
if (navigator.getUserMedia) {
navigator.getUserMedia({ video: true }, (stream) => {
this.src = window.URL.createObjectURL(stream);
this.stream = stream;
this.hasUserMedia = true;
}, (error) => {
console.log(error);
});
}
},
beforeDestroy: function () {
this.video.pause();
this.src = '';
this.stream.getTracks()[0].stop();
},
destroyed: function () {
console.log('Destroyed');
}
});
const VueWebcam = Vue.component('vue-webcam', WebcamComponent);
export default VueWebcam;