QrCodeGeneratorDemo.java 12 KB

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