Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions examples/cli.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
const QRCode = require('../lib')

import * as QRCode from '../lib/index.js'
QRCode.toString('yo yo yo', function (error, data) {
if (error) {
throw new Error(error)
}

console.log(data)
})
67 changes: 10 additions & 57 deletions examples/clientsideserver.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,36 @@
const express = require('express')
const app = express()// .createServer()
const http = require('http')
const fs = require('fs')
const QRCode = require('../lib')
const canvasutil = require('canvasutil')
const { createCanvas, loadImage } = require('canvas')

const path = require('path')

import express from 'express'
import http from 'http'
import fs from 'fs'
import * as QRCode from '../lib/index.js'
import canvasutil from 'canvasutil'
import canvas from 'canvas'
import path from 'path'
const app = express() // .createServer()
const { createCanvas, loadImage } = canvas
// app.use(express.methodOverride())
// app.use(express.bodyParser())
// app.use(app.router)
// app.use(express.static(path.resolve(__dirname, '..')))

app.get('/qrcode.js', (req, res) => {
res.set('content-type', 'text/javascript')
fs.createReadStream(path.join(__dirname, '..', 'build', 'qrcode.js')).pipe(res)
})

app.get('/qrcode.tosjis.js', (req, res) => {
res.set('content-type', 'text/javascript')
fs.createReadStream(path.join(__dirname, '..', 'build', 'qrcode.tosjis.js')).pipe(res)
})

app.get('/', function (req, res) {
fs.readFile(path.join(__dirname, 'clientside.html'), function (err, data) {
res.send(data ? data.toString() : err)
})
})

const effectHandlers = {}

app.get('/generate', function (req, res) {
const q = req.query || {}

let effect = q.effect || 'plain'
if (!effectHandlers[effect]) {
effect = 'plain'
}

effectHandlers[effect](q, function (error, canvas) {
if (!error) {
canvas.toBuffer(function (err, buf) {
Expand All @@ -55,30 +47,25 @@ app.get('/generate', function (req, res) {
}
})
})

effectHandlers.node = function (args, cb) {
args.src = path.join(__dirname, 'images', 'node_logo.png')
this.image(path.join(args, cb))
}

effectHandlers.npm = function (args, cb) {
args.src = path.join(__dirname, 'images', 'npm_logo.png')
this.image(args, cb)
}

effectHandlers.bacon = function (args, cb) {
args.src = path.join(__dirname, 'images', 'bacon-love.png')
this.image(args, cb)
}

effectHandlers.rounded = function (args, cb) {
const canvas = createCanvas(200, 200)
QRCode.toCanvas(canvas, args.text || '', function (err) {
if (err) {
cb(err, canvas)
return
}

const tpx = new canvasutil.PixelCore()
const luma709Only = canvasutil.conversionLib.luma709Only
const up = []
Expand All @@ -94,12 +81,9 @@ effectHandlers.rounded = function (args, cb) {
let l
let d
let corner = 0

tpx.threshold = 100

tpx.iterate(canvas, function (px, i, len, pixels, w, h, pixelCore) {
corner = 0

// is dark
if (luma709Only(px.r, px.g, px.b) < pixelCore.threshold) {
if (i - w > 0) {
Expand All @@ -109,15 +93,13 @@ effectHandlers.rounded = function (args, cb) {
up[2] = pixels[upPx + 2]
// console.log('up',up);
}

if (i + w <= len) {
downPx = (i + w) * 4
down[0] = pixels[downPx + 0]
down[1] = pixels[downPx + 1]
down[2] = pixels[downPx + 2]
// console.log('down',down);
}

// have left pixel but no wrapping
if (i % w !== 0) {
leftPx = (i - 1) * 4
Expand All @@ -126,20 +108,17 @@ effectHandlers.rounded = function (args, cb) {
left[2] = pixels[leftPx + 2]
// console.log('left',left);
}

if (i % w !== w - 1) {
rightPx = (i + 1) * 4
right[0] = pixels[rightPx + 0]
right[1] = pixels[rightPx + 1]
right[2] = pixels[rightPx + 2]
// console.log('right',right);
}

r = rightPx ? luma709Only(right[0], right[1], right[2]) : 0
t = upPx ? luma709Only(up[0], up[1], up[2]) : 0
l = leftPx ? luma709Only(left[0], left[1], left[2]) : 0
d = downPx ? luma709Only(down[0], down[1], down[2]) : 0

if (l > pixelCore.threshold) { // if left is light and i am dark
if (t > pixelCore.threshold) { // if top is light and i am dark
corner = 1
Expand All @@ -155,7 +134,6 @@ effectHandlers.rounded = function (args, cb) {
corner = 1
}
}

if (corner) {
px.a = 50
}
Expand All @@ -164,63 +142,51 @@ effectHandlers.rounded = function (args, cb) {
cb(null, canvas)
})
}

effectHandlers.remoteImage = function (args, cb) {
let src = args.src
let domain
let uri

if (!src) {
cb(new Error('src required'), null)
} else {
if (src.indexof('://') !== -1) {
src = src.split('://').unshift()
const parts = src.split('/')

domain = parts.shift()
uri = parts.join('/')
}
}

if (!domain || !uri) {
cb(new Error('missing domain or uri ' + args.src))
return
}

const options = {
host: domain,
port: 80,
path: uri,
method: 'GET'
}

const req = http.request(options, function (res) {
if (res.statusCode < 200 || res.statusCode > 299) {
cb(new Error('http ' + res.statusCode + ' response code'), null)
return
}

res.setEncoding('utf8')

let data = ''
res.on('data', function (chunk) {
data += chunk
console.log('BODY: ' + chunk)
})

res.on('complete', function () {
cb(null, data)
})

res.on('error', function (error) {
cb(error, null)
cb = function () {}
cb = function () { }
})
})

req.end()
}

effectHandlers.image = function (args, cb) {
loadImage(args.src || '').then((img) => {
const convert = canvasutil.conversionLib
Expand All @@ -230,21 +196,17 @@ effectHandlers.image = function (args, cb) {
cb(err, false)
return
}

const codeCtx = canvas.getContext('2d')
const frame = codeCtx.getImageData(0, 0, canvas.width, canvas.width)
const tpx = new canvasutil.PixelCore()
const baconCanvas = createCanvas(canvas.width, canvas.width)
const ctx = baconCanvas.getContext('2d')
const topThreshold = args.darkThreshold || 25
const bottomThreshold = args.lightThreshold || 75

tpx.threshold = 50

// scale image
let w = canvas.width
let h = canvas.height

if (img.width > img.height) {
w = w * (canvas.height / h)
h = canvas.height
Expand All @@ -253,20 +215,16 @@ effectHandlers.image = function (args, cb) {
w = canvas.width
}
ctx.drawImage(img, 0, 0, w, h)

try {
tpx.iterate(baconCanvas, function (px, i, l, pixels, w, h, pixelCore) {
const luma = (0.2125 * px.r + 0.7154 * px.g + 0.0721 * px.b)
const codeLuma = convert.luma709Only(frame.data[i * 4], frame.data[i * 4 + 1], frame.data[i * 4 + 2])
let yuv
let rgb

if (codeLuma > pixelCore.threshold) {
if (luma < bottomThreshold) {
yuv = convert.rgbToYuv(px.r, px.g, px.b)

rgb = convert.yuvToRgb(bottomThreshold, yuv[1], yuv[2])

px.r = rgb[0]
px.g = rgb[1]
px.b = rgb[2]
Expand All @@ -275,9 +233,7 @@ effectHandlers.image = function (args, cb) {
} else {
if (luma > topThreshold) {
yuv = convert.rgbToYuv(px.r, px.g, px.b)

rgb = convert.yuvToRgb(topThreshold, yuv[1], yuv[2])

px.r = rgb[0]
px.g = rgb[1]
px.b = rgb[2]
Expand All @@ -287,21 +243,18 @@ effectHandlers.image = function (args, cb) {
} catch (e) {
cb(err, false)
}

cb(null, baconCanvas)
})
}, (error) => {
cb(error, null)
})
}

effectHandlers.plain = function (args, cb) {
const canvas = createCanvas(200, 200)
const text = args.text || ''
QRCode.toCanvas(canvas, text || '', function (err) {
cb(err, canvas)
})
}

app.listen(3031)
console.log('listening on 3031')
5 changes: 2 additions & 3 deletions examples/save.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
const QRCode = require('../lib')

import * as QRCode from '../lib/index.js'
const path = './tmp.png'
QRCode.toFile(path, 'life of the party bros', {
color: {
dark: '#00F', // Blue modules
light: '#0000' // Transparent background
}
}, function (err) {
if (err) throw err
if (err) { throw err }
console.log('saved.')
})
46 changes: 21 additions & 25 deletions examples/server.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,31 @@
const QRCode = require('../lib')
const http = require('http')

import * as QRCode from '../lib/index.js'
import http from 'http'
function testQRCode (req, res) {
res.writeHead(200, { 'Content-Type': 'text/html' })

const jungleBook = "The moonlight was blocked out of the mouth of the cave, for Shere Khan's\n" +
'great square head and shoulders were thrust into the entrance. Tabaqui,\n' +
'behind him, was squeaking: "My lord, my lord, it went in here!"\n' +
'\n' +
'"Shere Khan does us great honor," said Father Wolf, but his eyes were\n' +
'very angry. "What does Shere Khan need?"\n' +
'\n' +
"\"My quarry. A man's cub went this way,\" said Shere Khan. \"Its parents\n" +
'have run off. Give it to me."\n' +
'\n' +
"Shere Khan had jumped at a woodcutter's campfire, as Father Wolf had\n" +
'said, and was furious from the pain of his burned feet. But Father Wolf\n' +
'knew that the mouth of the cave was too narrow for a tiger to come in\n' +
"by. Even where he was, Shere Khan's shoulders and forepaws were cramped\n" +
"for want of room, as a man's would be if he tried to fight in a barrel.\n" +
'\n' +
'"The Wolves are a free people," said Father Wolf. "They take orders from\n' +
"the Head of the Pack, and not from any striped cattle-killer. The man's\n" +
'cub is ours--to kill if we choose."'

'great square head and shoulders were thrust into the entrance. Tabaqui,\n' +
'behind him, was squeaking: "My lord, my lord, it went in here!"\n' +
'\n' +
'"Shere Khan does us great honor," said Father Wolf, but his eyes were\n' +
'very angry. "What does Shere Khan need?"\n' +
'\n' +
"\"My quarry. A man's cub went this way,\" said Shere Khan. \"Its parents\n" +
'have run off. Give it to me."\n' +
'\n' +
"Shere Khan had jumped at a woodcutter's campfire, as Father Wolf had\n" +
'said, and was furious from the pain of his burned feet. But Father Wolf\n' +
'knew that the mouth of the cave was too narrow for a tiger to come in\n' +
"by. Even where he was, Shere Khan's shoulders and forepaws were cramped\n" +
"for want of room, as a man's would be if he tried to fight in a barrel.\n" +
'\n' +
'"The Wolves are a free people," said Father Wolf. "They take orders from\n' +
"the Head of the Pack, and not from any striped cattle-killer. The man's\n" +
'cub is ours--to kill if we choose."'
// QRCode.QRCodeDraw.color.dark = '#d4d4d4';
QRCode.toDataURL(jungleBook, function (err, url) {
if (err) console.log('error: ' + err)
if (err) { console.log('error: ' + err) }
res.end("<!DOCTYPE html/><html><head><title>node-qrcode</title></head><body><img src='" + url + "'/></body></html>")
})
}

http.createServer(testQRCode).listen(3030)
console.log('test server started on port 3030')
Loading