Skip to content

Instantly share code, notes, and snippets.

@snobu
Created October 12, 2017 22:58
Show Gist options
  • Select an option

  • Save snobu/b564932e3bfccd69ae689d2366ddfeba to your computer and use it in GitHub Desktop.

Select an option

Save snobu/b564932e3bfccd69ae689d2366ddfeba to your computer and use it in GitHub Desktop.
Capture canvas frame to byte stream for Face API
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