This repository has been archived by the owner on Feb 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read.js
72 lines (65 loc) · 1.62 KB
/
read.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
var RaspiCam = require("raspicam");
var RpiLeds = require('rpi-leds');
var leds = new RpiLeds();
var fs = require("fs")
var jsqr = require('jsqr')
var Canvas = require('canvas')
var canvas = new Canvas(1000,1000)
var context = canvas.getContext('2d')
var Image = Canvas.Image
var camera = null;
leds.status.reset();
leds.status.blink();
console.log("Setting up the camera...")
camera = new RaspiCam({
mode: "photo",
output: __dirname + '/pics/cam.jpg',
rot: 0
});
camera.on("start", function(){
console.log("Started taking picture...")
});
camera.start()
/**
* Delete whatever local copy may exist.
*/
function deleteLocalPicture() {
fs.unlink(__dirname + '/pics/cam.jpg', function (err) {
if (err)
{
console.log('Error while trying to delete: ' + err)
}
else
{
console.log('Successfully deleted previous picture.');
}
});
}
/**
* Camera finished taking a picture. Upload and delete local copy.
*/
camera.on("exit", function(){
console.log("Done taking picture... Reading for QRs...")
fs.readFile(__dirname + '/pics/cam.jpg', function(err, qr) {
if (err) throw err;
var img = new Image;
img.src = qr
context.drawImage(img, 0, 0, img.width, img.height)
var imageData = context.getImageData(0,0,1000,1000)
console.log("Decoding QR code...")
var decode = jsqr.decodeQRFromImage(imageData.data, imageData.width, imageData.height)
if (decode)
{
leds.status.heartbeat();
console.log(decode)
deleteLocalPicture()
camera.start()
}
else
{
console.log("No QR code was found!")
deleteLocalPicture()
camera.start()
}
})
});