Created
October 12, 2017 22:58
-
-
Save snobu/b564932e3bfccd69ae689d2366ddfeba to your computer and use it in GitHub Desktop.
Capture canvas frame to byte stream for Face API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function captureVideoFrame(video, format) { | |
| if (typeof video === 'string') { | |
| video = document.getElementById(video); | |
| } | |
| format = format || 'jpeg'; | |
| 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); | |
| 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, bytes: bytes, arr: arr }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment