-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Why: - Continuing the migration from SPA to SSR. - Evaluated multiple QR code generator libraries for Java, before ending up using Naoyuki: - QRGen https://github.com/kenglxn/QRGen - π Produces a 20px border around the SVG, no option to remove it - π Generates very verbose SVG - π Not available in Maven Central - π Brings it lots of transitive dependencies - Okapi Barcode https://github.com/woo-j/OkapiBarcode - π Doesn't set the SVG viewbox - π Generates verbose SVG - Nayuki's QR Code generator https://github.com/nayuki/QR-Code-generator - π Generates dense SVG - π Requires hand-writing the image/SVG generator. We had to copy-paste and adapt the examples from https://github.com/nayuki/QR-Code-generator/blob/master/java/QrCodeGeneratorDemo.java - π‘ The qrcode.react library we've used previously is based on this - π Zero dependencies - A few other Java and Clojure libraries were found as well, but they didn't seem very well maintained. - To avoid generating lots of QR codes which will never be used, they will be cached for some time. Previously they were cached in the user's session, but HTMX makes it easy to cache them in the browser's request cache.
- Loading branch information
Showing
8 changed files
with
102 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright Β© 2015-2024 Esko Luontola | ||
// This software is released under the Apache License 2.0. | ||
// The license text is at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
package territory_bro; | ||
|
||
import io.nayuki.qrcodegen.QrCode; | ||
|
||
import java.util.Objects; | ||
|
||
public class QrCodeGenerator { | ||
|
||
// Adapted from https://github.com/nayuki/QR-Code-generator/blob/master/java/QrCodeGeneratorDemo.java | ||
|
||
/** | ||
* Returns a string of SVG code for an image depicting the specified QR Code, with the specified | ||
* number of border modules. The string always uses Unix newlines (\n), regardless of the platform. | ||
* | ||
* @param qr the QR Code to render (not {@code null}) | ||
* @param border the number of border modules to add, which must be non-negative | ||
* @param lightColor the color to use for light modules, in any format supported by CSS, not {@code null} | ||
* @param darkColor the color to use for dark modules, in any format supported by CSS, not {@code null} | ||
* @return a string representing the QR Code as an SVG XML document | ||
* @throws NullPointerException if any object is {@code null} | ||
* @throws IllegalArgumentException if the border is negative | ||
*/ | ||
public static String toSvgString(QrCode qr, int border, String lightColor, String darkColor) { | ||
Objects.requireNonNull(qr); | ||
Objects.requireNonNull(lightColor); | ||
Objects.requireNonNull(darkColor); | ||
if (border < 0) { | ||
throw new IllegalArgumentException("Border must be non-negative"); | ||
} | ||
StringBuilder sb = new StringBuilder() | ||
// .append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") | ||
// .append("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n") | ||
.append(String.format("<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 %1$d %1$d\" stroke=\"none\">\n", | ||
qr.size + border * 2)) | ||
.append("\t<rect width=\"100%\" height=\"100%\" fill=\"").append(lightColor).append("\" shape-rendering=\"crispEdges\"/>\n") | ||
.append("\t<path d=\""); | ||
for (int y = 0; y < qr.size; y++) { | ||
for (int x = 0; x < qr.size; x++) { | ||
if (qr.getModule(x, y)) { | ||
if (!(x == 0 && y == 0)) { | ||
sb.append(" "); | ||
} | ||
sb.append(String.format("M%d,%dh1v1h-1z", x + border, y + border)); | ||
} | ||
} | ||
} | ||
return sb.append("\" fill=\"").append(darkColor).append("\" shape-rendering=\"crispEdges\"/>\n") | ||
.append("</svg>\n") | ||
.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters