首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当node.js运行时,摄像头不会显示

当node.js运行时,摄像头不会显示
EN

Stack Overflow用户
提问于 2016-06-02 23:36:26
回答 1查看 501关注 0票数 0

我有一个脚本可以从我的摄像头上拍到一张照片。当我在本地运行或在在线服务器上看到时,它运行得很好。

但是当我从node.js运行html文件时,它不会显示来自我的摄像头的视图。

怎么解决这个问题?

节点中的我的服务器:

代码语言:javascript
运行
复制
// 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:

代码语言:javascript
运行
复制
    <!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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-02 23:48:58

您的程序似乎在为发送的内容--每个请求(用于html、图标、脚本等)。也许可以尝试使用express来正确地服务静态文件:

代码语言:javascript
运行
复制
var express = require('express');
var app = express();
app.use('/', express.static(__dirname));

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

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

另外,您可能希望将静态文件(html、css、js)移动到类似于static的不同目录中,并按以下方式更改app.js

代码语言:javascript
运行
复制
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代码。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37603778

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档