Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / examples / common / QRCode / repo / java / src / main / java / io / nayuki / qrcodegen / QrCodeGeneratorDemo.java
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 package io.nayuki.qrcodegen;
28
29 import java.awt.image.BufferedImage;
30 import java.io.File;
31 import java.io.IOException;
32 import java.nio.charset.StandardCharsets;
33 import java.nio.file.Files;
34 import java.util.Arrays;
35 import java.util.List;
36 import javax.imageio.ImageIO;
37
38
39 public final class QrCodeGeneratorDemo {
40         
41         // The main application program.
42         public static void main(String[] args) throws IOException {
43                 doBasicDemo();
44                 doVarietyDemo();
45                 doSegmentDemo();
46                 doMaskDemo();
47         }
48         
49         
50         
51         /*---- Demo suite ----*/
52         
53         // Creates a single QR Code, then writes it to a PNG file and an SVG file.
54         private static void doBasicDemo() throws IOException {
55                 String text = "Hello, world!";          // User-supplied Unicode text
56                 QrCode.Ecc errCorLvl = QrCode.Ecc.LOW;  // Error correction level
57                 
58                 QrCode qr = QrCode.encodeText(text, errCorLvl);  // Make the QR Code symbol
59                 
60                 BufferedImage img = qr.toImage(10, 4);           // Convert to bitmap image
61                 File imgFile = new File("hello-world-QR.png");   // File path for output
62                 ImageIO.write(img, "png", imgFile);              // Write image to file
63                 
64                 String svg = qr.toSvgString(4);                  // Convert to SVG XML code
65                 File svgFile = new File("hello-world-QR.svg");   // File path for output
66                 Files.write(svgFile.toPath(),                    // Write image to file
67                         svg.getBytes(StandardCharsets.UTF_8));
68         }
69         
70         
71         // Creates a variety of QR Codes that exercise different features of the library, and writes each one to file.
72         private static void doVarietyDemo() throws IOException {
73                 QrCode qr;
74                 
75                 // Numeric mode encoding (3.33 bits per digit)
76                 qr = QrCode.encodeText("314159265358979323846264338327950288419716939937510", QrCode.Ecc.MEDIUM);
77                 writePng(qr.toImage(13, 1), "pi-digits-QR.png");
78                 
79                 // Alphanumeric mode encoding (5.5 bits per character)
80                 qr = QrCode.encodeText("DOLLAR-AMOUNT:$39.87 PERCENTAGE:100.00% OPERATIONS:+-*/", QrCode.Ecc.HIGH);
81                 writePng(qr.toImage(10, 2), "alphanumeric-QR.png");
82                 
83                 // Unicode text as UTF-8
84                 qr = QrCode.encodeText("こんにちwa、世界! αβγδ", QrCode.Ecc.QUARTILE);
85                 writePng(qr.toImage(10, 3), "unicode-QR.png");
86                 
87                 // Moderately large QR Code using longer text (from Lewis Carroll's Alice in Wonderland)
88                 qr = QrCode.encodeText(
89                         "Alice was beginning to get very tired of sitting by her sister on the bank, "
90                         + "and of having nothing to do: once or twice she had peeped into the book her sister was reading, "
91                         + "but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice "
92                         + "'without pictures or conversations?' So she was considering in her own mind (as well as she could, "
93                         + "for the hot day made her feel very sleepy and stupid), whether the pleasure of making a "
94                         + "daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly "
95                         + "a White Rabbit with pink eyes ran close by her.", QrCode.Ecc.HIGH);
96                 writePng(qr.toImage(6, 10), "alice-wonderland-QR.png");
97         }
98         
99         
100         // Creates QR Codes with manually specified segments for better compactness.
101         private static void doSegmentDemo() throws IOException {
102                 QrCode qr;
103                 List<QrSegment> segs;
104                 
105                 // Illustration "silver"
106                 String silver0 = "THE SQUARE ROOT OF 2 IS 1.";
107                 String silver1 = "41421356237309504880168872420969807856967187537694807317667973799";
108                 qr = QrCode.encodeText(silver0 + silver1, QrCode.Ecc.LOW);
109                 writePng(qr.toImage(10, 3), "sqrt2-monolithic-QR.png");
110                 
111                 segs = Arrays.asList(
112                         QrSegment.makeAlphanumeric(silver0),
113                         QrSegment.makeNumeric(silver1));
114                 qr = QrCode.encodeSegments(segs, QrCode.Ecc.LOW);
115                 writePng(qr.toImage(10, 3), "sqrt2-segmented-QR.png");
116                 
117                 // Illustration "golden"
118                 String golden0 = "Golden ratio φ = 1.";
119                 String golden1 = "6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374";
120                 String golden2 = "......";
121                 qr = QrCode.encodeText(golden0 + golden1 + golden2, QrCode.Ecc.LOW);
122                 writePng(qr.toImage(8, 5), "phi-monolithic-QR.png");
123                 
124                 segs = Arrays.asList(
125                         QrSegment.makeBytes(golden0.getBytes(StandardCharsets.UTF_8)),
126                         QrSegment.makeNumeric(golden1),
127                         QrSegment.makeAlphanumeric(golden2));
128                 qr = QrCode.encodeSegments(segs, QrCode.Ecc.LOW);
129                 writePng(qr.toImage(8, 5), "phi-segmented-QR.png");
130                 
131                 // Illustration "Madoka": kanji, kana, Cyrillic, full-width Latin, Greek characters
132                 String madoka = "「魔法少女まどか☆マギカ」って、 ИАИ desu κα?";
133                 qr = QrCode.encodeText(madoka, QrCode.Ecc.LOW);
134                 writePng(qr.toImage(9, 4), "madoka-utf8-QR.png");
135                 
136                 segs = Arrays.asList(QrSegmentAdvanced.makeKanji(madoka));
137                 qr = QrCode.encodeSegments(segs, QrCode.Ecc.LOW);
138                 writePng(qr.toImage(9, 4), "madoka-kanji-QR.png");
139         }
140         
141         
142         // Creates QR Codes with the same size and contents but different mask patterns.
143         private static void doMaskDemo() throws IOException {
144                 QrCode qr;
145                 List<QrSegment> segs;
146                 
147                 // Project Nayuki URL
148                 segs = QrSegment.makeSegments("https://www.nayuki.io/");
149                 qr = QrCode.encodeSegments(segs, QrCode.Ecc.HIGH, QrCode.MIN_VERSION, QrCode.MAX_VERSION, -1, true);  // Automatic mask
150                 writePng(qr.toImage(8, 6), "project-nayuki-automask-QR.png");
151                 qr = QrCode.encodeSegments(segs, QrCode.Ecc.HIGH, QrCode.MIN_VERSION, QrCode.MAX_VERSION, 3, true);  // Force mask 3
152                 writePng(qr.toImage(8, 6), "project-nayuki-mask3-QR.png");
153                 
154                 // Chinese text as UTF-8
155                 segs = QrSegment.makeSegments("維基百科(Wikipedia,聆聽i/ˌwɪkᵻˈpiːdi.ə/)是一個自由內容、公開編輯且多語言的網路百科全書協作計畫");
156                 qr = QrCode.encodeSegments(segs, QrCode.Ecc.MEDIUM, QrCode.MIN_VERSION, QrCode.MAX_VERSION, 0, true);  // Force mask 0
157                 writePng(qr.toImage(10, 3), "unicode-mask0-QR.png");
158                 qr = QrCode.encodeSegments(segs, QrCode.Ecc.MEDIUM, QrCode.MIN_VERSION, QrCode.MAX_VERSION, 1, true);  // Force mask 1
159                 writePng(qr.toImage(10, 3), "unicode-mask1-QR.png");
160                 qr = QrCode.encodeSegments(segs, QrCode.Ecc.MEDIUM, QrCode.MIN_VERSION, QrCode.MAX_VERSION, 5, true);  // Force mask 5
161                 writePng(qr.toImage(10, 3), "unicode-mask5-QR.png");
162                 qr = QrCode.encodeSegments(segs, QrCode.Ecc.MEDIUM, QrCode.MIN_VERSION, QrCode.MAX_VERSION, 7, true);  // Force mask 7
163                 writePng(qr.toImage(10, 3), "unicode-mask7-QR.png");
164         }
165         
166         
167         
168         /*---- Utilities ----*/
169         
170         // Helper function to reduce code duplication.
171         private static void writePng(BufferedImage img, String filepath) throws IOException {
172                 ImageIO.write(img, "png", new File(filepath));
173         }
174         
175 }