BrowserQRCodeSvgWriter.js 5.7 KB

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