qr_image/index.js

251 wiersze
6.5 KiB
JavaScript
Czysty Zwykły widok Historia

2022-01-25 05:35:47 +00:00
/**
* Section: Uploading Images
*/
2022-01-23 03:22:52 +00:00
var imageLoader = document.getElementById("imageLoader");
imageLoader.addEventListener("change", handleImage, false);
var uploadCanvas = document.getElementById("imageCanvas");
var uploadContext = uploadCanvas.getContext("2d");
uploadWidth = 200;
uploadHeight = 200;
uploadCanvas.width = uploadWidth;
uploadCanvas.height = uploadHeight;
2022-01-29 06:43:47 +00:00
2022-01-23 03:22:52 +00:00
img = false;
function handleImage(e) {
var reader = new FileReader();
reader.onload = function (event) {
img = new Image();
img.onload = function () {
uploadContext.clearRect(0, 0, uploadWidth, uploadHeight);
uploadContext.drawImage(img, 0, 0, uploadWidth, uploadHeight);
2022-01-23 03:22:52 +00:00
};
img.src = event.target.result;
};
reader.readAsDataURL(e.target.files[0]);
}
2022-01-25 05:35:47 +00:00
/**
* Section: Initialize qrcode and canvas
*/
var canvas = false;
var qrcode = new QRCode("qrcode", {
2022-01-22 19:05:38 +00:00
width: 256,
height: 256,
colorDark: "#000000",
colorLight: "#ffffff",
2022-01-23 03:22:52 +00:00
correctLevel: QRCode.CorrectLevel.L,
});
2022-01-25 05:35:47 +00:00
/**
* Section: Code to handle inputs (e.g., sliders)
*/
// Size of QR Code squares
2022-01-23 03:22:52 +00:00
var sizeSlider = document.getElementById("radiusSize");
var sizeOutput = document.getElementById("printSize");
2022-01-25 05:35:47 +00:00
// Display the default slider value and grab it
sizeOutput.innerHTML = sizeSlider.value;
2022-01-23 03:42:10 +00:00
var radiusRatio = sizeSlider.value / 200;
2022-01-23 03:22:52 +00:00
2022-01-25 05:35:47 +00:00
// Update the current slider value
2022-01-23 03:22:52 +00:00
sizeSlider.oninput = function () {
sizeOutput.innerHTML = this.value;
2022-01-23 03:42:10 +00:00
radiusRatio = this.value / 200;
2022-01-23 03:22:52 +00:00
};
2022-01-25 05:35:47 +00:00
// Level of error correction (low, medium, high) (excluding quartile)
2022-01-23 03:22:52 +00:00
var correctionSlider = document.getElementById("errorCorrection");
var correctionOutput = document.getElementById("printCorrection");
2022-01-25 05:35:47 +00:00
// Display the default slider value and grab it
correctionOutput.innerHTML = correctionSlider.value;
2022-01-23 03:22:52 +00:00
var correctionLevel = correctionSlider.value;
2022-01-23 03:42:10 +00:00
if (correctionLevel === "1") {
2022-01-23 03:22:52 +00:00
qrcode._htOption.correctLevel = QRCode.CorrectLevel.L;
2022-01-23 03:42:10 +00:00
} else if (correctionLevel === "2") {
2022-01-23 03:22:52 +00:00
qrcode._htOption.correctLevel = QRCode.CorrectLevel.M;
2022-01-23 03:42:10 +00:00
} else if (correctionLevel === "3") {
2022-01-23 03:22:52 +00:00
qrcode._htOption.correctLevel = QRCode.CorrectLevel.H;
2022-01-23 03:42:10 +00:00
}
2022-01-23 03:22:52 +00:00
2022-01-25 05:35:47 +00:00
// Update the current slider value
2022-01-23 03:22:52 +00:00
correctionSlider.oninput = function () {
correctionOutput.innerHTML = this.value;
correctionLevel = correctionSlider.value;
2022-01-23 03:42:10 +00:00
if (correctionLevel === "1") {
2022-01-23 03:22:52 +00:00
qrcode._htOption.correctLevel = QRCode.CorrectLevel.L;
2022-01-23 03:42:10 +00:00
} else if (correctionLevel === "2") {
2022-01-23 03:22:52 +00:00
qrcode._htOption.correctLevel = QRCode.CorrectLevel.M;
2022-01-23 03:42:10 +00:00
} else if (correctionLevel === "3") {
2022-01-23 03:22:52 +00:00
qrcode._htOption.correctLevel = QRCode.CorrectLevel.H;
2022-01-23 03:42:10 +00:00
}
2022-01-23 03:22:52 +00:00
};
2022-01-25 05:35:47 +00:00
// Size of white border (quiet zone)
var borderSlider = document.getElementById("borderSize");
var borderOutput = document.getElementById("printBorderSize");
borderOutput.innerHTML = borderSlider.value; // Display the default slider value
2022-01-23 17:45:15 +00:00
var borderSizeValue = Number(borderSlider.value);
// Update the current slider value (each time you drag the slider handle)
borderSlider.oninput = function () {
borderOutput.innerHTML = this.value;
2022-01-23 17:45:15 +00:00
borderSizeValue = Number(this.value);
};
2022-01-25 05:35:47 +00:00
/**
* Section: Helper functions for visualizing QR code
*/
2022-01-22 19:05:38 +00:00
/**
* Check whether bit at current position should be full sized.
2022-01-25 05:35:47 +00:00
* In particular, make the position bits (corners) full sized.
2022-01-22 19:05:38 +00:00
*
* @param {i} The current bit's row.
* @param {j} The current bit's column.
* @param {QRLength} The length of the QR code.
* @return {isPosition} Whether or not the current bit is safe to modify.
*/
function isSafeBit(i, j, QRLength) {
// Currently hard coding position bits
2022-01-25 01:20:42 +00:00
lowerLimit = 7 + borderSizeValue;
upperLimit = QRLength - 8 + borderSizeValue;
if (i < lowerLimit && j < lowerLimit) {
2022-01-22 19:05:38 +00:00
return false;
} else if (i > upperLimit && j < lowerLimit) {
2022-01-22 19:05:38 +00:00
return false;
} else if (i < lowerLimit && j > upperLimit) {
2022-01-22 19:05:38 +00:00
return false;
2022-01-23 03:22:52 +00:00
}
2022-01-22 19:05:38 +00:00
return true;
}
/**
* Draw basic shape representing each bit of the QR code.
*
* @param {ctx} Context of associated canvas.
* @param {i} The current bit's row.
* @param {j} The current bit's column.
* @param {bitLength} The maximum length of each bit.
* @param {radiusRatio} The radius should be this ratio times the bitLength.
* The ratio should be between 0 and 0.5.
* @param {QRLength} The length of the QR code.
*/
function drawShape(ctx, i, j, bitLength, radiusRatio, QRLength) {
// Draw centered
xCenter = bitLength * (i + 0.5);
yCenter = bitLength * (j + 0.5);
if (!isSafeBit(i, j, QRLength)) {
radiusRatio = 0.5;
}
radius = bitLength * radiusRatio;
ctx.fillRect(xCenter - radius, yCenter - radius, 2 * radius, 2 * radius);
}
2022-01-25 05:35:47 +00:00
/**
* Download the QR code as a PNG
*/
2022-01-23 03:22:52 +00:00
function download() {
// Download image
if (!canvas) {
alert("Error: no QR code to download");
return;
}
2022-01-23 03:42:10 +00:00
var link = document.getElementById("link");
link.setAttribute("download", "qr_image.png");
link.setAttribute(
"href",
canvas.toDataURL("image/png").replace("image/png", "image/octet-stream")
);
2022-01-23 03:22:52 +00:00
link.click();
}
2022-01-25 05:35:47 +00:00
/**
* Make the QR code
*/
2022-01-22 19:05:38 +00:00
function makeCode() {
// Grab url input
elementText = document.getElementById("text");
2022-01-22 19:05:38 +00:00
url = elementText.value;
// Check for non-empty url
if (!url) {
alert("Error: empty input");
elementText.focus();
return;
}
2022-01-25 05:36:37 +00:00
// // Pad URL since we want more density
// maxLength = 40;
// if (url.length < maxLength) {
// url += "?/" + "0".repeat(maxLength - url.length);
// }
2022-01-22 19:05:38 +00:00
// Generate URL bits
qrcode.makeCode(url);
// Manually draw canvas
QRMatrix = qrcode._oQRCode.modules;
2022-01-22 19:05:38 +00:00
QRLength = QRMatrix.length;
// Form canvas
canvas = document.getElementById("myCanvas");
ctx = canvas.getContext("2d");
2022-01-22 19:05:38 +00:00
// QR code sizing
bitLength = 10;
2022-01-23 17:45:15 +00:00
canvasLength = bitLength * (QRLength + borderSizeValue * 2);
2022-01-22 19:05:38 +00:00
canvas.width = canvasLength;
canvas.height = canvasLength;
2022-01-22 19:05:38 +00:00
// Set background of canvas
2022-01-25 05:35:47 +00:00
if (document.getElementById("whitebackground").checked) {
ctx.fillStyle = "#FFFFFF";
ctx.fillRect(0, 0, canvasLength, canvasLength);
}
// Set image of code
2022-01-23 03:22:52 +00:00
if (img) {
2022-01-23 03:42:10 +00:00
ctx.drawImage(
img,
2022-01-25 01:20:42 +00:00
bitLength * borderSizeValue,
bitLength * borderSizeValue,
2022-01-23 03:42:10 +00:00
bitLength * QRLength,
bitLength * QRLength
);
2022-01-23 03:22:52 +00:00
}
2022-01-22 19:05:38 +00:00
// Colors of true and false bits
black = "#000000";
2022-01-22 19:05:38 +00:00
white = "#FFFFFF";
2022-01-22 19:05:38 +00:00
// Populate canvas with bits
for (let i = 0; i < QRLength; i++) {
for (let j = 0; j < QRLength; j++) {
if (QRMatrix[i][j]) {
ctx.fillStyle = black;
} else {
ctx.fillStyle = white;
}
drawShape(
ctx,
2022-01-23 17:45:15 +00:00
j + borderSizeValue,
2022-02-03 01:42:32 +00:00
i + borderSizeValue,
bitLength,
radiusRatio,
QRLength
);
}
}
}