Skip to content

Commit

Permalink
Merge pull request #226 from LinusU/uint8array
Browse files Browse the repository at this point in the history
Avoid dependency on Buffer
  • Loading branch information
soldair authored May 4, 2020
2 parents eb2d499 + df2ba5a commit f829dd2
Show file tree
Hide file tree
Showing 80 changed files with 7,853 additions and 7,177 deletions.
21 changes: 3 additions & 18 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
language: node_js

dist: bionic
node_js:
- '4'
- '6'
- '8'
- '10.13.0'
- '10'
- '12'
- 'node'

env:
- CXX=g++-4.8

addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-4.8

before_install:
- sudo apt-get update
- sudo apt-get install -y libgif-dev
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ QRCode.toCanvas(canvas, 'sample text', function (error) {
```html
<canvas id="canvas"></canvas>

<script src="/build/qrcode.min.js"></script>
<script src="/build/qrcode.js"></script>
<script>
QRCode.toCanvas(document.getElementById('canvas'), 'sample text', function (error) {
if (error) console.error(error)
Expand All @@ -113,7 +113,9 @@ QRCode.toCanvas(canvas, 'sample text', function (error) {
</script>
```

If you install through `npm`, precompiled files will be available in `node_modules/qrcode/build/` folder.<br>
If you install through `npm`, precompiled files will be available in `node_modules/qrcode/build/` folder.

The precompiled bundle have support for [Internet Explorer 10+, Safari 5.1+, and all evergreen browsers](https://browserl.ist/?q=defaults%2C+IE+%3E%3D+10%2C+Safari+%3E%3D+5.1).

### NodeJS
Require the module `qrcode`
Expand Down Expand Up @@ -347,7 +349,7 @@ QRCode.toFile(
)
```

TypeScript users: if you are using [@types/qrcode](https://www.npmjs.com/package/@types/qrcode), you will need to add a `// @ts-ignore` above the data segment because it expects `data: string`.
TypeScript users: if you are using [@types/qrcode](https://www.npmjs.com/package/@types/qrcode), you will need to add a `// @ts-ignore` above the data segment because it expects `data: string`.

## Multibyte characters
Support for multibyte characters isn't present in the initial QR Code standard, but is possible to encode UTF-8 characters in Byte mode.
Expand Down
85 changes: 0 additions & 85 deletions build.js

This file was deleted.

2 changes: 1 addition & 1 deletion examples/cli.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var QRCode = require('../lib')
const QRCode = require('../lib')

QRCode.toString('yo yo yo', function (error, data) {
if (error) {
Expand Down
137 changes: 64 additions & 73 deletions examples/clientsideserver.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
var express = require('express')
var app = express()// .createServer()
var http = require('http')
var fs = require('fs')
var QRCode = require('../lib')
var canvasutil = require('canvasutil')
var Canvas = require('canvas')
var Image = Canvas.Image

var path = require('path')

// app.use(express.methodOverride())
// app.use(express.bodyParser())
// app.use(app.router)
// app.use(express.static(path.resolve(__dirname, '..')))
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')

// 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')
Expand All @@ -30,12 +29,12 @@ app.get('/', function (req, res) {
})
})

var effectHandlers = {}
const effectHandlers = {}

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

var effect = q.effect || 'plain'
let effect = q.effect || 'plain'
if (!effectHandlers[effect]) {
effect = 'plain'
}
Expand All @@ -49,7 +48,7 @@ app.get('/generate', function (req, res) {
}
})
} else {
var msg = error.message + '\n' + error.stack
const msg = error.message + '\n' + error.stack
res.header('Content-Type', 'text/plain')
res.send(msg)
console.error(msg)
Expand All @@ -73,28 +72,28 @@ effectHandlers.bacon = function (args, cb) {
}

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

var tpx = new canvasutil.PixelCore()
var luma709Only = canvasutil.conversionLib.luma709Only
var up = []
var down = []
var left = []
var right = []
var upPx
var downPx
var leftPx
var rightPx
var r
var t
var l
var d
var corner = 0
const tpx = new canvasutil.PixelCore()
const luma709Only = canvasutil.conversionLib.luma709Only
const up = []
const down = []
const left = []
const right = []
let upPx
let downPx
let leftPx
let rightPx
let r
let t
let l
let d
let corner = 0

tpx.threshold = 100

Expand Down Expand Up @@ -162,21 +161,21 @@ effectHandlers.rounded = function (args, cb) {
}
}
})
cb(false, canvas)
cb(null, canvas)
})
}

effectHandlers.remoteImage = function (args, cb) {
var src = args.src
var domain
var uri
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()
var parts = src.split('/')
const parts = src.split('/')

domain = parts.shift()
uri = parts.join('/')
Expand All @@ -188,29 +187,29 @@ effectHandlers.remoteImage = function (args, cb) {
return
}

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

var req = http.request(options, function (res) {
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')

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

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

res.on('error', function (error) {
Expand All @@ -223,31 +222,28 @@ effectHandlers.remoteImage = function (args, cb) {
}

effectHandlers.image = function (args, cb) {
var src = args.src || ''

var img = new Image()
var convert = canvasutil.conversionLib
img.onload = function () {
var canvas = new Canvas(200, 200)
loadImage(args.src || '').then((img) => {
const convert = canvasutil.conversionLib
const canvas = createCanvas(200, 200)
QRCode.toCanvas(canvas, args.text || '', function (err) {
if (err) {
cb(err, false)
return
}

var codeCtx = canvas.getContext('2d')
var frame = codeCtx.getImageData(0, 0, canvas.width, canvas.width)
var tpx = new canvasutil.PixelCore()
var baconCanvas = new Canvas(canvas.width, canvas.width)
var ctx = baconCanvas.getContext('2d')
var topThreshold = args.darkThreshold || 25
var bottomThreshold = args.lightThreshold || 75
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
var w = canvas.width
var h = canvas.height
let w = canvas.width
let h = canvas.height

if (img.width > img.height) {
w = w * (canvas.height / h)
Expand All @@ -260,10 +256,10 @@ effectHandlers.image = function (args, cb) {

try {
tpx.iterate(baconCanvas, function (px, i, l, pixels, w, h, pixelCore) {
var luma = (0.2125 * px.r + 0.7154 * px.g + 0.0721 * px.b)
var codeLuma = convert.luma709Only(frame.data[i * 4], frame.data[i * 4 + 1], frame.data[i * 4 + 2])
var yuv
var rgb
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) {
Expand Down Expand Up @@ -292,21 +288,16 @@ effectHandlers.image = function (args, cb) {
cb(err, false)
}

cb(false, baconCanvas)
cb(null, baconCanvas)
})
}

img.onerror = function (error) {
error.message += ' (' + src + ')'
}, (error) => {
cb(error, null)
}

img.src = src
})
}

effectHandlers.plain = function (args, cb) {
var canvas = new Canvas(200, 200)
var text = args.text || ''
const canvas = createCanvas(200, 200)
const text = args.text || ''
QRCode.toCanvas(canvas, text || '', function (err) {
cb(err, canvas)
})
Expand Down
Loading

0 comments on commit f829dd2

Please sign in to comment.