1 /* 2 * QR Code generator demo (Java) 3 * 4 * Run this command-line program with no arguments. The program creates/overwrites a bunch of 5 * PNG and SVG files in the current working directory to demonstrate the creation of QR Codes. 6 * 7 * Copyright (c) Project Nayuki. (MIT License) 8 * https://www.nayuki.io/page/qr-code-generator-library 9 * 10 * Permission is hereby granted, free of charge, to any person obtaining a copy of 11 * this software and associated documentation files (the "Software"), to deal in 12 * the Software without restriction, including without limitation the rights to 13 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 14 * the Software, and to permit persons to whom the Software is furnished to do so, 15 * subject to the following conditions: 16 * - The above copyright notice and this permission notice shall be included in 17 * all copies or substantial portions of the Software. 18 * - The Software is provided "as is", without warranty of any kind, express or 19 * implied, including but not limited to the warranties of merchantability, 20 * fitness for a particular purpose and noninfringement. In no event shall the 21 * authors or copyright holders be liable for any claim, damages or other 22 * liability, whether in an action of contract, tort or otherwise, arising from, 23 * out of or in connection with the Software or the use or other dealings in the 24 * Software. 25 */ 26 27 import java.awt.image.BufferedImage; 28 import java.io.File; 29 import java.io.IOException; 30 import java.nio.charset.StandardCharsets; 31 import java.nio.file.Files; 32 import java.util.Arrays; 33 import java.util.List; 34 import java.util.Objects; 35 import javax.imageio.ImageIO; 36 import io.nayuki.qrcodegen.QrCode; 37 import io.nayuki.qrcodegen.QrSegment; 38 import io.nayuki.qrcodegen.QrSegmentAdvanced; 39 40 41 public final class QrCodeGeneratorDemo { 42 43 // The main application program. main(String[] args)44 public static void main(String[] args) throws IOException { 45 doBasicDemo(); 46 doVarietyDemo(); 47 doSegmentDemo(); 48 doMaskDemo(); 49 } 50 51 52 53 /*---- Demo suite ----*/ 54 55 // Creates a single QR Code, then writes it to a PNG file and an SVG file. doBasicDemo()56 private static void doBasicDemo() throws IOException { 57 String text = "Hello, world!"; // User-supplied Unicode text 58 QrCode.Ecc errCorLvl = QrCode.Ecc.LOW; // Error correction level 59 60 QrCode qr = QrCode.encodeText(text, errCorLvl); // Make the QR Code symbol 61 62 BufferedImage img = toImage(qr, 10, 4); // Convert to bitmap image 63 File imgFile = new File("hello-world-QR.png"); // File path for output 64 ImageIO.write(img, "png", imgFile); // Write image to file 65 66 String svg = toSvgString(qr, 4, "#FFFFFF", "#000000"); // Convert to SVG XML code 67 File svgFile = new File("hello-world-QR.svg"); // File path for output 68 Files.write(svgFile.toPath(), // Write image to file 69 svg.getBytes(StandardCharsets.UTF_8)); 70 } 71 72 73 // Creates a variety of QR Codes that exercise different features of the library, and writes each one to file. doVarietyDemo()74 private static void doVarietyDemo() throws IOException { 75 QrCode qr; 76 77 // Numeric mode encoding (3.33 bits per digit) 78 qr = QrCode.encodeText("314159265358979323846264338327950288419716939937510", QrCode.Ecc.MEDIUM); 79 writePng(toImage(qr, 13, 1), "pi-digits-QR.png"); 80 81 // Alphanumeric mode encoding (5.5 bits per character) 82 qr = QrCode.encodeText("DOLLAR-AMOUNT:$39.87 PERCENTAGE:100.00% OPERATIONS:+-*/", QrCode.Ecc.HIGH); 83 writePng(toImage(qr, 10, 2), "alphanumeric-QR.png"); 84 85 // Unicode text as UTF-8 86 qr = QrCode.encodeText("こんにちwa、世界! αβγδ", QrCode.Ecc.QUARTILE); 87 writePng(toImage(qr, 10, 3), "unicode-QR.png"); 88 89 // Moderately large QR Code using longer text (from Lewis Carroll's Alice in Wonderland) 90 qr = QrCode.encodeText( 91 "Alice was beginning to get very tired of sitting by her sister on the bank, " 92 + "and of having nothing to do: once or twice she had peeped into the book her sister was reading, " 93 + "but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice " 94 + "'without pictures or conversations?' So she was considering in her own mind (as well as she could, " 95 + "for the hot day made her feel very sleepy and stupid), whether the pleasure of making a " 96 + "daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly " 97 + "a White Rabbit with pink eyes ran close by her.", QrCode.Ecc.HIGH); 98 writePng(toImage(qr, 6, 10), "alice-wonderland-QR.png"); 99 } 100 101 102 // Creates QR Codes with manually specified segments for better compactness. doSegmentDemo()103 private static void doSegmentDemo() throws IOException { 104 QrCode qr; 105 List<QrSegment> segs; 106 107 // Illustration "silver" 108 String silver0 = "THE SQUARE ROOT OF 2 IS 1."; 109 String silver1 = "41421356237309504880168872420969807856967187537694807317667973799"; 110 qr = QrCode.encodeText(silver0 + silver1, QrCode.Ecc.LOW); 111 writePng(toImage(qr, 10, 3), "sqrt2-monolithic-QR.png"); 112 113 segs = Arrays.asList( 114 QrSegment.makeAlphanumeric(silver0), 115 QrSegment.makeNumeric(silver1)); 116 qr = QrCode.encodeSegments(segs, QrCode.Ecc.LOW); 117 writePng(toImage(qr, 10, 3), "sqrt2-segmented-QR.png"); 118 119 // Illustration "golden" 120 String golden0 = "Golden ratio φ = 1."; 121 String golden1 = "6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374"; 122 String golden2 = "......"; 123 qr = QrCode.encodeText(golden0 + golden1 + golden2, QrCode.Ecc.LOW); 124 writePng(toImage(qr, 8, 5), "phi-monolithic-QR.png"); 125 126 segs = Arrays.asList( 127 QrSegment.makeBytes(golden0.getBytes(StandardCharsets.UTF_8)), 128 QrSegment.makeNumeric(golden1), 129 QrSegment.makeAlphanumeric(golden2)); 130 qr = QrCode.encodeSegments(segs, QrCode.Ecc.LOW); 131 writePng(toImage(qr, 8, 5), "phi-segmented-QR.png"); 132 133 // Illustration "Madoka": kanji, kana, Cyrillic, full-width Latin, Greek characters 134 String madoka = "「魔法少女まどか☆マギカ」って、 ИАИ desu κα?"; 135 qr = QrCode.encodeText(madoka, QrCode.Ecc.LOW); 136 writePng(toImage(qr, 9, 4, 0xFFFFE0, 0x303080), "madoka-utf8-QR.png"); 137 138 segs = Arrays.asList(QrSegmentAdvanced.makeKanji(madoka)); 139 qr = QrCode.encodeSegments(segs, QrCode.Ecc.LOW); 140 writePng(toImage(qr, 9, 4, 0xE0F0FF, 0x404040), "madoka-kanji-QR.png"); 141 } 142 143 144 // Creates QR Codes with the same size and contents but different mask patterns. doMaskDemo()145 private static void doMaskDemo() throws IOException { 146 QrCode qr; 147 List<QrSegment> segs; 148 149 // Project Nayuki URL 150 segs = QrSegment.makeSegments("https://www.nayuki.io/"); 151 qr = QrCode.encodeSegments(segs, QrCode.Ecc.HIGH, QrCode.MIN_VERSION, QrCode.MAX_VERSION, -1, true); // Automatic mask 152 writePng(toImage(qr, 8, 6, 0xE0FFE0, 0x206020), "project-nayuki-automask-QR.png"); 153 qr = QrCode.encodeSegments(segs, QrCode.Ecc.HIGH, QrCode.MIN_VERSION, QrCode.MAX_VERSION, 3, true); // Force mask 3 154 writePng(toImage(qr, 8, 6, 0xFFE0E0, 0x602020), "project-nayuki-mask3-QR.png"); 155 156 // Chinese text as UTF-8 157 segs = QrSegment.makeSegments("維基百科(Wikipedia,聆聽i/ˌwɪkᵻˈpiːdi.ə/)是一個自由內容、公開編輯且多語言的網路百科全書協作計畫"); 158 qr = QrCode.encodeSegments(segs, QrCode.Ecc.MEDIUM, QrCode.MIN_VERSION, QrCode.MAX_VERSION, 0, true); // Force mask 0 159 writePng(toImage(qr, 10, 3), "unicode-mask0-QR.png"); 160 qr = QrCode.encodeSegments(segs, QrCode.Ecc.MEDIUM, QrCode.MIN_VERSION, QrCode.MAX_VERSION, 1, true); // Force mask 1 161 writePng(toImage(qr, 10, 3), "unicode-mask1-QR.png"); 162 qr = QrCode.encodeSegments(segs, QrCode.Ecc.MEDIUM, QrCode.MIN_VERSION, QrCode.MAX_VERSION, 5, true); // Force mask 5 163 writePng(toImage(qr, 10, 3), "unicode-mask5-QR.png"); 164 qr = QrCode.encodeSegments(segs, QrCode.Ecc.MEDIUM, QrCode.MIN_VERSION, QrCode.MAX_VERSION, 7, true); // Force mask 7 165 writePng(toImage(qr, 10, 3), "unicode-mask7-QR.png"); 166 } 167 168 169 170 /*---- Utilities ----*/ 171 toImage(QrCode qr, int scale, int border)172 private static BufferedImage toImage(QrCode qr, int scale, int border) { 173 return toImage(qr, scale, border, 0xFFFFFF, 0x000000); 174 } 175 176 177 /** 178 * Returns a raster image depicting the specified QR Code, with 179 * the specified module scale, border modules, and module colors. 180 * <p>For example, scale=10 and border=4 means to pad the QR Code with 4 light border 181 * modules on all four sides, and use 10×10 pixels to represent each module. 182 * @param qr the QR Code to render (not {@code null}) 183 * @param scale the side length (measured in pixels, must be positive) of each module 184 * @param border the number of border modules to add, which must be non-negative 185 * @param lightColor the color to use for light modules, in 0xRRGGBB format 186 * @param darkColor the color to use for dark modules, in 0xRRGGBB format 187 * @return a new image representing the QR Code, with padding and scaling 188 * @throws NullPointerException if the QR Code is {@code null} 189 * @throws IllegalArgumentException if the scale or border is out of range, or if 190 * {scale, border, size} cause the image dimensions to exceed Integer.MAX_VALUE 191 */ toImage(QrCode qr, int scale, int border, int lightColor, int darkColor)192 private static BufferedImage toImage(QrCode qr, int scale, int border, int lightColor, int darkColor) { 193 Objects.requireNonNull(qr); 194 if (scale <= 0 || border < 0) 195 throw new IllegalArgumentException("Value out of range"); 196 if (border > Integer.MAX_VALUE / 2 || qr.size + border * 2L > Integer.MAX_VALUE / scale) 197 throw new IllegalArgumentException("Scale or border too large"); 198 199 BufferedImage result = new BufferedImage((qr.size + border * 2) * scale, (qr.size + border * 2) * scale, BufferedImage.TYPE_INT_RGB); 200 for (int y = 0; y < result.getHeight(); y++) { 201 for (int x = 0; x < result.getWidth(); x++) { 202 boolean color = qr.getModule(x / scale - border, y / scale - border); 203 result.setRGB(x, y, color ? darkColor : lightColor); 204 } 205 } 206 return result; 207 } 208 209 210 // Helper function to reduce code duplication. writePng(BufferedImage img, String filepath)211 private static void writePng(BufferedImage img, String filepath) throws IOException { 212 ImageIO.write(img, "png", new File(filepath)); 213 } 214 215 216 /** 217 * Returns a string of SVG code for an image depicting the specified QR Code, with the specified 218 * number of border modules. The string always uses Unix newlines (\n), regardless of the platform. 219 * @param qr the QR Code to render (not {@code null}) 220 * @param border the number of border modules to add, which must be non-negative 221 * @param lightColor the color to use for light modules, in any format supported by CSS, not {@code null} 222 * @param darkColor the color to use for dark modules, in any format supported by CSS, not {@code null} 223 * @return a string representing the QR Code as an SVG XML document 224 * @throws NullPointerException if any object is {@code null} 225 * @throws IllegalArgumentException if the border is negative 226 */ toSvgString(QrCode qr, int border, String lightColor, String darkColor)227 private static String toSvgString(QrCode qr, int border, String lightColor, String darkColor) { 228 Objects.requireNonNull(qr); 229 Objects.requireNonNull(lightColor); 230 Objects.requireNonNull(darkColor); 231 if (border < 0) 232 throw new IllegalArgumentException("Border must be non-negative"); 233 long brd = border; 234 StringBuilder sb = new StringBuilder() 235 .append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") 236 .append("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n") 237 .append(String.format("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" viewBox=\"0 0 %1$d %1$d\" stroke=\"none\">\n", 238 qr.size + brd * 2)) 239 .append("\t<rect width=\"100%\" height=\"100%\" fill=\"" + lightColor + "\"/>\n") 240 .append("\t<path d=\""); 241 for (int y = 0; y < qr.size; y++) { 242 for (int x = 0; x < qr.size; x++) { 243 if (qr.getModule(x, y)) { 244 if (x != 0 || y != 0) 245 sb.append(" "); 246 sb.append(String.format("M%d,%dh1v1h-1z", x + brd, y + brd)); 247 } 248 } 249 } 250 return sb 251 .append("\" fill=\"" + darkColor + "\"/>\n") 252 .append("</svg>\n") 253 .toString(); 254 } 255 256 } 257