import 'babel-polyfill'; import jsQR from 'jsqr'; /* eslint-disable no-underscore-dangle */ class QrcodeDecoder { /** * Constructor for QrcodeDecoder */ constructor() { this.timerCapture = null; this.canvasElem = null; this.gCtx = null; this.stream = null; this.videoElem = null; this.getUserMediaHandler = null; this.videoConstraints = { video: true, audio: false }; this.defaultOption = { inversionAttempts: 'attemptBoth' }; } /** * Verifies if canvas element is supported. */ /* eslint-disable class-methods-use-this */ isCanvasSupported() { const elem = document.createElement('canvas'); return !!(elem.getContext && elem.getContext('2d')); } /** * draw lint * * @param {object} begin line begin point * @param {object} end line end point * @param {string} color color string */ // _drawLine(begin, end, color) { // this.gCtx.beginPath(); // this.gCtx.moveTo(begin.x, begin.y); // this.gCtx.lineTo(end.x, end.y); // this.gCtx.lineWidth = 4; // this.gCtx.strokeStyle = color; // this.gCtx.stroke(); // } /** * create qrcode marker * * @param {object} code jsqr parse code object */ // _createQrcodeMark(code) { // this._drawLine( // code.location.topLeftCorner, // code.location.topRightCorner, // '#FF3B58', // ); // this._drawLine( // code.location.topRightCorner, // code.location.bottomRightCorner, // '#FF3B58', // ); // this._drawLine( // code.location.bottomRightCorner, // code.location.bottomLeftCorner, // '#FF3B58', // ); // this._drawLine( // code.location.bottomLeftCorner, // code.location.topLeftCorner, // '#FF3B58', // ); // } _createImageData(target, width, height) { if (!this.canvasElem) { this._prepareCanvas(width, height); } this.gCtx.clearRect(0, 0, width, height); this.gCtx.drawImage(target, 0, 0, width, height); const imageData = this.gCtx.getImageData( 0, 0, this.canvasElem.width, this.canvasElem.height, ); return imageData; } /** * Prepares the canvas element (which will * receive the image from the camera and provide * what the algorithm needs for checking for a * QRCode and then decoding it.) * * * @param {DOMElement} canvasElem the canvas * element * @param {number} width The width that * the canvas element * should have * @param {number} height The height that * the canvas element * should have * @return {DOMElement} the canvas * after the resize if width and height * provided. */ _prepareCanvas(width, height) { if (!this.canvasElem) { this.canvasElem = document.createElement('canvas'); this.canvasElem.style.width = `${width}px`; this.canvasElem.style.height = `${height}px`; this.canvasElem.width = width; this.canvasElem.height = height; } this.gCtx = this.canvasElem.getContext('2d'); } /** * Based on the video dimensions and the canvas * that was previously generated captures the * video/image source and then paints into the * canvas so that the decoder is able to work as * it expects. * @param {DOMElement} videoElem