covid-green-dcc-file-decoder
Version:
<img alttext="COVID Green Logo" src="https://raw.githubusercontent.com/lfph/artwork/master/projects/covidgreen/stacked/color/covidgreen-stacked-color.png" width="300" />
58 lines (57 loc) • 2.74 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractQRFromImage = void 0;
const jsqr_1 = __importDefault(require("jsqr"));
const jimp_1 = __importDefault(require("jimp"));
// import path from 'path'
const util_1 = require("../util");
const splitImage = (source) => __awaiter(void 0, void 0, void 0, function* () {
// Parse the image using Jimp.read() method
const uncroppedLeft = yield jimp_1.default.read(source);
const uncroppedRight = yield jimp_1.default.read(source);
yield uncroppedLeft.crop(0, 0, Math.ceil(uncroppedLeft.bitmap.width / 2), uncroppedLeft.bitmap.height);
// uncroppedLeft.write(path.join(__dirname, 'myimage1.png'))
const width = Math.floor(uncroppedRight.bitmap.width / 2);
const offset = Math.floor(width * 0.1);
yield uncroppedRight.crop(width - offset, 0, width + offset, uncroppedRight.bitmap.height);
// uncroppedRight.write(path.join(__dirname, 'myimage2.png'))
return [uncroppedRight, uncroppedLeft];
});
const findQR = (images) => {
const qrCodes = [];
for (const image of images) {
const qrObj = jsqr_1.default(image.bitmap.data, image.bitmap.width, image.bitmap.height);
if (qrObj === null || qrObj === void 0 ? void 0 : qrObj.data) {
qrCodes.push(qrObj.data || Buffer.from(qrObj.binaryData).toString());
}
}
return qrCodes;
};
function extractQRFromImage(sourceImage) {
return __awaiter(this, void 0, void 0, function* () {
const image = yield jimp_1.default.read(sourceImage);
let qrs = findQR([image]);
if (qrs.length === 0) {
// half image vertically
const parts = yield splitImage(sourceImage);
qrs = findQR(parts);
}
if (qrs.length === 0) {
throw util_1.qrNotDetected();
}
return qrs;
});
}
exports.extractQRFromImage = extractQRFromImage;