ソースを参照

Some improvements

Johannes Zellner 6 年 前
親
コミット
8d72e118c4
4 ファイル変更、55 行追加、6 行削除
  1. 23 2
      index.js
  2. 14 0
      package-lock.json
  3. 1 0
      package.json
  4. 17 4
      public/index.html

+ 23 - 2
index.js

@@ -5,18 +5,21 @@
 var express = require('express'),
     path = require('path'),
     fs = require('fs'),
+    cors = require('cors'),
     NodeWebcam = require('node-webcam');
 
 var opts = {
-    width: 800,
-    height: 600,
     quality: 100,
     saveShots: false,
     output: 'jpeg',
     device: false,
+    setValues: {
+        '"White Balance Temperature, Auto"': true
+    },
     // device: '/dev/video2',
     // [location, buffer, base64]
     callbackReturn: 'location',
+    verbose: true
 };
 
 //Creates webcam instance
@@ -33,6 +36,8 @@ try {
     if (e.code !== 'EEXIST') console.log(e);
 }
 
+app.use(cors());
+
 app.get('/pictures', function (req, res, next) {
     var entries = fs.readdirSync(picturePath);
     console.log(entries);
@@ -40,6 +45,22 @@ app.get('/pictures', function (req, res, next) {
     res.status(200).send({ pictures: entries.sort() });
 });
 
+app.get('/picture', function (req, res, next) {
+    var tmp = JSON.parse(JSON.stringify(opts));
+    tmp.callbackReturn = 'buffer';
+
+    console.log(req.query)
+
+    NodeWebcam.capture('/tmp/test', tmp, function (error, data) {
+        if (error) {
+            console.error(error);
+            return res.status(500).end();
+        }
+
+        res.status(200).send(data);
+    });
+});
+
 app.use(express.static('public'));
 
 function capture() {

+ 14 - 0
package-lock.json

@@ -68,6 +68,15 @@
       "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz",
       "integrity": "sha1-4wOogrNCzD7oylE6eZmXNNqzriw="
     },
+    "cors": {
+      "version": "2.8.5",
+      "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz",
+      "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==",
+      "requires": {
+        "object-assign": "^4",
+        "vary": "^1"
+      }
+    },
     "debug": {
       "version": "2.6.9",
       "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
@@ -257,6 +266,11 @@
         "osenv": "^0.1.4"
       }
     },
+    "object-assign": {
+      "version": "4.1.1",
+      "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",
+      "integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM="
+    },
     "on-finished": {
       "version": "2.3.0",
       "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz",

+ 1 - 0
package.json

@@ -9,6 +9,7 @@
   "author": "",
   "license": "ISC",
   "dependencies": {
+    "cors": "^2.8.5",
     "express": "^4.17.1",
     "node-webcam": "^0.6.0"
   }

+ 17 - 4
public/index.html

@@ -8,13 +8,22 @@ var i = 0;
 var pictures = [];
 var interval = null;
 
+var origin = '';
+// var origin = 'http://growcam';
+
 function reload() {
+    if (pictures.length === 0) return;
+
     if (i >= pictures.length) i = 0;
 
-    document.getElementById('picture').src = '/pictures/' + pictures[i++];
+    document.getElementById('stream').src = origin + '/pictures/' + pictures[i++];
     document.getElementById('currentPictureLabel').innerText = pictures[i++];
 }
 
+function takePicture() {
+    document.getElementById('picture').src = origin + '/picture?' + Date.now();
+}
+
 function getPictureList() {
     var req = new XMLHttpRequest();
     req.addEventListener('load', function () {
@@ -22,7 +31,7 @@ function getPictureList() {
 
         document.getElementById('pictureCount').innerText = pictures.length;
     });
-    req.open('GET', '/pictures');
+    req.open('GET', origin + '/pictures');
     req.send();
 }
 
@@ -31,7 +40,7 @@ function pausePlay() {
         clearInterval(interval);
         interval = null;
     } else {
-        interval = setInterval(reload, 100);
+        interval = setInterval(reload, 500);
     }
 }
 
@@ -48,7 +57,11 @@ window.onload = function () {
 <body>
 
     <p>Pictures available: <span id="pictureCount"></span> showing <span id="currentPictureLabel"></span></p>
-    <img id="picture" width="800" height="600" onclick="pausePlay()"/>
+    <img id="stream" width="800" onclick="pausePlay()"/>
+
+    <br/>
+
+    <img id="picture" width="400" height="400" onclick="takePicture()"/>
 
 </body>
 </html>