@lamassu/v4l2camera
Version:
Capturing images from USB (UVC) cameras on linux machines
74 lines (69 loc) • 2.1 kB
JavaScript
var http = require("http");
var pngjs = require("pngjs");
var v4l2camera = require("../");
var server = http.createServer(function (req, res) {
//console.log(req.url);
if (req.url === "/") {
res.writeHead(200, {
"content-type": "text/html;charset=utf-8",
});
res.end([
"<!doctype html>",
"<html><head><meta charset='utf-8'/>",
"<script>(", script.toString(), ")()</script>",
"</head><body>",
"<img id='cam' width='352' height='288' />",
"</body></html>",
].join(""));
return;
}
if (req.url.match(/^\/.+\.png$/)) {
res.writeHead(200, {
"content-type": "image/png",
"cache-control": "no-cache",
});
var png = toPng();
return png.pack().pipe(res);
}
});
server.listen(3000);
var script = function () {
window.addEventListener("load", function (ev) {
var cam = document.getElementById("cam");
(function load() {
var img = new Image();
img.addEventListener("load", function loaded(ev) {
cam.parentNode.replaceChild(img, cam);
img.id = "cam";
cam = img;
load();
}, false);
img.src = "/" + Date.now() + ".png";
})();
}, false);
};
var toPng = function () {
var rgb = cam.toRGB();
var png = new pngjs.PNG({
width: cam.width, height: cam.height,
deflateLevel: 1, deflateStrategy: 1,
});
var size = cam.width * cam.height;
for (var i = 0; i < size; i++) {
png.data[i * 4 + 0] = rgb[i * 3 + 0];
png.data[i * 4 + 1] = rgb[i * 3 + 1];
png.data[i * 4 + 2] = rgb[i * 3 + 2];
png.data[i * 4 + 3] = 255;
}
return png;
};
var cam = new v4l2camera.Camera("/dev/video0")
if (cam.configGet().formatName !== "YUYV") {
console.log("YUYV camera required");
process.exit(1);
}
cam.configSet({width: 352, height: 288});
cam.start();
cam.capture(function loop() {
cam.capture(loop);
});