qr-hpulido
Version:
Encrypt and decrypt data using your own key
124 lines (123 loc) • 5.89 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.generateContact = exports.generateWifi = exports.generateEmail = exports.generateTextPlain = exports.generateUrl = exports.generateSms = void 0;
const qrcode_1 = __importDefault(require("qrcode"));
const axios_1 = __importDefault(require("axios"));
const jimp_1 = __importDefault(require("jimp"));
const downloadAndProcessImage = (url) => __awaiter(void 0, void 0, void 0, function* () {
try {
const response = yield (0, axios_1.default)({
method: 'get',
url: url,
responseType: 'arraybuffer',
});
const buffer = Buffer.from(response.data);
const image = yield jimp_1.default.read(buffer);
image.resize(512, 512).circle({ radius: 256 });
return image;
}
catch (err) {
console.error('Error al descargar la imagen:', err);
throw err;
}
});
const generateLogoCircle = (url) => __awaiter(void 0, void 0, void 0, function* () {
try {
const lenna = yield downloadAndProcessImage(url);
const borderWidth = lenna.bitmap.width * 0.1;
const original_width = lenna.bitmap.width;
const original_height = lenna.bitmap.height;
const new_width = original_width < original_height ? original_height : original_width;
const new_height = original_height < original_width ? original_width : original_height;
// Create a new image with the size increased to include the frame
const circle_image = new jimp_1.default(new_width + 2 * borderWidth, new_height + 2 * borderWidth, 0xFFFFFFFF);
const x = (new_width - original_width) / 2;
const y = (new_height - original_height) / 2;
// Paste the original image in the center of the new image with the frame
if (original_width === original_height) {
circle_image.composite(lenna, borderWidth, borderWidth);
}
else {
circle_image.composite(lenna, x, y);
}
// Create the circle in the new image
circle_image.circle({
radius: circle_image.bitmap.width / 2 - circle_image.bitmap.width * 0.00,
antialias: true,
}).quality(100);
const img = yield circle_image.resize(64, 64).circle({ radius: circle_image.bitmap.width / 2 });
return img;
}
catch (err) {
console.error('Error en el proceso:', err);
throw err;
}
});
const generateQr = (text, url_icon) => __awaiter(void 0, void 0, void 0, function* () {
const url = yield new Promise((resolve, reject) => {
qrcode_1.default.toDataURL(text, { width: 512 }, (err, url) => {
if (err) {
console.error(err);
reject(err);
}
else {
resolve(url);
}
});
});
// Procesamiento posterior utilizando la URL obtenida
const base64_data = url.replace(/^data:image\/png;base64,/, '');
const qrImage = yield jimp_1.default.read(Buffer.from(base64_data, 'base64'));
const iconImage = yield generateLogoCircle(url_icon);
const x = Math.floor((512 - 64) / 2);
const y = Math.floor((512 - 64) / 2);
qrImage.composite(iconImage, x, y);
return qrImage;
});
const generateSms = (number, sms, url_icon) => __awaiter(void 0, void 0, void 0, function* () {
const text = "SMSTO:" + number + ":" + sms;
const image = yield generateQr(text, url_icon);
return image;
});
exports.generateSms = generateSms;
const generateUrl = (url, url_icon) => __awaiter(void 0, void 0, void 0, function* () {
const image = yield generateQr(url, url_icon);
return image;
});
exports.generateUrl = generateUrl;
const generateTextPlain = (text, url_icon) => __awaiter(void 0, void 0, void 0, function* () {
const image = yield generateQr(text, url_icon);
return image;
});
exports.generateTextPlain = generateTextPlain;
const generateEmail = (to, subject, body, url_icon) => __awaiter(void 0, void 0, void 0, function* () {
const text = "MATMSG:TO:" + to + ";SUB:" + subject + ";BODY:" + body + "send;;";
const image = yield generateQr(text, url_icon);
return image;
});
exports.generateEmail = generateEmail;
const generateWifi = (name, password, url_icon) => __awaiter(void 0, void 0, void 0, function* () {
const text = "WIFI:T:WPA;S:" + name + ";P:" + password + ";;";
const image = yield generateQr(text, url_icon);
return image;
});
exports.generateWifi = generateWifi;
const generateContact = (name, telephone, email, company, address, website, url_icon) => __awaiter(void 0, void 0, void 0, function* () {
var text = "";
text += "BEGIN:VCARD\nVERSION:3.0\nN:" + name ? name : '' + "\nTEL:" + telephone ? telephone : '' + "\nEMAIL:" + email ? email : '' + "\nORG:" + company ? company : '' + "\nADR:" + address ? address : '' + "\nURL:" + website ? website : '' + "\nEND:VCARD";
const image = yield generateQr(text, url_icon);
return image;
});
exports.generateContact = generateContact;