Moved QrCode.toSvgString() out of the library and into the runnable demo program, slightly adapted some code, updated documentation comments.

pull/134/head
Project Nayuki 3 years ago
parent 2515a4213c
commit 1954c534bf

@ -18,7 +18,7 @@ Features
Core features: Core features:
* Supports encoding all 40 versions (sizes) and all 4 error correction levels, as per the QR Code Model 2 standard * Supports encoding all 40 versions (sizes) and all 4 error correction levels, as per the QR Code Model 2 standard
* Output formats: Raw modules/pixels of the QR symbol, SVG XML string * Output format: Raw modules/pixels of the QR symbol
* Encodes numeric and special-alphanumeric text in less space than general text * Encodes numeric and special-alphanumeric text in less space than general text
* Encodes Japanese Unicode text in kanji mode to save a lot of space compared to UTF-8 bytes * Encodes Japanese Unicode text in kanji mode to save a lot of space compared to UTF-8 bytes
* Computes optimal segment mode switching for text with mixed numeric/alphanumeric/general/kanji parts * Computes optimal segment mode switching for text with mixed numeric/alphanumeric/general/kanji parts

@ -277,40 +277,6 @@ public final class QrCode {
} }
/**
* Returns a string of SVG code for an image depicting this QR Code, with the specified number
* of border modules. The string always uses Unix newlines (\n), regardless of the platform.
* @param border the number of border modules to add, which must be non-negative
* @return a string representing this QR Code as an SVG XML document
* @throws IllegalArgumentException if the border is negative
*/
public String toSvgString(int border) {
if (border < 0)
throw new IllegalArgumentException("Border must be non-negative");
long brd = border;
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\" version=\"1.1\" viewBox=\"0 0 %1$d %1$d\" stroke=\"none\">\n",
size + brd * 2))
.append("\t<rect width=\"100%\" height=\"100%\" fill=\"#FFFFFF\"/>\n")
.append("\t<path d=\"");
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
if (getModule(x, y)) {
if (x != 0 || y != 0)
sb.append(" ");
sb.append(String.format("M%d,%dh1v1h-1z", x + brd, y + brd));
}
}
}
return sb
.append("\" fill=\"#000000\"/>\n")
.append("</svg>\n")
.toString();
}
/*---- Private helper methods for constructor: Drawing function modules ----*/ /*---- Private helper methods for constructor: Drawing function modules ----*/

@ -62,7 +62,7 @@ public final class QrCodeGeneratorDemo {
File imgFile = new File("hello-world-QR.png"); // File path for output File imgFile = new File("hello-world-QR.png"); // File path for output
ImageIO.write(img, "png", imgFile); // Write image to file ImageIO.write(img, "png", imgFile); // Write image to file
String svg = qr.toSvgString(4); // Convert to SVG XML code String svg = toSvgString(qr, 4); // Convert to SVG XML code
File svgFile = new File("hello-world-QR.svg"); // File path for output File svgFile = new File("hello-world-QR.svg"); // File path for output
Files.write(svgFile.toPath(), // Write image to file Files.write(svgFile.toPath(), // Write image to file
svg.getBytes(StandardCharsets.UTF_8)); svg.getBytes(StandardCharsets.UTF_8));
@ -215,4 +215,41 @@ public final class QrCodeGeneratorDemo {
ImageIO.write(img, "png", new File(filepath)); ImageIO.write(img, "png", new File(filepath));
} }
/**
* 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
* @return a string representing the QR Code as an SVG XML document
* @throws NullPointerException if the QR Code is {@code null}
* @throws IllegalArgumentException if the border is negative
*/
private static String toSvgString(QrCode qr, int border) {
Objects.requireNonNull(qr);
if (border < 0)
throw new IllegalArgumentException("Border must be non-negative");
long brd = border;
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\" version=\"1.1\" viewBox=\"0 0 %1$d %1$d\" stroke=\"none\">\n",
qr.size + brd * 2))
.append("\t<rect width=\"100%\" height=\"100%\" fill=\"#FFFFFF\"/>\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 + brd, y + brd));
}
}
}
return sb
.append("\" fill=\"#000000\"/>\n")
.append("</svg>\n")
.toString();
}
} }

@ -9,7 +9,7 @@
* <p>Core features:</p> * <p>Core features:</p>
* <ul> * <ul>
* <li><p>Supports encoding all 40 versions (sizes) and all 4 error correction levels, as per the QR Code Model 2 standard</p></li> * <li><p>Supports encoding all 40 versions (sizes) and all 4 error correction levels, as per the QR Code Model 2 standard</p></li>
* <li><p>Output formats: Raw modules/pixels of the QR symbol, SVG XML string</p></li> * <li><p>Output format: Raw modules/pixels of the QR symbol</p></li>
* <li><p>Encodes numeric and special-alphanumeric text in less space than general text</p></li> * <li><p>Encodes numeric and special-alphanumeric text in less space than general text</p></li>
* <li><p>Encodes Japanese Unicode text in kanji mode to save a lot of space compared to UTF-8 bytes</p></li> * <li><p>Encodes Japanese Unicode text in kanji mode to save a lot of space compared to UTF-8 bytes</p></li>
* <li><p>Computes optimal segment mode switching for text with mixed numeric/alphanumeric/general/kanji parts</p></li> * <li><p>Computes optimal segment mode switching for text with mixed numeric/alphanumeric/general/kanji parts</p></li>

Loading…
Cancel
Save