BrowserQRCodeSvgWriter.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import EncodeHintType from '../core/EncodeHintType';
  2. import Encoder from '../core/qrcode/encoder/Encoder';
  3. import ErrorCorrectionLevel from '../core/qrcode/decoder/ErrorCorrectionLevel';
  4. import IllegalArgumentException from '../core/IllegalArgumentException';
  5. import IllegalStateException from '../core/IllegalStateException';
  6. /**
  7. * @deprecated Moving to @zxing/browser
  8. */
  9. class BrowserQRCodeSvgWriter {
  10. /**
  11. * Writes and renders a QRCode SVG element.
  12. *
  13. * @param contents
  14. * @param width
  15. * @param height
  16. * @param hints
  17. */
  18. write(contents, width, height, hints = null) {
  19. if (contents.length === 0) {
  20. throw new IllegalArgumentException('Found empty contents');
  21. }
  22. // if (format != BarcodeFormat.QR_CODE) {
  23. // throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format)
  24. // }
  25. if (width < 0 || height < 0) {
  26. throw new IllegalArgumentException('Requested dimensions are too small: ' + width + 'x' + height);
  27. }
  28. let errorCorrectionLevel = ErrorCorrectionLevel.L;
  29. let quietZone = BrowserQRCodeSvgWriter.QUIET_ZONE_SIZE;
  30. if (hints !== null) {
  31. if (undefined !== hints.get(EncodeHintType.ERROR_CORRECTION)) {
  32. errorCorrectionLevel = ErrorCorrectionLevel.fromString(hints.get(EncodeHintType.ERROR_CORRECTION).toString());
  33. }
  34. if (undefined !== hints.get(EncodeHintType.MARGIN)) {
  35. quietZone = Number.parseInt(hints.get(EncodeHintType.MARGIN).toString(), 10);
  36. }
  37. }
  38. const code = Encoder.encode(contents, errorCorrectionLevel, hints);
  39. return this.renderResult(code, width, height, quietZone);
  40. }
  41. /**
  42. * Renders the result and then appends it to the DOM.
  43. */
  44. writeToDom(containerElement, contents, width, height, hints = null) {
  45. if (typeof containerElement === 'string') {
  46. containerElement = document.querySelector(containerElement);
  47. }
  48. const svgElement = this.write(contents, width, height, hints);
  49. if (containerElement)
  50. containerElement.appendChild(svgElement);
  51. }
  52. /**
  53. * Note that the input matrix uses 0 == white, 1 == black.
  54. * The output matrix uses 0 == black, 255 == white (i.e. an 8 bit greyscale bitmap).
  55. */
  56. renderResult(code, width /*int*/, height /*int*/, quietZone /*int*/) {
  57. const input = code.getMatrix();
  58. if (input === null) {
  59. throw new IllegalStateException();
  60. }
  61. const inputWidth = input.getWidth();
  62. const inputHeight = input.getHeight();
  63. const qrWidth = inputWidth + (quietZone * 2);
  64. const qrHeight = inputHeight + (quietZone * 2);
  65. const outputWidth = Math.max(width, qrWidth);
  66. const outputHeight = Math.max(height, qrHeight);
  67. const multiple = Math.min(Math.floor(outputWidth / qrWidth), Math.floor(outputHeight / qrHeight));
  68. // Padding includes both the quiet zone and the extra white pixels to accommodate the requested
  69. // dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone.
  70. // If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will
  71. // handle all the padding from 100x100 (the actual QR) up to 200x160.
  72. const leftPadding = Math.floor((outputWidth - (inputWidth * multiple)) / 2);
  73. const topPadding = Math.floor((outputHeight - (inputHeight * multiple)) / 2);
  74. const svgElement = this.createSVGElement(outputWidth, outputHeight);
  75. for (let inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple) {
  76. // Write the contents of this row of the barcode
  77. for (let inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) {
  78. if (input.get(inputX, inputY) === 1) {
  79. const svgRectElement = this.createSvgRectElement(outputX, outputY, multiple, multiple);
  80. svgElement.appendChild(svgRectElement);
  81. }
  82. }
  83. }
  84. return svgElement;
  85. }
  86. /**
  87. * Creates a SVG element.
  88. *
  89. * @param w SVG's width attribute
  90. * @param h SVG's height attribute
  91. */
  92. createSVGElement(w, h) {
  93. const svgElement = document.createElementNS(BrowserQRCodeSvgWriter.SVG_NS, 'svg');
  94. svgElement.setAttributeNS(null, 'height', w.toString());
  95. svgElement.setAttributeNS(null, 'width', h.toString());
  96. return svgElement;
  97. }
  98. /**
  99. * Creates a SVG rect element.
  100. *
  101. * @param x Element's x coordinate
  102. * @param y Element's y coordinate
  103. * @param w Element's width attribute
  104. * @param h Element's height attribute
  105. */
  106. createSvgRectElement(x, y, w, h) {
  107. const rect = document.createElementNS(BrowserQRCodeSvgWriter.SVG_NS, 'rect');
  108. rect.setAttributeNS(null, 'x', x.toString());
  109. rect.setAttributeNS(null, 'y', y.toString());
  110. rect.setAttributeNS(null, 'height', w.toString());
  111. rect.setAttributeNS(null, 'width', h.toString());
  112. rect.setAttributeNS(null, 'fill', '#000000');
  113. return rect;
  114. }
  115. }
  116. BrowserQRCodeSvgWriter.QUIET_ZONE_SIZE = 4;
  117. /**
  118. * SVG markup NameSpace
  119. */
  120. BrowserQRCodeSvgWriter.SVG_NS = 'http://www.w3.org/2000/svg';
  121. export { BrowserQRCodeSvgWriter };