Простой код как обработать данные с jpegcam на node.js
var http = require('http'),
fs = require('fs'),
sys = require('sys');
http.createServer(function(req, res){
// Обработка запроса отправленного библиотекой
if(req.url == '/webcam'){
res.writeHead(200,{'Content-Type':'text/html'});
// Создаем поток для записи в файл
ws=fs.createWriteStream('/tmp/'+new Date()+'.jpg');
// Обрабатываем получаемые данные
req.on('data',function(d){
// Производим запись в поток
ws.write(d);
});
// Завершаем обработку
req.on('end',function(){
ws.destroySoon();
ws.on('close',function(){
fs.stat('/tmp/'+new Date()+'.jpg',function(e,s){
if(e) throw e;
res.end(sys.inspect(s));
});
});
});
// Если flash исходних библиотеки находиться на другом хосте пускаем его в моем случаи это i.localhost
}else if(req.url == '/crossdomain.xml'){
res.writeHead(200,{'Content-Type':'text/xml'});
var xml = '<?xml version="1.0"?>\r\n';
xml += '<cross-domain-policy>\r\n';
xml += '<allow-http-request-headers-from domain="i.localhost" headers="*"/>\r\n';
xml += '<allow-access-from domain="i.localhost"/>\r\n';
xml += '</cross-domain-policy>';
res.end(xml);
// Дефолтом выводим jpegcam
}else{
res.writeHead(200,{'Content-Type':'text/html'});
res.end('<script type="text/javascript" src="http://i.localhost/js/u/wc.js"></script>'+
'<script language="JavaScript">'+
'webcam.set_quality( 90 );'+
'webcam.set_shutter_sound( false );'+
'webcam.set_swf_url("http://i.localhost/i/wc.swf");'+
'webcam.set_api_url("/webcam");'+
'webcam.set_hook("onComplete", "my_callback_function");'+
'function my_callback_function(response) {'+
' alert(response);'+
'}'+
'document.write( webcam.get_html(320, 240) );'+
'</script>'+
'<a href="javascript:void(webcam.snap())">Фото</a>');
}
}).listen(3000);
console.log('Server started.\n');
Исходники на github'е https://github.com/MikleSol/jpegcam-node.js
В комментариях к коду все максимально описано.
Комментариев нет:
Отправить комментарий