网络摄像头在 node.js 运行时不显示 [英] Webcam doesn't display when node.js it's running

查看:36
本文介绍了网络摄像头在 node.js 运行时不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本可以从我的网络摄像头拍摄照片.当我在本地运行或在在线服务器中看到时,它工作正常.

I have a script that takes a picture from my webcam. it's working fine when i runs locally or when i see in a online server.

但是当我从 node.js 运行 html 文件时,它没有显示来自我的网络摄像头的视图.

But when i run the html file from the node.js, it doesnt show the view from my webcam.

如何解决这个问题?

节点中的我的服务器:

// app.js

var http = require('http');
var fs = require('fs');
sys = require('util');

var server = http.createServer(function(request, response){
  fs.readFile(__dirname + '/index.html', function(err, html){
    console.log("oi");
    response.writeHeader(200, {'Content-Type': 'text/html'});
    response.write(html);
    response.end();
  });
});

server.listen(3000, function(){
  console.log('Executando Servidor HTTP');
});

我的 HTML:

    <!DOCTYPE html>
<html>
<head>
<title>Javascript Webcam Demo - <MyCodingTricks/></title>
</head>
<body>

<h3>Demonstrates simple 320x240 capture &amp; display</h3>


<div id="my_camera"></div>

    <!-- A button for taking snaps -->

<form>
        <input type=button class="btn btn-success" value="Take Snapshot" onClick="take_snapshot()">
    </form>


<div id="results" class="well">Your captured image will appear here...</div>

    <!-- First, include the Webcam.js JavaScript Library -->
    <script type="text/javascript" src="2/webcam.min.js"></script>

    <!-- Configure a few settings and attach camera -->
    <script language="JavaScript">
        Webcam.set({
            width: 320,
            height: 240,
            image_format: 'jpeg',
            jpeg_quality: 90
        });
        Webcam.attach( '#my_camera' );
        function take_snapshot() {
            // take snapshot and get image data
            Webcam.snap( function(data_uri) {
                // display results in page
                document.getElementById('results').innerHTML =
                    '<h2>Here is your image:</h2>' +
                    '<img src="'+data_uri+'"/>';

                                Webcam.upload( data_uri, 'upload.php', function(code, text) {
                                            // Upload complete!
                                            // 'code' will be the HTTP response code from the server, e.g. 200
                                            // 'text' will be the raw response content
                                });
            } );
        }
    </script>
</body>
</html>

推荐答案

您的程序似乎正在为 每个 请求发送 index.html 的内容(对于 html、图标、脚本等).也许尝试使用 express 来正确提供静态文件:

Your program seems to be sending the content of index.html for every request (for html, icons, scripts, etc.). Maybe try using express to serve static files properly:

var express = require('express');
var app = express();
app.use('/', express.static(__dirname));

app.listen(3000, function(){
  console.log('Executando Servidor HTTP');
});

它甚至比您想要的编写方式更容易,因为要使您的脚本工作,您必须根据 request 对象手动路由请求以正确发送脚本和不同的资产,使确保处理 MIME 类型,路径如 ../.. 等.

It's even easier than the way you want to write it, because to make your script work you would have to manually route the requests based on the request object to properly send scripts and different assets, make sure to handle MIME types, paths like ../.. etc.

此外,您可能希望将静态文件(html、css、js)移动到不同的目录,例如 static 并像这样更改 app.js:

Also you may want to move your static files (html, css, js) to a different directory like static and change the app.js like this:

var express = require('express');
var app = express();
app.use('/', express.static(__dirname + '/static'));

app.listen(3000, function(){
  console.log('Executando Servidor HTTP');
});

这样就没有人能够通过浏览到:app.js 代码>http://localhost:3000/app.js

so that no one will be able to get your app.js code by browsing to: http://localhost:3000/app.js

这篇关于网络摄像头在 node.js 运行时不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆