index.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import extend from 'extend'
  2. import {
  3. QRCode,
  4. QRErrorCorrectLevel
  5. } from './qrcode'
  6. // support Chinese
  7. function utf16to8 (str) {
  8. var out, i, len, c
  9. out = ''
  10. len = str.length
  11. for (i = 0; i < len; i++) {
  12. c = str.charCodeAt(i)
  13. if ((c >= 0x0001) && (c <= 0x007F)) {
  14. out += str.charAt(i)
  15. } else if (c > 0x07FF) {
  16. out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F))
  17. out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F))
  18. out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F))
  19. } else {
  20. out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F))
  21. out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F))
  22. }
  23. }
  24. return out
  25. }
  26. function drawQrcode (options) {
  27. options = options || {}
  28. options = extend(true, {
  29. width: 256,
  30. height: 256,
  31. x: 0,
  32. y: 0,
  33. typeNumber: -1,
  34. correctLevel: QRErrorCorrectLevel.H,
  35. background: '#ffffff',
  36. foreground: '#000000',
  37. image: {
  38. imageResource: '',
  39. dx: 0,
  40. dy: 0,
  41. dWidth: 100,
  42. dHeight: 100
  43. }
  44. }, options)
  45. if (!options.canvasId && !options.ctx) {
  46. console.warn('please set canvasId or ctx!')
  47. return
  48. }
  49. createCanvas()
  50. function createCanvas () {
  51. // create the qrcode itself
  52. var qrcode = new QRCode(options.typeNumber, options.correctLevel)
  53. qrcode.addData(utf16to8(options.text))
  54. qrcode.make()
  55. // get canvas context
  56. var ctx
  57. if (options.ctx) {
  58. ctx = options.ctx
  59. } else {
  60. ctx = options._this ? wx.createCanvasContext && wx.createCanvasContext(options.canvasId, options._this) : wx.createCanvasContext && wx.createCanvasContext(options.canvasId)
  61. }
  62. // compute tileW/tileH based on options.width/options.height
  63. var tileW = options.width / qrcode.getModuleCount()
  64. var tileH = options.height / qrcode.getModuleCount()
  65. // draw in the canvas
  66. for (var row = 0; row < qrcode.getModuleCount(); row++) {
  67. for (var col = 0; col < qrcode.getModuleCount(); col++) {
  68. var style = qrcode.isDark(row, col) ? options.foreground : options.background
  69. ctx.setFillStyle(style)
  70. var w = (Math.ceil((col + 1) * tileW) - Math.floor(col * tileW))
  71. var h = (Math.ceil((row + 1) * tileW) - Math.floor(row * tileW))
  72. ctx.fillRect(Math.round(col * tileW) + options.x, Math.round(row * tileH) + options.y, w, h)
  73. }
  74. }
  75. if (options.image.imageResource) {
  76. ctx.drawImage(options.image.imageResource, options.image.dx, options.image.dy, options.image.dWidth, options.image.dHeight)
  77. }
  78. ctx.draw(false, function (e) {
  79. options.callback && options.callback(e)
  80. })
  81. }
  82. }
  83. export default drawQrcode