BrowserQRCodeSvgWriter.js 6.0 KB

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