Last active
January 16, 2017 20:39
-
-
Save iamareebjamal/39f56078326d7b2ee6b35bd90def85b5 to your computer and use it in GitHub Desktop.
Basic Node Server to capture images and display in multipart using boundary
This file contains 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
/* Node Server | |
* Captures image and displays them using multipart and boundary headers | |
* 16th Jan 2016 | |
* Author : Areeb Jamal, Rahul Jha | |
*/ | |
var fs = require('fs'), | |
http = require('http'), | |
express = require('express'), | |
NodeWebcam = require("node-webcam"); | |
var opts = { | |
width: 1280, | |
height: 720, | |
delay: 0, | |
quality: 100, | |
output: "jpeg", | |
verbose: false | |
} | |
var Webcam = NodeWebcam.create(opts); | |
app = express(); | |
var server = http.createServer(app); | |
app.use(express.static('app')); | |
app.get('/', function(req, res) { | |
res.writeHead(200, { | |
'Content-Type': 'multipart/x-mixed-replace; boundary=myboundary' | |
}); | |
var capture = function() { | |
Webcam.capture("my_picture"); | |
fs.readFile('./' + 'my_picture.jpg', function(err, content) { | |
if (err) { | |
console.log(err); | |
return; | |
} | |
res.write("--myboundary\r\n"); | |
res.write("Content-Type: image/jpeg\r\n"); | |
res.write("\r\n"); | |
res.write(content, 'binary'); | |
res.write("\r\n"); | |
}); | |
setTimeout(capture, 100); | |
}; | |
capture(); | |
}); | |
var port = process.env.PORT || 8080; | |
server.listen(port, function() { | |
console.log('Listening on ' + port); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Found a better alternative
https://gist.github.com/RJ722/403c92a86d82c95ad0ff71f4ad0be997