Added HTML elements and JavaScript demo code to support QR Code output as an SVG vector image.
This commit is contained in:
parent
fc705efac3
commit
549f4bb38b
|
@ -26,6 +26,26 @@
|
||||||
|
|
||||||
|
|
||||||
function redrawQrCode() {
|
function redrawQrCode() {
|
||||||
|
// Show/hide rows based on bitmap/vector image output
|
||||||
|
var bitmapOutput = document.getElementById("output-format-bitmap").checked;
|
||||||
|
var scaleRow = document.getElementById("scale-row");
|
||||||
|
var svgXmlRow = document.getElementById("svg-xml-row");
|
||||||
|
if (bitmapOutput) {
|
||||||
|
scaleRow.style.removeProperty("display");
|
||||||
|
svgXmlRow.style.display = "none";
|
||||||
|
} else {
|
||||||
|
scaleRow.style.display = "none";
|
||||||
|
svgXmlRow.style.removeProperty("display");
|
||||||
|
}
|
||||||
|
var svgXml = document.getElementById("svg-xml-output");
|
||||||
|
svgXml.value = "";
|
||||||
|
|
||||||
|
// Reset output images in case of early termination
|
||||||
|
var canvas = document.getElementById("qrcode-canvas");
|
||||||
|
var svg = document.getElementById("qrcode-svg");
|
||||||
|
canvas.style.display = "none";
|
||||||
|
svg.style.display = "none";
|
||||||
|
|
||||||
// Returns a QrCode.Ecc object based on the radio buttons in the HTML form.
|
// Returns a QrCode.Ecc object based on the radio buttons in the HTML form.
|
||||||
function getInputErrorCorrectionLevel() {
|
function getInputErrorCorrectionLevel() {
|
||||||
if (document.getElementById("errcorlvl-medium").checked)
|
if (document.getElementById("errcorlvl-medium").checked)
|
||||||
|
@ -38,11 +58,6 @@ function redrawQrCode() {
|
||||||
return qrcodegen.QrCode.Ecc.LOW;
|
return qrcodegen.QrCode.Ecc.LOW;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset canvas in case of early termination
|
|
||||||
var canvas = document.getElementById("qrcode-canvas");
|
|
||||||
canvas.width = 0;
|
|
||||||
canvas.height = 0;
|
|
||||||
|
|
||||||
// Get form inputs and compute QR Code
|
// Get form inputs and compute QR Code
|
||||||
var ecl = getInputErrorCorrectionLevel();
|
var ecl = getInputErrorCorrectionLevel();
|
||||||
var text = document.getElementById("text-input").value;
|
var text = document.getElementById("text-input").value;
|
||||||
|
@ -53,12 +68,14 @@ function redrawQrCode() {
|
||||||
var boostEcc = document.getElementById("boost-ecc-input").checked;
|
var boostEcc = document.getElementById("boost-ecc-input").checked;
|
||||||
var qr = qrcodegen.QrCode.encodeSegments(segs, ecl, minVer, maxVer, mask, boostEcc);
|
var qr = qrcodegen.QrCode.encodeSegments(segs, ecl, minVer, maxVer, mask, boostEcc);
|
||||||
|
|
||||||
// Get scale and border
|
// Draw image output
|
||||||
var scale = parseInt(document.getElementById("scale-input").value, 10);
|
|
||||||
var border = parseInt(document.getElementById("border-input").value, 10);
|
var border = parseInt(document.getElementById("border-input").value, 10);
|
||||||
if (scale <= 0 || border < 0 || scale > 30 || border > 100)
|
if (border < 0 || border > 100)
|
||||||
|
return;
|
||||||
|
if (bitmapOutput) {
|
||||||
|
var scale = parseInt(document.getElementById("scale-input").value, 10);
|
||||||
|
if (scale <= 0 || scale > 30)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Draw QR Code onto canvas
|
// Draw QR Code onto canvas
|
||||||
var width = (qr.getSize() + border * 2) * scale;
|
var width = (qr.getSize() + border * 2) * scale;
|
||||||
canvas.width = width;
|
canvas.width = width;
|
||||||
|
@ -70,6 +87,14 @@ function redrawQrCode() {
|
||||||
ctx.fillRect((x + border) * scale, (y + border) * scale, scale, scale);
|
ctx.fillRect((x + border) * scale, (y + border) * scale, scale, scale);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
canvas.style.removeProperty("display");
|
||||||
|
} else {
|
||||||
|
var code = qr.toSvgString(border);
|
||||||
|
svg.setAttribute("viewBox", / viewBox="([^"]*)"/.exec(code)[1]);
|
||||||
|
svg.querySelector("path").setAttribute("d", / d="([^"]*)"/.exec(code)[1]);
|
||||||
|
svg.style.removeProperty("display");
|
||||||
|
svgXml.value = qr.toSvgString(border);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Returns a string to describe the given list of segments.
|
// Returns a string to describe the given list of segments.
|
||||||
|
|
|
@ -60,13 +60,20 @@
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>Scale:</td>
|
<td>Output format:</td>
|
||||||
<td><input type="number" value="8" min="1" max="30" step="1" id="scale-input" style="width:4em" oninput="redrawQrCode();"> pixels per module</td>
|
<td>
|
||||||
|
<input type="radio" name="output-format" id="output-format-bitmap" onchange="redrawQrCode();" checked="checked"><label for="output-format-bitmap">Bitmap</label>
|
||||||
|
<input type="radio" name="output-format" id="output-format-vector" onchange="redrawQrCode();"><label for="output-format-vector">Vector</label>
|
||||||
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>Border:</td>
|
<td>Border:</td>
|
||||||
<td><input type="number" value="4" min="0" max="100" step="1" id="border-input" style="width:4em" oninput="redrawQrCode();"> modules</td>
|
<td><input type="number" value="4" min="0" max="100" step="1" id="border-input" style="width:4em" oninput="redrawQrCode();"> modules</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
<tr id="scale-row">
|
||||||
|
<td>Scale:</td>
|
||||||
|
<td><input type="number" value="8" min="1" max="30" step="1" id="scale-input" style="width:4em" oninput="redrawQrCode();"> pixels per module</td>
|
||||||
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>Statistics:</td>
|
<td>Statistics:</td>
|
||||||
<td id="statistics-output" style="white-space:pre"></td>
|
<td id="statistics-output" style="white-space:pre"></td>
|
||||||
|
@ -75,6 +82,16 @@
|
||||||
<td>QR Code:</td>
|
<td>QR Code:</td>
|
||||||
<td>
|
<td>
|
||||||
<canvas id="qrcode-canvas" style="padding:1em; background-color:#E8E8E8"></canvas>
|
<canvas id="qrcode-canvas" style="padding:1em; background-color:#E8E8E8"></canvas>
|
||||||
|
<svg id="qrcode-svg" style="width:30em; height:30em; padding:1em; background-color:#E8E8E8">
|
||||||
|
<rect width="100%" height="100%" fill="#FFFFFF" stroke-width="0"></rect>
|
||||||
|
<path d="" fill="#000000" stroke-width="0"></path>
|
||||||
|
</svg>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr id="svg-xml-row">
|
||||||
|
<td>SVG XML code:</td>
|
||||||
|
<td>
|
||||||
|
<textarea id="svg-xml-output" readonly="readonly" style="width:100%; max-width:50em; height:15em; font-family:monospace"></textarea>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
|
|
Loading…
Reference in New Issue