-
Notifications
You must be signed in to change notification settings - Fork 33
/
capture-video-frame.js
42 lines (33 loc) · 1.24 KB
/
capture-video-frame.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
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory);
} else {
root.captureVideoFrame = factory();
}
}(this, function () {
return function captureVideoFrame(video, format, quality) {
if (typeof video === 'string') {
video = document.getElementById(video);
}
format = format || 'jpeg';
quality = quality || 0.92;
if (!video || (format !== 'png' && format !== 'jpeg')) {
return false;
}
var canvas = document.createElement("CANVAS");
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
canvas.getContext('2d').drawImage(video, 0, 0);
var dataUri = canvas.toDataURL('image/' + format, quality);
var data = dataUri.split(',')[1];
var mimeType = dataUri.split(';')[0].slice(5)
var bytes = window.atob(data);
var buf = new ArrayBuffer(bytes.length);
var arr = new Uint8Array(buf);
for (var i = 0; i < bytes.length; i++) {
arr[i] = bytes.charCodeAt(i);
}
var blob = new Blob([ arr ], { type: mimeType });
return { blob: blob, dataUri: dataUri, format: format };
};
}));