UNPKG

react-qrcode-reader

Version:

[![npm version](https://badge.fury.io/js/react-qrcode-reader.svg)](https://badge.fury.io/js/react-qrcode-reader)

1,256 lines (1,222 loc) 303 kB
import require$$0 from 'react'; var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; function getDefaultExportFromCjs (x) { return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x; } var jsxRuntime = {exports: {}}; var reactJsxRuntime_production = {}; /** * @license React * react-jsx-runtime.production.js * * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ var hasRequiredReactJsxRuntime_production; function requireReactJsxRuntime_production () { if (hasRequiredReactJsxRuntime_production) return reactJsxRuntime_production; hasRequiredReactJsxRuntime_production = 1; var REACT_ELEMENT_TYPE = Symbol.for("react.transitional.element"), REACT_FRAGMENT_TYPE = Symbol.for("react.fragment"); function jsxProd(type, config, maybeKey) { var key = null; void 0 !== maybeKey && (key = "" + maybeKey); void 0 !== config.key && (key = "" + config.key); if ("key" in config) { maybeKey = {}; for (var propName in config) "key" !== propName && (maybeKey[propName] = config[propName]); } else maybeKey = config; config = maybeKey.ref; return { $$typeof: REACT_ELEMENT_TYPE, type: type, key: key, ref: void 0 !== config ? config : null, props: maybeKey }; } reactJsxRuntime_production.Fragment = REACT_FRAGMENT_TYPE; reactJsxRuntime_production.jsx = jsxProd; reactJsxRuntime_production.jsxs = jsxProd; return reactJsxRuntime_production; } var reactJsxRuntime_development = {}; /** * @license React * react-jsx-runtime.development.js * * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ var hasRequiredReactJsxRuntime_development; function requireReactJsxRuntime_development () { if (hasRequiredReactJsxRuntime_development) return reactJsxRuntime_development; hasRequiredReactJsxRuntime_development = 1; "production" !== process.env.NODE_ENV && (function () { function getComponentNameFromType(type) { if (null == type) return null; if ("function" === typeof type) return type.$$typeof === REACT_CLIENT_REFERENCE ? null : type.displayName || type.name || null; if ("string" === typeof type) return type; switch (type) { case REACT_FRAGMENT_TYPE: return "Fragment"; case REACT_PROFILER_TYPE: return "Profiler"; case REACT_STRICT_MODE_TYPE: return "StrictMode"; case REACT_SUSPENSE_TYPE: return "Suspense"; case REACT_SUSPENSE_LIST_TYPE: return "SuspenseList"; case REACT_ACTIVITY_TYPE: return "Activity"; } if ("object" === typeof type) switch ( ("number" === typeof type.tag && console.error( "Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue." ), type.$$typeof) ) { case REACT_PORTAL_TYPE: return "Portal"; case REACT_CONTEXT_TYPE: return (type.displayName || "Context") + ".Provider"; case REACT_CONSUMER_TYPE: return (type._context.displayName || "Context") + ".Consumer"; case REACT_FORWARD_REF_TYPE: var innerType = type.render; type = type.displayName; type || ((type = innerType.displayName || innerType.name || ""), (type = "" !== type ? "ForwardRef(" + type + ")" : "ForwardRef")); return type; case REACT_MEMO_TYPE: return ( (innerType = type.displayName || null), null !== innerType ? innerType : getComponentNameFromType(type.type) || "Memo" ); case REACT_LAZY_TYPE: innerType = type._payload; type = type._init; try { return getComponentNameFromType(type(innerType)); } catch (x) {} } return null; } function testStringCoercion(value) { return "" + value; } function checkKeyStringCoercion(value) { try { testStringCoercion(value); var JSCompiler_inline_result = !1; } catch (e) { JSCompiler_inline_result = true; } if (JSCompiler_inline_result) { JSCompiler_inline_result = console; var JSCompiler_temp_const = JSCompiler_inline_result.error; var JSCompiler_inline_result$jscomp$0 = ("function" === typeof Symbol && Symbol.toStringTag && value[Symbol.toStringTag]) || value.constructor.name || "Object"; JSCompiler_temp_const.call( JSCompiler_inline_result, "The provided key is an unsupported type %s. This value must be coerced to a string before using it here.", JSCompiler_inline_result$jscomp$0 ); return testStringCoercion(value); } } function getTaskName(type) { if (type === REACT_FRAGMENT_TYPE) return "<>"; if ( "object" === typeof type && null !== type && type.$$typeof === REACT_LAZY_TYPE ) return "<...>"; try { var name = getComponentNameFromType(type); return name ? "<" + name + ">" : "<...>"; } catch (x) { return "<...>"; } } function getOwner() { var dispatcher = ReactSharedInternals.A; return null === dispatcher ? null : dispatcher.getOwner(); } function UnknownOwner() { return Error("react-stack-top-frame"); } function hasValidKey(config) { if (hasOwnProperty.call(config, "key")) { var getter = Object.getOwnPropertyDescriptor(config, "key").get; if (getter && getter.isReactWarning) return false; } return void 0 !== config.key; } function defineKeyPropWarningGetter(props, displayName) { function warnAboutAccessingKey() { specialPropKeyWarningShown || ((specialPropKeyWarningShown = true), console.error( "%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://react.dev/link/special-props)", displayName )); } warnAboutAccessingKey.isReactWarning = true; Object.defineProperty(props, "key", { get: warnAboutAccessingKey, configurable: true }); } function elementRefGetterWithDeprecationWarning() { var componentName = getComponentNameFromType(this.type); didWarnAboutElementRef[componentName] || ((didWarnAboutElementRef[componentName] = true), console.error( "Accessing element.ref was removed in React 19. ref is now a regular prop. It will be removed from the JSX Element type in a future release." )); componentName = this.props.ref; return void 0 !== componentName ? componentName : null; } function ReactElement( type, key, self, source, owner, props, debugStack, debugTask ) { self = props.ref; type = { $$typeof: REACT_ELEMENT_TYPE, type: type, key: key, props: props, _owner: owner }; null !== (void 0 !== self ? self : null) ? Object.defineProperty(type, "ref", { enumerable: false, get: elementRefGetterWithDeprecationWarning }) : Object.defineProperty(type, "ref", { enumerable: false, value: null }); type._store = {}; Object.defineProperty(type._store, "validated", { configurable: false, enumerable: false, writable: true, value: 0 }); Object.defineProperty(type, "_debugInfo", { configurable: false, enumerable: false, writable: true, value: null }); Object.defineProperty(type, "_debugStack", { configurable: false, enumerable: false, writable: true, value: debugStack }); Object.defineProperty(type, "_debugTask", { configurable: false, enumerable: false, writable: true, value: debugTask }); Object.freeze && (Object.freeze(type.props), Object.freeze(type)); return type; } function jsxDEVImpl( type, config, maybeKey, isStaticChildren, source, self, debugStack, debugTask ) { var children = config.children; if (void 0 !== children) if (isStaticChildren) if (isArrayImpl(children)) { for ( isStaticChildren = 0; isStaticChildren < children.length; isStaticChildren++ ) validateChildKeys(children[isStaticChildren]); Object.freeze && Object.freeze(children); } else console.error( "React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead." ); else validateChildKeys(children); if (hasOwnProperty.call(config, "key")) { children = getComponentNameFromType(type); var keys = Object.keys(config).filter(function (k) { return "key" !== k; }); isStaticChildren = 0 < keys.length ? "{key: someKey, " + keys.join(": ..., ") + ": ...}" : "{key: someKey}"; didWarnAboutKeySpread[children + isStaticChildren] || ((keys = 0 < keys.length ? "{" + keys.join(": ..., ") + ": ...}" : "{}"), console.error( 'A props object containing a "key" prop is being spread into JSX:\n let props = %s;\n <%s {...props} />\nReact keys must be passed directly to JSX without using spread:\n let props = %s;\n <%s key={someKey} {...props} />', isStaticChildren, children, keys, children ), (didWarnAboutKeySpread[children + isStaticChildren] = true)); } children = null; void 0 !== maybeKey && (checkKeyStringCoercion(maybeKey), (children = "" + maybeKey)); hasValidKey(config) && (checkKeyStringCoercion(config.key), (children = "" + config.key)); if ("key" in config) { maybeKey = {}; for (var propName in config) "key" !== propName && (maybeKey[propName] = config[propName]); } else maybeKey = config; children && defineKeyPropWarningGetter( maybeKey, "function" === typeof type ? type.displayName || type.name || "Unknown" : type ); return ReactElement( type, children, self, source, getOwner(), maybeKey, debugStack, debugTask ); } function validateChildKeys(node) { "object" === typeof node && null !== node && node.$$typeof === REACT_ELEMENT_TYPE && node._store && (node._store.validated = 1); } var React = require$$0, REACT_ELEMENT_TYPE = Symbol.for("react.transitional.element"), REACT_PORTAL_TYPE = Symbol.for("react.portal"), REACT_FRAGMENT_TYPE = Symbol.for("react.fragment"), REACT_STRICT_MODE_TYPE = Symbol.for("react.strict_mode"), REACT_PROFILER_TYPE = Symbol.for("react.profiler"); var REACT_CONSUMER_TYPE = Symbol.for("react.consumer"), REACT_CONTEXT_TYPE = Symbol.for("react.context"), REACT_FORWARD_REF_TYPE = Symbol.for("react.forward_ref"), REACT_SUSPENSE_TYPE = Symbol.for("react.suspense"), REACT_SUSPENSE_LIST_TYPE = Symbol.for("react.suspense_list"), REACT_MEMO_TYPE = Symbol.for("react.memo"), REACT_LAZY_TYPE = Symbol.for("react.lazy"), REACT_ACTIVITY_TYPE = Symbol.for("react.activity"), REACT_CLIENT_REFERENCE = Symbol.for("react.client.reference"), ReactSharedInternals = React.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, hasOwnProperty = Object.prototype.hasOwnProperty, isArrayImpl = Array.isArray, createTask = console.createTask ? console.createTask : function () { return null; }; React = { "react-stack-bottom-frame": function (callStackForError) { return callStackForError(); } }; var specialPropKeyWarningShown; var didWarnAboutElementRef = {}; var unknownOwnerDebugStack = React["react-stack-bottom-frame"].bind( React, UnknownOwner )(); var unknownOwnerDebugTask = createTask(getTaskName(UnknownOwner)); var didWarnAboutKeySpread = {}; reactJsxRuntime_development.Fragment = REACT_FRAGMENT_TYPE; reactJsxRuntime_development.jsx = function (type, config, maybeKey, source, self) { var trackActualOwner = 1e4 > ReactSharedInternals.recentlyCreatedOwnerStacks++; return jsxDEVImpl( type, config, maybeKey, false, source, self, trackActualOwner ? Error("react-stack-top-frame") : unknownOwnerDebugStack, trackActualOwner ? createTask(getTaskName(type)) : unknownOwnerDebugTask ); }; reactJsxRuntime_development.jsxs = function (type, config, maybeKey, source, self) { var trackActualOwner = 1e4 > ReactSharedInternals.recentlyCreatedOwnerStacks++; return jsxDEVImpl( type, config, maybeKey, true, source, self, trackActualOwner ? Error("react-stack-top-frame") : unknownOwnerDebugStack, trackActualOwner ? createTask(getTaskName(type)) : unknownOwnerDebugTask ); }; })(); return reactJsxRuntime_development; } if (process.env.NODE_ENV === 'production') { jsxRuntime.exports = requireReactJsxRuntime_production(); } else { jsxRuntime.exports = requireReactJsxRuntime_development(); } var jsxRuntimeExports = jsxRuntime.exports; var jsQR$1 = {exports: {}}; (function (module, exports) { (function webpackUniversalModuleDefinition(root, factory) { module.exports = factory(); })(typeof self !== 'undefined' ? self : commonjsGlobal, function() { return /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) { /******/ return installedModules[moduleId].exports; /******/ } /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ i: moduleId, /******/ l: false, /******/ exports: {} /******/ }; /******/ /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ /******/ // Flag the module as loaded /******/ module.l = true; /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /******/ /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ /******/ // define getter function for harmony exports /******/ __webpack_require__.d = function(exports, name, getter) { /******/ if(!__webpack_require__.o(exports, name)) { /******/ Object.defineProperty(exports, name, { /******/ configurable: false, /******/ enumerable: true, /******/ get: getter /******/ }); /******/ } /******/ }; /******/ /******/ // getDefaultExport function for compatibility with non-harmony modules /******/ __webpack_require__.n = function(module) { /******/ var getter = module && module.__esModule ? /******/ function getDefault() { return module['default']; } : /******/ function getModuleExports() { return module; }; /******/ __webpack_require__.d(getter, 'a', getter); /******/ return getter; /******/ }; /******/ /******/ // Object.prototype.hasOwnProperty.call /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; /******/ /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ /******/ // Load entry module and return exports /******/ return __webpack_require__(__webpack_require__.s = 3); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ (function(module, exports, __webpack_require__) { Object.defineProperty(exports, "__esModule", { value: true }); var BitMatrix = /** @class */ (function () { function BitMatrix(data, width) { this.width = width; this.height = data.length / width; this.data = data; } BitMatrix.createEmpty = function (width, height) { return new BitMatrix(new Uint8ClampedArray(width * height), width); }; BitMatrix.prototype.get = function (x, y) { if (x < 0 || x >= this.width || y < 0 || y >= this.height) { return false; } return !!this.data[y * this.width + x]; }; BitMatrix.prototype.set = function (x, y, v) { this.data[y * this.width + x] = v ? 1 : 0; }; BitMatrix.prototype.setRegion = function (left, top, width, height, v) { for (var y = top; y < top + height; y++) { for (var x = left; x < left + width; x++) { this.set(x, y, !!v); } } }; return BitMatrix; }()); exports.BitMatrix = BitMatrix; /***/ }), /* 1 */ /***/ (function(module, exports, __webpack_require__) { Object.defineProperty(exports, "__esModule", { value: true }); var GenericGFPoly_1 = __webpack_require__(2); function addOrSubtractGF(a, b) { return a ^ b; // tslint:disable-line:no-bitwise } exports.addOrSubtractGF = addOrSubtractGF; var GenericGF = /** @class */ (function () { function GenericGF(primitive, size, genBase) { this.primitive = primitive; this.size = size; this.generatorBase = genBase; this.expTable = new Array(this.size); this.logTable = new Array(this.size); var x = 1; for (var i = 0; i < this.size; i++) { this.expTable[i] = x; x = x * 2; if (x >= this.size) { x = (x ^ this.primitive) & (this.size - 1); // tslint:disable-line:no-bitwise } } for (var i = 0; i < this.size - 1; i++) { this.logTable[this.expTable[i]] = i; } this.zero = new GenericGFPoly_1.default(this, Uint8ClampedArray.from([0])); this.one = new GenericGFPoly_1.default(this, Uint8ClampedArray.from([1])); } GenericGF.prototype.multiply = function (a, b) { if (a === 0 || b === 0) { return 0; } return this.expTable[(this.logTable[a] + this.logTable[b]) % (this.size - 1)]; }; GenericGF.prototype.inverse = function (a) { if (a === 0) { throw new Error("Can't invert 0"); } return this.expTable[this.size - this.logTable[a] - 1]; }; GenericGF.prototype.buildMonomial = function (degree, coefficient) { if (degree < 0) { throw new Error("Invalid monomial degree less than 0"); } if (coefficient === 0) { return this.zero; } var coefficients = new Uint8ClampedArray(degree + 1); coefficients[0] = coefficient; return new GenericGFPoly_1.default(this, coefficients); }; GenericGF.prototype.log = function (a) { if (a === 0) { throw new Error("Can't take log(0)"); } return this.logTable[a]; }; GenericGF.prototype.exp = function (a) { return this.expTable[a]; }; return GenericGF; }()); exports.default = GenericGF; /***/ }), /* 2 */ /***/ (function(module, exports, __webpack_require__) { Object.defineProperty(exports, "__esModule", { value: true }); var GenericGF_1 = __webpack_require__(1); var GenericGFPoly = /** @class */ (function () { function GenericGFPoly(field, coefficients) { if (coefficients.length === 0) { throw new Error("No coefficients."); } this.field = field; var coefficientsLength = coefficients.length; if (coefficientsLength > 1 && coefficients[0] === 0) { // Leading term must be non-zero for anything except the constant polynomial "0" var firstNonZero = 1; while (firstNonZero < coefficientsLength && coefficients[firstNonZero] === 0) { firstNonZero++; } if (firstNonZero === coefficientsLength) { this.coefficients = field.zero.coefficients; } else { this.coefficients = new Uint8ClampedArray(coefficientsLength - firstNonZero); for (var i = 0; i < this.coefficients.length; i++) { this.coefficients[i] = coefficients[firstNonZero + i]; } } } else { this.coefficients = coefficients; } } GenericGFPoly.prototype.degree = function () { return this.coefficients.length - 1; }; GenericGFPoly.prototype.isZero = function () { return this.coefficients[0] === 0; }; GenericGFPoly.prototype.getCoefficient = function (degree) { return this.coefficients[this.coefficients.length - 1 - degree]; }; GenericGFPoly.prototype.addOrSubtract = function (other) { var _a; if (this.isZero()) { return other; } if (other.isZero()) { return this; } var smallerCoefficients = this.coefficients; var largerCoefficients = other.coefficients; if (smallerCoefficients.length > largerCoefficients.length) { _a = [largerCoefficients, smallerCoefficients], smallerCoefficients = _a[0], largerCoefficients = _a[1]; } var sumDiff = new Uint8ClampedArray(largerCoefficients.length); var lengthDiff = largerCoefficients.length - smallerCoefficients.length; for (var i = 0; i < lengthDiff; i++) { sumDiff[i] = largerCoefficients[i]; } for (var i = lengthDiff; i < largerCoefficients.length; i++) { sumDiff[i] = GenericGF_1.addOrSubtractGF(smallerCoefficients[i - lengthDiff], largerCoefficients[i]); } return new GenericGFPoly(this.field, sumDiff); }; GenericGFPoly.prototype.multiply = function (scalar) { if (scalar === 0) { return this.field.zero; } if (scalar === 1) { return this; } var size = this.coefficients.length; var product = new Uint8ClampedArray(size); for (var i = 0; i < size; i++) { product[i] = this.field.multiply(this.coefficients[i], scalar); } return new GenericGFPoly(this.field, product); }; GenericGFPoly.prototype.multiplyPoly = function (other) { if (this.isZero() || other.isZero()) { return this.field.zero; } var aCoefficients = this.coefficients; var aLength = aCoefficients.length; var bCoefficients = other.coefficients; var bLength = bCoefficients.length; var product = new Uint8ClampedArray(aLength + bLength - 1); for (var i = 0; i < aLength; i++) { var aCoeff = aCoefficients[i]; for (var j = 0; j < bLength; j++) { product[i + j] = GenericGF_1.addOrSubtractGF(product[i + j], this.field.multiply(aCoeff, bCoefficients[j])); } } return new GenericGFPoly(this.field, product); }; GenericGFPoly.prototype.multiplyByMonomial = function (degree, coefficient) { if (degree < 0) { throw new Error("Invalid degree less than 0"); } if (coefficient === 0) { return this.field.zero; } var size = this.coefficients.length; var product = new Uint8ClampedArray(size + degree); for (var i = 0; i < size; i++) { product[i] = this.field.multiply(this.coefficients[i], coefficient); } return new GenericGFPoly(this.field, product); }; GenericGFPoly.prototype.evaluateAt = function (a) { var result = 0; if (a === 0) { // Just return the x^0 coefficient return this.getCoefficient(0); } var size = this.coefficients.length; if (a === 1) { // Just the sum of the coefficients this.coefficients.forEach(function (coefficient) { result = GenericGF_1.addOrSubtractGF(result, coefficient); }); return result; } result = this.coefficients[0]; for (var i = 1; i < size; i++) { result = GenericGF_1.addOrSubtractGF(this.field.multiply(a, result), this.coefficients[i]); } return result; }; return GenericGFPoly; }()); exports.default = GenericGFPoly; /***/ }), /* 3 */ /***/ (function(module, exports, __webpack_require__) { Object.defineProperty(exports, "__esModule", { value: true }); var binarizer_1 = __webpack_require__(4); var decoder_1 = __webpack_require__(5); var extractor_1 = __webpack_require__(11); var locator_1 = __webpack_require__(12); function scan(matrix) { var locations = locator_1.locate(matrix); if (!locations) { return null; } for (var _i = 0, locations_1 = locations; _i < locations_1.length; _i++) { var location_1 = locations_1[_i]; var extracted = extractor_1.extract(matrix, location_1); var decoded = decoder_1.decode(extracted.matrix); if (decoded) { return { binaryData: decoded.bytes, data: decoded.text, chunks: decoded.chunks, version: decoded.version, location: { topRightCorner: extracted.mappingFunction(location_1.dimension, 0), topLeftCorner: extracted.mappingFunction(0, 0), bottomRightCorner: extracted.mappingFunction(location_1.dimension, location_1.dimension), bottomLeftCorner: extracted.mappingFunction(0, location_1.dimension), topRightFinderPattern: location_1.topRight, topLeftFinderPattern: location_1.topLeft, bottomLeftFinderPattern: location_1.bottomLeft, bottomRightAlignmentPattern: location_1.alignmentPattern, }, }; } } return null; } var defaultOptions = { inversionAttempts: "attemptBoth", }; function jsQR(data, width, height, providedOptions) { if (providedOptions === void 0) { providedOptions = {}; } var options = defaultOptions; Object.keys(options || {}).forEach(function (opt) { options[opt] = providedOptions[opt] || options[opt]; }); var shouldInvert = options.inversionAttempts === "attemptBoth" || options.inversionAttempts === "invertFirst"; var tryInvertedFirst = options.inversionAttempts === "onlyInvert" || options.inversionAttempts === "invertFirst"; var _a = binarizer_1.binarize(data, width, height, shouldInvert), binarized = _a.binarized, inverted = _a.inverted; var result = scan(tryInvertedFirst ? inverted : binarized); if (!result && (options.inversionAttempts === "attemptBoth" || options.inversionAttempts === "invertFirst")) { result = scan(tryInvertedFirst ? binarized : inverted); } return result; } jsQR.default = jsQR; exports.default = jsQR; /***/ }), /* 4 */ /***/ (function(module, exports, __webpack_require__) { Object.defineProperty(exports, "__esModule", { value: true }); var BitMatrix_1 = __webpack_require__(0); var REGION_SIZE = 8; var MIN_DYNAMIC_RANGE = 24; function numBetween(value, min, max) { return value < min ? min : value > max ? max : value; } // Like BitMatrix but accepts arbitry Uint8 values var Matrix = /** @class */ (function () { function Matrix(width, height) { this.width = width; this.data = new Uint8ClampedArray(width * height); } Matrix.prototype.get = function (x, y) { return this.data[y * this.width + x]; }; Matrix.prototype.set = function (x, y, value) { this.data[y * this.width + x] = value; }; return Matrix; }()); function binarize(data, width, height, returnInverted) { if (data.length !== width * height * 4) { throw new Error("Malformed data passed to binarizer."); } // Convert image to greyscale var greyscalePixels = new Matrix(width, height); for (var x = 0; x < width; x++) { for (var y = 0; y < height; y++) { var r = data[((y * width + x) * 4) + 0]; var g = data[((y * width + x) * 4) + 1]; var b = data[((y * width + x) * 4) + 2]; greyscalePixels.set(x, y, 0.2126 * r + 0.7152 * g + 0.0722 * b); } } var horizontalRegionCount = Math.ceil(width / REGION_SIZE); var verticalRegionCount = Math.ceil(height / REGION_SIZE); var blackPoints = new Matrix(horizontalRegionCount, verticalRegionCount); for (var verticalRegion = 0; verticalRegion < verticalRegionCount; verticalRegion++) { for (var hortizontalRegion = 0; hortizontalRegion < horizontalRegionCount; hortizontalRegion++) { var sum = 0; var min = Infinity; var max = 0; for (var y = 0; y < REGION_SIZE; y++) { for (var x = 0; x < REGION_SIZE; x++) { var pixelLumosity = greyscalePixels.get(hortizontalRegion * REGION_SIZE + x, verticalRegion * REGION_SIZE + y); sum += pixelLumosity; min = Math.min(min, pixelLumosity); max = Math.max(max, pixelLumosity); } } var average = sum / (Math.pow(REGION_SIZE, 2)); if (max - min <= MIN_DYNAMIC_RANGE) { // If variation within the block is low, assume this is a block with only light or only // dark pixels. In that case we do not want to use the average, as it would divide this // low contrast area into black and white pixels, essentially creating data out of noise. // // Default the blackpoint for these blocks to be half the min - effectively white them out average = min / 2; if (verticalRegion > 0 && hortizontalRegion > 0) { // Correct the "white background" assumption for blocks that have neighbors by comparing // the pixels in this block to the previously calculated black points. This is based on // the fact that dark barcode symbology is always surrounded by some amount of light // background for which reasonable black point estimates were made. The bp estimated at // the boundaries is used for the interior. // The (min < bp) is arbitrary but works better than other heuristics that were tried. var averageNeighborBlackPoint = (blackPoints.get(hortizontalRegion, verticalRegion - 1) + (2 * blackPoints.get(hortizontalRegion - 1, verticalRegion)) + blackPoints.get(hortizontalRegion - 1, verticalRegion - 1)) / 4; if (min < averageNeighborBlackPoint) { average = averageNeighborBlackPoint; } } } blackPoints.set(hortizontalRegion, verticalRegion, average); } } var binarized = BitMatrix_1.BitMatrix.createEmpty(width, height); var inverted = null; if (returnInverted) { inverted = BitMatrix_1.BitMatrix.createEmpty(width, height); } for (var verticalRegion = 0; verticalRegion < verticalRegionCount; verticalRegion++) { for (var hortizontalRegion = 0; hortizontalRegion < horizontalRegionCount; hortizontalRegion++) { var left = numBetween(hortizontalRegion, 2, horizontalRegionCount - 3); var top_1 = numBetween(verticalRegion, 2, verticalRegionCount - 3); var sum = 0; for (var xRegion = -2; xRegion <= 2; xRegion++) { for (var yRegion = -2; yRegion <= 2; yRegion++) { sum += blackPoints.get(left + xRegion, top_1 + yRegion); } } var threshold = sum / 25; for (var xRegion = 0; xRegion < REGION_SIZE; xRegion++) { for (var yRegion = 0; yRegion < REGION_SIZE; yRegion++) { var x = hortizontalRegion * REGION_SIZE + xRegion; var y = verticalRegion * REGION_SIZE + yRegion; var lum = greyscalePixels.get(x, y); binarized.set(x, y, lum <= threshold); if (returnInverted) { inverted.set(x, y, !(lum <= threshold)); } } } } } if (returnInverted) { return { binarized: binarized, inverted: inverted }; } return { binarized: binarized }; } exports.binarize = binarize; /***/ }), /* 5 */ /***/ (function(module, exports, __webpack_require__) { Object.defineProperty(exports, "__esModule", { value: true }); var BitMatrix_1 = __webpack_require__(0); var decodeData_1 = __webpack_require__(6); var reedsolomon_1 = __webpack_require__(9); var version_1 = __webpack_require__(10); // tslint:disable:no-bitwise function numBitsDiffering(x, y) { var z = x ^ y; var bitCount = 0; while (z) { bitCount++; z &= z - 1; } return bitCount; } function pushBit(bit, byte) { return (byte << 1) | bit; } // tslint:enable:no-bitwise var FORMAT_INFO_TABLE = [ { bits: 0x5412, formatInfo: { errorCorrectionLevel: 1, dataMask: 0 } }, { bits: 0x5125, formatInfo: { errorCorrectionLevel: 1, dataMask: 1 } }, { bits: 0x5E7C, formatInfo: { errorCorrectionLevel: 1, dataMask: 2 } }, { bits: 0x5B4B, formatInfo: { errorCorrectionLevel: 1, dataMask: 3 } }, { bits: 0x45F9, formatInfo: { errorCorrectionLevel: 1, dataMask: 4 } }, { bits: 0x40CE, formatInfo: { errorCorrectionLevel: 1, dataMask: 5 } }, { bits: 0x4F97, formatInfo: { errorCorrectionLevel: 1, dataMask: 6 } }, { bits: 0x4AA0, formatInfo: { errorCorrectionLevel: 1, dataMask: 7 } }, { bits: 0x77C4, formatInfo: { errorCorrectionLevel: 0, dataMask: 0 } }, { bits: 0x72F3, formatInfo: { errorCorrectionLevel: 0, dataMask: 1 } }, { bits: 0x7DAA, formatInfo: { errorCorrectionLevel: 0, dataMask: 2 } }, { bits: 0x789D, formatInfo: { errorCorrectionLevel: 0, dataMask: 3 } }, { bits: 0x662F, formatInfo: { errorCorrectionLevel: 0, dataMask: 4 } }, { bits: 0x6318, formatInfo: { errorCorrectionLevel: 0, dataMask: 5 } }, { bits: 0x6C41, formatInfo: { errorCorrectionLevel: 0, dataMask: 6 } }, { bits: 0x6976, formatInfo: { errorCorrectionLevel: 0, dataMask: 7 } }, { bits: 0x1689, formatInfo: { errorCorrectionLevel: 3, dataMask: 0 } }, { bits: 0x13BE, formatInfo: { errorCorrectionLevel: 3, dataMask: 1 } }, { bits: 0x1CE7, formatInfo: { errorCorrectionLevel: 3, dataMask: 2 } }, { bits: 0x19D0, formatInfo: { errorCorrectionLevel: 3, dataMask: 3 } }, { bits: 0x0762, formatInfo: { errorCorrectionLevel: 3, dataMask: 4 } }, { bits: 0x0255, formatInfo: { errorCorrectionLevel: 3, dataMask: 5 } }, { bits: 0x0D0C, formatInfo: { errorCorrectionLevel: 3, dataMask: 6 } }, { bits: 0x083B, formatInfo: { errorCorrectionLevel: 3, dataMask: 7 } }, { bits: 0x355F, formatInfo: { errorCorrectionLevel: 2, dataMask: 0 } }, { bits: 0x3068, formatInfo: { errorCorrectionLevel: 2, dataMask: 1 } }, { bits: 0x3F31, formatInfo: { errorCorrectionLevel: 2, dataMask: 2 } }, { bits: 0x3A06, formatInfo: { errorCorrectionLevel: 2, dataMask: 3 } }, { bits: 0x24B4, formatInfo: { errorCorrectionLevel: 2, dataMask: 4 } }, { bits: 0x2183, formatInfo: { errorCorrectionLevel: 2, dataMask: 5 } }, { bits: 0x2EDA, formatInfo: { errorCorrectionLevel: 2, dataMask: 6 } }, { bits: 0x2BED, formatInfo: { errorCorrectionLevel: 2, dataMask: 7 } }, ]; var DATA_MASKS = [ function (p) { return ((p.y + p.x) % 2) === 0; }, function (p) { return (p.y % 2) === 0; }, function (p) { return p.x % 3 === 0; }, function (p) { return (p.y + p.x) % 3 === 0; }, function (p) { return (Math.floor(p.y / 2) + Math.floor(p.x / 3)) % 2 === 0; }, function (p) { return ((p.x * p.y) % 2) + ((p.x * p.y) % 3) === 0; }, function (p) { return ((((p.y * p.x) % 2) + (p.y * p.x) % 3) % 2) === 0; }, function (p) { return ((((p.y + p.x) % 2) + (p.y * p.x) % 3) % 2) === 0; }, ]; function buildFunctionPatternMask(version) { var dimension = 17 + 4 * version.versionNumber; var matrix = BitMatrix_1.BitMatrix.createEmpty(dimension, dimension); matrix.setRegion(0, 0, 9, 9, true); // Top left finder pattern + separator + format matrix.setRegion(dimension - 8, 0, 8, 9, true); // Top right finder pattern + separator + format matrix.setRegion(0, dimension - 8, 9, 8, true); // Bottom left finder pattern + separator + format // Alignment patterns for (var _i = 0, _a = version.alignmentPatternCenters; _i < _a.length; _i++) { var x = _a[_i]; for (var _b = 0, _c = version.alignmentPatternCenters; _b < _c.length; _b++) { var y = _c[_b]; if (!(x === 6 && y === 6 || x === 6 && y === dimension - 7 || x === dimension - 7 && y === 6)) { matrix.setRegion(x - 2, y - 2, 5, 5, true); } } } matrix.setRegion(6, 9, 1, dimension - 17, true); // Vertical timing pattern matrix.setRegion(9, 6, dimension - 17, 1, true); // Horizontal timing pattern if (version.versionNumber > 6) { matrix.setRegion(dimension - 11, 0, 3, 6, true); // Version info, top right matrix.setRegion(0, dimension - 11, 6, 3, true); // Version info, bottom left } return matrix; } function readCodewords(matrix, version, formatInfo) { var dataMask = DATA_MASKS[formatInfo.dataMask]; var dimension = matrix.height; var functionPatternMask = buildFunctionPatternMask(version); var codewords = []; var currentByte = 0; var bitsRead = 0; // Read columns in pairs, from right to left var readingUp = true; for (var columnIndex = dimension - 1; columnIndex > 0; columnIndex -= 2) { if (columnIndex === 6) { // Skip whole column with vertical alignment pattern; columnIndex--; } for (var i = 0; i < dimension; i++) { var y = readingUp ? dimension - 1 - i : i; for (var columnOffset = 0; columnOffset < 2; columnOffset++) { var x = columnIndex - columnOffset; if (!functionPatternMask.get(x, y)) { bitsRead++; var bit = matrix.get(x, y); if (dataMask({ y: y, x: x })) { bit = !bit; } currentByte = pushBit(bit, currentByte); if (bitsRead === 8) { // Whole bytes codewords.push(currentByte); bitsRead = 0; currentByte = 0; } } } } readingUp = !readingUp; } return codewords; } function readVersion(matrix) { var dimension = matrix.height; var provisionalVersion = Math.floor((dimension - 17) / 4); if (provisionalVersion <= 6) { // 6 and under dont have version info in the QR code return version_1.VERSIONS[provisionalVersion - 1]; } var topRightVersionBits = 0; for (var y = 5; y >= 0; y--) { for (var x = dimension - 9; x >= dimension - 11; x--) { topRightVersionBits = pushBit(matrix.get(x, y), topRightVersionBits); } } var bottomLeftVersionBits = 0; for (var x = 5; x >= 0; x--) { for (var y = dimension - 9; y >= dimension - 11; y--) { bottomLeftVersionBits = pushBit(matrix.get(x, y), bottomLeftVersionBits); } } var bestDifference = Infinity; var bestVersion; for (var _i = 0, VERSIONS_1 = version_1.VERSIONS; _i < VERSIONS_1.length; _i++) { var version = VERSIONS_1[_i]; if (version.infoBits === topRightVersionBits || version.infoBits === bottomLeftVersionBits) { return version; } var difference = numBitsDiffering(topRightVersionBits, version.infoBits); if (difference < bestDifference) { bestVersion = version; bestDifference = difference; } difference = numBitsDiffering(bottomLeftVersionBits, version.infoBits); if (difference < bestDifference) { bestVersion = version; bestDifference = difference; } } // We can tolerate up to 3 bits of error since no two version info codewords will // differ in less than 8 bits. if (bestDifference <= 3) { return bestVersion; } } function readFormatInformation(matrix) { var topLeftFormatInfoBits = 0; for (var x = 0; x <= 8; x++) { if (x !== 6) { // Skip timing pattern bit topLeftFormatInfoBits = pushBit(matrix.get(x, 8), topLeftFormatInfoBits); } } for (var y = 7; y >= 0; y--) { if (y !== 6) { // Skip timing pattern bit topLeftFormatInfoBits = pushBit(matrix.get(8, y), topLeftFormatInfoBits); } } var dimension = matrix.height; var topRightBottomRightFormatInfoBits = 0; for (var y = dimension - 1; y >= dimension - 7; y--) { // bottom left topRightBottomRightFormatInfoBits = pushBit(matrix.get(8, y), topRightBottomRightFormatInfoBits); } for (var x = dimension - 8; x < dimension; x++) { // top right topRightBottomRightFormatInfoBits = pushBit(matrix.get(x, 8), topRightBottomRightFormatInfoBits); } var bestDifference = Infinity; var bestFormatInfo = null; for (var _i = 0, FORMAT_INFO_TABLE_1 = FORMAT_INFO_TABLE; _i < FORMAT_INFO_TABLE_1.length; _i++) { var _a = FORMAT_INFO_TABLE_1[_i], bits = _a.bits, formatInfo = _a.formatInfo; if (bits === topLeftFormatInfoBits || bits === topRightBottomRightFormatInfoBits) { return formatInfo; } var difference = numBitsDiffering(topLeftFormatInfoBits, bits); if (difference < bestDifference) { bestFormatInfo = formatInfo; bestDifference = difference; } if (topLeftFormatInfoBits !== topRightBottomRightFormatInfoBits) { // also try the other option difference = numBitsDiffering(topRightBottomRightFormatInfoBits, bits); if (difference < bestDifference) { bestFormatInfo = formatInfo; bestDifference = difference; } } } // Hamming distance of the 32 masked codes is 7, by construction, so <= 3 bits differing means we found a match if (bestDifference <= 3) { return bestFormatInfo; } return null; } function getDataBlocks(codewords, version, ecLevel) { var ecInfo = version.errorCorrectionLevels[ecLevel]; var dataBlocks = []; var totalCodewords = 0; ecInfo.ecBlocks.forEach(function (block) { for (var i = 0; i < block.numBlocks; i++) { dataBlocks.push({ numDataCodewords: block.dataCodewordsPerBlock, codewords: [] }); totalCodewords += block.dataCodewordsPerBlock + ecInfo.ecCodewordsPerBlock; } }); // In some cases the QR code will be malformed enough that we pull off more or less than we should. // If we pull off less there's nothing we can do. // If we pull off more we can safely truncate if (codewords.length < totalCodewords) { return null; } codewords = codewords.slice(0, totalCodewords); var shortBlockSize = ecInfo.ecBlocks[0].dataCodewordsPerBlock; // Pull codewords to fill the blocks up to the minimum size for (var i = 0; i < shortBlockSize; i++) { for (var _i = 0, dataBlocks_1 = dataBlocks; _i < dataBlocks_1.length; _i++) { var dataBlock = dataBlocks_1[_i]; dataBlock.codewords.push(codewords.shift()); } } // If there are any large blocks, pull codewords to fill the last element of those if (ecInfo.ecBlocks.length > 1) { var smallBlockCount = ecInfo.ecBlocks[0].numBlocks; var largeBlockCount = ecInfo.ecBlocks[1].numBlocks; for (var i = 0; i < largeBlockCount; i++) { dataBlocks[smallBlockCount + i].codewords.push(codewords.shift()); } } // Add the rest of the codewords to the blocks. These are the error correction codewords. while (codewords.length > 0) { for (var _a = 0, dataBlocks_2 = dataBlocks; _a < dataBlocks_2.length; _a++) { var dataBlock = dataBlocks_2[_a]; dataBlock.codewords.push(codewords.shift()); } } return dataBlocks; } function decodeMatrix(matrix) { var version = readVersion(matrix); if (!version) { return null; } var formatInfo = readFormatInformation(matrix); if (!formatInfo) { return null; } var codewords = readCodewords(matrix, version, formatInfo); var dataBlocks = getDataBlocks(codewords, version, formatInfo.errorCorrectionLevel); if (!dataBlocks) { return null; } // Count total number of data bytes var totalBytes = dataBlocks.reduce(function (a, b) { return a + b.numDataCodewords; }, 0); var resultBytes = new Uint8ClampedArray(totalBytes); var resultIndex = 0; for (var _i = 0, dataBlocks_3 = dataBlocks; _i < dataBlocks_3.length; _i++) { var dataBlock = dataBlocks_3[_i]; var correctedBytes = reedsolomon_1.decode(dataBlock.codewords, dataBlock.codewords.length - dataBlock.numDataCodewords); if (!correctedBytes) { return null; } for (var i = 0; i < dataBlock.numDataCodewords; i++) { resultBytes[resultIndex++] = correctedBytes[i]; } } try { return decodeData_1.decode(resultBytes, version.versionNumber); } catch (_a) { return null; } } function decode(matrix) { if (matrix == null) { return null; } var result = decodeMatrix(matrix); if (result) { return result; } // Decoding didn't work, try mirroring the QR across the topLeft -> bottomRight line. for (var x = 0; x < matrix.width; x++) { for (var y = x + 1; y < matrix.height; y++) { if (matrix.get(x, y) !== matrix.get(y, x)) { matrix.set(x, y, !matrix.get(x, y)); matrix.set(y, x, !matrix.get(y, x)); } } } return decodeMatrix(matrix); } exports.decode = decode; /***/ }), /* 6 */ /***/ (function(module, exports, __webpack_require__) { Object.defineProperty(exports, "__esModule", { value: true }); // tslint:disable:no-bitwise var BitStream_1 = __webpack_require__(7); var shiftJISTable_1 = __webpack_require__(8); var Mode; (function (Mode) { Mode["Numeric"] = "numeric"; Mode["Alphanumeric"] = "alphanumeric"; Mode["Byte"] = "byte"; Mode["Kanji"] = "kanji"; Mode["ECI"] = "eci"; })(Mode = exports.Mode || (exports.Mode = {})); var ModeByte; (function (ModeByte) { ModeByte[ModeByte["Terminator"] = 0] = "Terminator"; ModeByte[ModeByte["Numeric"] = 1] = "Numeric"; ModeByte[ModeByte["Alphanumeric"] = 2] = "Alphanumeric"; ModeByte[ModeByte["Byte"] = 4] = "Byte"; ModeByte[ModeByte["Kanji"] = 8] = "Kanji"; ModeByte[ModeByte["ECI"] = 7] = "ECI"; // StructuredAppend = 0x3, // FNC1FirstPosition = 0x5, // FNC1SecondPosition = 0x