Blame view

public/assets/addons/fastflow/plugins/node-qrcode-master/lib/renderer/png.js 1.73 KB
2db47680   hexiaodong   chore: 初始提交 php 遗...
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
73
74
75
76
77
78
  const fs = require('fs')
  const PNG = require('pngjs').PNG
  const Utils = require('./utils')
  
  exports.render = function render (qrData, options) {
    const opts = Utils.getOptions(options)
    const pngOpts = opts.rendererOpts
    const size = Utils.getImageWidth(qrData.modules.size, opts)
  
    pngOpts.width = size
    pngOpts.height = size
  
    const pngImage = new PNG(pngOpts)
    Utils.qrToImageData(pngImage.data, qrData, opts)
  
    return pngImage
  }
  
  exports.renderToDataURL = function renderToDataURL (qrData, options, cb) {
    if (typeof cb === 'undefined') {
      cb = options
      options = undefined
    }
  
    exports.renderToBuffer(qrData, options, function (err, output) {
      if (err) cb(err)
      let url = 'data:image/png;base64,'
      url += output.toString('base64')
      cb(null, url)
    })
  }
  
  exports.renderToBuffer = function renderToBuffer (qrData, options, cb) {
    if (typeof cb === 'undefined') {
      cb = options
      options = undefined
    }
  
    const png = exports.render(qrData, options)
    const buffer = []
  
    png.on('error', cb)
  
    png.on('data', function (data) {
      buffer.push(data)
    })
  
    png.on('end', function () {
      cb(null, Buffer.concat(buffer))
    })
  
    png.pack()
  }
  
  exports.renderToFile = function renderToFile (path, qrData, options, cb) {
    if (typeof cb === 'undefined') {
      cb = options
      options = undefined
    }
  
    let called = false
    const done = (...args) => {
      if (called) return
      called = true
      cb.apply(null, args)
    }
    const stream = fs.createWriteStream(path)
  
    stream.on('error', done)
    stream.on('close', done)
  
    exports.renderToFileStream(stream, qrData, options)
  }
  
  exports.renderToFileStream = function renderToFileStream (stream, qrData, options) {
    const png = exports.render(qrData, options)
    png.pack().pipe(stream)
  }