Add barcode-detector-api-polyfill dependency and update environment variable declarations in SvelteKit project
This commit is contained in:
+47
@@ -0,0 +1,47 @@
|
||||
import { EncodeHintType } from '@zxing/library';
|
||||
declare abstract class BrowserCodeSvgWriter {
|
||||
/**
|
||||
* Default quiet zone in pixels.
|
||||
*/
|
||||
private static readonly QUIET_ZONE_SIZE;
|
||||
/**
|
||||
* SVG markup NameSpace
|
||||
*/
|
||||
private static readonly SVG_NS;
|
||||
/**
|
||||
* A HTML container element for the image.
|
||||
*/
|
||||
private containerElement;
|
||||
/**
|
||||
* Constructs. 😉
|
||||
*/
|
||||
constructor(containerElement: string | HTMLElement);
|
||||
/**
|
||||
* Writes the QR code to a SVG and renders it in the container.
|
||||
*/
|
||||
write(contents: string, width: number, height: number, hints?: Map<EncodeHintType, any>): SVGSVGElement;
|
||||
/**
|
||||
* Creates a SVG element.
|
||||
*/
|
||||
protected createSVGElement(w: number, h: number): SVGSVGElement;
|
||||
/**
|
||||
* Creates a SVG rect.
|
||||
*/
|
||||
protected createSvgPathPlaceholderElement(w: number, h: number): SVGPathElement;
|
||||
/**
|
||||
* Creates a SVG rect.
|
||||
*/
|
||||
protected createSvgRectElement(x: number, y: number, w: number, h: number): SVGRectElement;
|
||||
/**
|
||||
* Encodes the content to a Barcode type.
|
||||
*/
|
||||
private encode;
|
||||
/**
|
||||
* Renders the SVG in the container.
|
||||
*
|
||||
* @note the input matrix uses 0 == white, 1 == black. The output matrix
|
||||
* uses 0 == black, 255 == white (i.e. an 8 bit greyscale bitmap).
|
||||
*/
|
||||
private renderResult;
|
||||
}
|
||||
export { BrowserCodeSvgWriter };
|
||||
+131
@@ -0,0 +1,131 @@
|
||||
import { EncodeHintType, IllegalArgumentException, IllegalStateException, QRCodeDecoderErrorCorrectionLevel, QRCodeEncoder, } from '@zxing/library';
|
||||
const svgNs = 'http://www.w3.org/2000/svg';
|
||||
/**/
|
||||
class BrowserCodeSvgWriter {
|
||||
/**
|
||||
* Constructs. 😉
|
||||
*/
|
||||
constructor(containerElement) {
|
||||
if (typeof containerElement === 'string') {
|
||||
const container = document.getElementById(containerElement);
|
||||
if (!container) {
|
||||
throw new Error(`Could not find a Container element with '${containerElement}'.`);
|
||||
}
|
||||
this.containerElement = container;
|
||||
}
|
||||
else {
|
||||
this.containerElement = containerElement;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Writes the QR code to a SVG and renders it in the container.
|
||||
*/
|
||||
write(contents, width, height, hints) {
|
||||
if (contents.length === 0) {
|
||||
throw new IllegalArgumentException('Found empty contents');
|
||||
}
|
||||
if (width < 0 || height < 0) {
|
||||
throw new IllegalArgumentException('Requested dimensions are too small: ' + width + 'x' + height);
|
||||
}
|
||||
const quietZone = hints && hints.get(EncodeHintType.MARGIN) !== undefined
|
||||
? Number.parseInt(hints.get(EncodeHintType.MARGIN).toString(), 10)
|
||||
: BrowserCodeSvgWriter.QUIET_ZONE_SIZE;
|
||||
const code = this.encode(hints, contents);
|
||||
return this.renderResult(code, width, height, quietZone);
|
||||
}
|
||||
/**
|
||||
* Creates a SVG element.
|
||||
*/
|
||||
createSVGElement(w, h) {
|
||||
const el = document.createElementNS(BrowserCodeSvgWriter.SVG_NS, 'svg');
|
||||
el.setAttributeNS(svgNs, 'width', h.toString());
|
||||
el.setAttributeNS(svgNs, 'height', w.toString());
|
||||
return el;
|
||||
}
|
||||
/**
|
||||
* Creates a SVG rect.
|
||||
*/
|
||||
createSvgPathPlaceholderElement(w, h) {
|
||||
const el = document.createElementNS(BrowserCodeSvgWriter.SVG_NS, 'path');
|
||||
el.setAttributeNS(svgNs, 'd', `M0 0h${w}v${h}H0z`);
|
||||
el.setAttributeNS(svgNs, 'fill', 'none');
|
||||
return el;
|
||||
}
|
||||
/**
|
||||
* Creates a SVG rect.
|
||||
*/
|
||||
createSvgRectElement(x, y, w, h) {
|
||||
const el = document.createElementNS(BrowserCodeSvgWriter.SVG_NS, 'rect');
|
||||
el.setAttributeNS(svgNs, 'x', x.toString());
|
||||
el.setAttributeNS(svgNs, 'y', y.toString());
|
||||
el.setAttributeNS(svgNs, 'height', w.toString());
|
||||
el.setAttributeNS(svgNs, 'width', h.toString());
|
||||
el.setAttributeNS(svgNs, 'fill', '#000000');
|
||||
return el;
|
||||
}
|
||||
/**
|
||||
* Encodes the content to a Barcode type.
|
||||
*/
|
||||
encode(hints, contents) {
|
||||
let errorCorrectionLevel = QRCodeDecoderErrorCorrectionLevel.L;
|
||||
if (hints && hints.get(EncodeHintType.ERROR_CORRECTION) !== undefined) {
|
||||
const correctionStr = hints.get(EncodeHintType.ERROR_CORRECTION).toString();
|
||||
errorCorrectionLevel = QRCodeDecoderErrorCorrectionLevel.fromString(correctionStr);
|
||||
}
|
||||
const code = QRCodeEncoder.encode(contents, errorCorrectionLevel, hints);
|
||||
return code;
|
||||
}
|
||||
/**
|
||||
* Renders the SVG in the container.
|
||||
*
|
||||
* @note the input matrix uses 0 == white, 1 == black. The output matrix
|
||||
* uses 0 == black, 255 == white (i.e. an 8 bit greyscale bitmap).
|
||||
*/
|
||||
renderResult(code, width /*int*/, height /*int*/, quietZone /*int*/) {
|
||||
// if (this.format && format != this.format) {
|
||||
// throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format)
|
||||
// }
|
||||
const input = code.getMatrix();
|
||||
if (input === null) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
const inputWidth = input.getWidth();
|
||||
const inputHeight = input.getHeight();
|
||||
const qrWidth = inputWidth + (quietZone * 2);
|
||||
const qrHeight = inputHeight + (quietZone * 2);
|
||||
const outputWidth = Math.max(width, qrWidth);
|
||||
const outputHeight = Math.max(height, qrHeight);
|
||||
const multiple = Math.min(Math.floor(outputWidth / qrWidth), Math.floor(outputHeight / qrHeight));
|
||||
// Padding includes both the quiet zone and the extra white pixels to accommodate the requested
|
||||
// dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone.
|
||||
// If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will
|
||||
// handle all the padding from 100x100 (the actual QR) up to 200x160.
|
||||
const leftPadding = Math.floor((outputWidth - (inputWidth * multiple)) / 2);
|
||||
const topPadding = Math.floor((outputHeight - (inputHeight * multiple)) / 2);
|
||||
const svgElement = this.createSVGElement(outputWidth, outputHeight);
|
||||
const placeholder = this.createSvgPathPlaceholderElement(width, height);
|
||||
svgElement.appendChild(placeholder);
|
||||
this.containerElement.appendChild(svgElement);
|
||||
// 2D loop
|
||||
for (let inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple) {
|
||||
// Write the contents of this row of the barcode
|
||||
for (let inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) {
|
||||
if (input.get(inputX, inputY) === 1) {
|
||||
const svgRectElement = this.createSvgRectElement(outputX, outputY, multiple, multiple);
|
||||
svgElement.appendChild(svgRectElement);
|
||||
}
|
||||
}
|
||||
}
|
||||
return svgElement;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Default quiet zone in pixels.
|
||||
*/
|
||||
BrowserCodeSvgWriter.QUIET_ZONE_SIZE = 4;
|
||||
/**
|
||||
* SVG markup NameSpace
|
||||
*/
|
||||
BrowserCodeSvgWriter.SVG_NS = 'http://www.w3.org/2000/svg';
|
||||
export { BrowserCodeSvgWriter };
|
||||
//# sourceMappingURL=BrowserCodeSvgWriter.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"BrowserCodeSvgWriter.js","sourceRoot":"","sources":["../../../src/writers/BrowserCodeSvgWriter.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,wBAAwB,EACxB,qBAAqB,EACrB,iCAAiC,EACjC,aAAa,GAEd,MAAM,gBAAgB,CAAC;AAExB,MAAM,KAAK,GAAG,4BAA4B,CAAC;AAE3C,IAAI;AACJ,MAAe,oBAAoB;IAiBjC;;OAEG;IACH,YAAmB,gBAAsC;QACvD,IAAI,OAAO,gBAAgB,KAAK,QAAQ,EAAE;YACxC,MAAM,SAAS,GAAG,QAAQ,CAAC,cAAc,CAAC,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UAAU,CAAC;IACpB,CAAC;;AA7KD;;GAEG;AACqB,oCAAe,GAAG,CAAC,CAAC;AAE5C;;GAEG;AACqB,2BAAM,GAAG,4BAA4B,CAAC;AAwKhE,OAAO,EAAE,oBAAoB,EAAE,CAAC"}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
import { EncodeHintType } from '@zxing/library';
|
||||
declare class BrowserQRCodeSvgWriter {
|
||||
private static readonly QUIET_ZONE_SIZE;
|
||||
/**
|
||||
* Writes and renders a QRCode SVG element.
|
||||
*
|
||||
* @param contents
|
||||
* @param width
|
||||
* @param height
|
||||
* @param hints
|
||||
*/
|
||||
write(contents: string, width: number, height: number, hints?: Map<EncodeHintType, any>): SVGSVGElement;
|
||||
/**
|
||||
* Renders the result and then appends it to the DOM.
|
||||
*/
|
||||
writeToDom(containerElement: string | HTMLElement, contents: string, width: number, height: number, hints?: Map<EncodeHintType, any>): void;
|
||||
/**
|
||||
* Note that the input matrix uses 0 == white, 1 == black.
|
||||
* The output matrix uses 0 == black, 255 == white (i.e. an 8 bit greyscale bitmap).
|
||||
*/
|
||||
private renderResult;
|
||||
/**
|
||||
* Creates a SVG element.
|
||||
*
|
||||
* @param w SVG's width attribute
|
||||
* @param h SVG's height attribute
|
||||
*/
|
||||
private createSVGElement;
|
||||
/**
|
||||
* Creates a SVG rect element.
|
||||
*
|
||||
* @param x Element's x coordinate
|
||||
* @param y Element's y coordinate
|
||||
* @param w Element's width attribute
|
||||
* @param h Element's height attribute
|
||||
*/
|
||||
private createSvgRectElement;
|
||||
}
|
||||
export { BrowserQRCodeSvgWriter };
|
||||
+122
@@ -0,0 +1,122 @@
|
||||
import { EncodeHintType, IllegalArgumentException, IllegalStateException, QRCodeDecoderErrorCorrectionLevel, QRCodeEncoder, } from '@zxing/library';
|
||||
const svgNs = 'http://www.w3.org/2000/svg';
|
||||
/**/
|
||||
class BrowserQRCodeSvgWriter {
|
||||
/**
|
||||
* Writes and renders a QRCode SVG element.
|
||||
*
|
||||
* @param contents
|
||||
* @param width
|
||||
* @param height
|
||||
* @param hints
|
||||
*/
|
||||
write(contents, width, height, hints) {
|
||||
if (contents.length === 0) {
|
||||
throw new IllegalArgumentException('Found empty contents');
|
||||
}
|
||||
// if (format != BarcodeFormat.QR_CODE) {
|
||||
// throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format)
|
||||
// }
|
||||
if (width < 0 || height < 0) {
|
||||
throw new IllegalArgumentException('Requested dimensions are too small: ' + width + 'x' + height);
|
||||
}
|
||||
let errorCorrectionLevel = QRCodeDecoderErrorCorrectionLevel.L;
|
||||
let quietZone = BrowserQRCodeSvgWriter.QUIET_ZONE_SIZE;
|
||||
if (hints) {
|
||||
if (undefined !== hints.get(EncodeHintType.ERROR_CORRECTION)) {
|
||||
const correctionStr = hints.get(EncodeHintType.ERROR_CORRECTION).toString();
|
||||
errorCorrectionLevel = QRCodeDecoderErrorCorrectionLevel.fromString(correctionStr);
|
||||
}
|
||||
if (undefined !== hints.get(EncodeHintType.MARGIN)) {
|
||||
quietZone = Number.parseInt(hints.get(EncodeHintType.MARGIN).toString(), 10);
|
||||
}
|
||||
}
|
||||
const code = QRCodeEncoder.encode(contents, errorCorrectionLevel, hints);
|
||||
return this.renderResult(code, width, height, quietZone);
|
||||
}
|
||||
/**
|
||||
* Renders the result and then appends it to the DOM.
|
||||
*/
|
||||
writeToDom(containerElement, contents, width, height, hints) {
|
||||
if (typeof containerElement === 'string') {
|
||||
const targetEl = document.querySelector(containerElement);
|
||||
if (!targetEl) {
|
||||
throw new Error('Could no find the target HTML element.');
|
||||
}
|
||||
containerElement = targetEl;
|
||||
}
|
||||
const svgElement = this.write(contents, width, height, hints);
|
||||
if (containerElement instanceof HTMLElement) {
|
||||
containerElement.appendChild(svgElement);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Note that the input matrix uses 0 == white, 1 == black.
|
||||
* The output matrix uses 0 == black, 255 == white (i.e. an 8 bit greyscale bitmap).
|
||||
*/
|
||||
renderResult(code, width /*int*/, height /*int*/, quietZone /*int*/) {
|
||||
const input = code.getMatrix();
|
||||
if (input === null) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
const inputWidth = input.getWidth();
|
||||
const inputHeight = input.getHeight();
|
||||
const qrWidth = inputWidth + (quietZone * 2);
|
||||
const qrHeight = inputHeight + (quietZone * 2);
|
||||
const outputWidth = Math.max(width, qrWidth);
|
||||
const outputHeight = Math.max(height, qrHeight);
|
||||
const multiple = Math.min(Math.floor(outputWidth / qrWidth), Math.floor(outputHeight / qrHeight));
|
||||
// Padding includes both the quiet zone and the extra white pixels to accommodate the requested
|
||||
// dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone.
|
||||
// If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will
|
||||
// handle all the padding from 100x100 (the actual QR) up to 200x160.
|
||||
const leftPadding = Math.floor((outputWidth - (inputWidth * multiple)) / 2);
|
||||
const topPadding = Math.floor((outputHeight - (inputHeight * multiple)) / 2);
|
||||
const svgElement = this.createSVGElement(outputWidth, outputHeight);
|
||||
for (let inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple) {
|
||||
// Write the contents of this row of the barcode
|
||||
for (let inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) {
|
||||
if (input.get(inputX, inputY) === 1) {
|
||||
const svgRectElement = this.createSvgRectElement(outputX, outputY, multiple, multiple);
|
||||
svgElement.appendChild(svgRectElement);
|
||||
}
|
||||
}
|
||||
}
|
||||
return svgElement;
|
||||
}
|
||||
/**
|
||||
* Creates a SVG element.
|
||||
*
|
||||
* @param w SVG's width attribute
|
||||
* @param h SVG's height attribute
|
||||
*/
|
||||
createSVGElement(w, h) {
|
||||
const svgElement = document.createElementNS(svgNs, 'svg');
|
||||
const width = w.toString();
|
||||
const height = h.toString();
|
||||
svgElement.setAttribute('height', height);
|
||||
svgElement.setAttribute('width', width);
|
||||
svgElement.setAttribute('viewBox', "0 0 " + width + " " + height);
|
||||
return svgElement;
|
||||
}
|
||||
/**
|
||||
* Creates a SVG rect element.
|
||||
*
|
||||
* @param x Element's x coordinate
|
||||
* @param y Element's y coordinate
|
||||
* @param w Element's width attribute
|
||||
* @param h Element's height attribute
|
||||
*/
|
||||
createSvgRectElement(x, y, w, h) {
|
||||
const rect = document.createElementNS(svgNs, 'rect');
|
||||
rect.setAttribute('x', x.toString());
|
||||
rect.setAttribute('y', y.toString());
|
||||
rect.setAttribute('height', w.toString());
|
||||
rect.setAttribute('width', h.toString());
|
||||
rect.setAttribute('fill', '#000000');
|
||||
return rect;
|
||||
}
|
||||
}
|
||||
BrowserQRCodeSvgWriter.QUIET_ZONE_SIZE = 4;
|
||||
export { BrowserQRCodeSvgWriter };
|
||||
//# sourceMappingURL=BrowserQRCodeSvgWriter.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"BrowserQRCodeSvgWriter.js","sourceRoot":"","sources":["../../../src/writers/BrowserQRCodeSvgWriter.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,wBAAwB,EACxB,qBAAqB,EACrB,iCAAiC,EACjC,aAAa,GAEd,MAAM,gBAAgB,CAAC;AAExB,MAAM,KAAK,GAAG,4BAA4B,CAAC;AAE3C,IAAI;AACJ,MAAM,sBAAsB;IAI1B;;;;;;;OAOG;IACI,KAAK,CACV,QAAgB,EAChB,KAAa,EACb,MAAc,EACd,KAAgC;QAGhC,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC,EAAE;YACzB,MAAM,IAAI,wBAAwB,CAAC,sBAAsB,CAAC,CAAC;SAC5D;QAED,yCAAyC;QACzC,qFAAqF;QACrF,IAAI;QAEJ,IAAI,KAAK,GAAG,CAAC,IAAI,MAAM,GAAG,CAAC,EAAE;YAC3B,MAAM,IAAI,wBAAwB,CAAC,sCAAsC,GAAG,KAAK,GAAG,GAAG,GAAG,MAAM,CAAC,CAAC;SACnG;QAED,IAAI,oBAAoB,GAAG,iCAAiC,CAAC,CAAC,CAAC;Q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