/* * QR Code generator library (Java) * * Copyright (c) Project Nayuki. (MIT License) * https://www.nayuki.io/page/qr-code-generator-library * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * - The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * - The Software is provided "as is", without warranty of any kind, express or * implied, including but not limited to the warranties of merchantability, * fitness for a particular purpose and noninfringement. In no event shall the * authors or copyright holders be liable for any claim, damages or other * liability, whether in an action of contract, tort or otherwise, arising from, * out of or in connection with the Software or the use or other dealings in the * Software. */ package io.nayuki.qrcodegen; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Objects; import java.util.regex.Pattern; /** * Represents a character string to be encoded in a QR Code symbol. Each segment has * a mode, and a sequence of characters that is already encoded as a sequence of bits. * Instances of this class are immutable. *
This segment class imposes no length restrictions, but QR Codes have restrictions. * Even in the most favorable conditions, a QR Code can only hold 7089 characters of data. * Any segment longer than this is meaningless for the purpose of generating QR Codes.
*/ public final class QrSegment { /*---- Static factory functions ----*/ /** * Returns a segment representing the specified binary data encoded in byte mode. * @param data the binary data * @return a segment containing the data * @throws NullPointerException if the array is {@code null} */ public static QrSegment makeBytes(byte[] data) { Objects.requireNonNull(data); return new QrSegment(Mode.BYTE, data.length, data, data.length * 8); } /** * Returns a segment representing the specified string of decimal digits encoded in numeric mode. * @param digits a string consisting of digits from 0 to 9 * @return a segment containing the data * @throws NullPointerException if the string is {@code null} * @throws IllegalArgumentException if the string contains non-digit characters */ public static QrSegment makeNumeric(String digits) { Objects.requireNonNull(digits); if (!NUMERIC_REGEX.matcher(digits).matches()) throw new IllegalArgumentException("String contains non-numeric characters"); BitBuffer bb = new BitBuffer(); int i; for (i = 0; i + 3 <= digits.length(); i += 3) // Process groups of 3 bb.appendBits(Integer.parseInt(digits.substring(i, i + 3)), 10); int rem = digits.length() - i; if (rem > 0) // 1 or 2 digits remaining bb.appendBits(Integer.parseInt(digits.substring(i)), rem * 3 + 1); return new QrSegment(Mode.NUMERIC, digits.length(), bb.getBytes(), bb.bitLength()); } /** * Returns a segment representing the specified text string encoded in alphanumeric mode. The characters allowed are: * 0 to 9, A to Z (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon. * @param text a string of text, with only certain characters allowed * @return a segment containing the data * @throws NullPointerException if the string is {@code null} * @throws IllegalArgumentException if the string contains non-encodable characters */ public static QrSegment makeAlphanumeric(String text) { Objects.requireNonNull(text); if (!ALPHANUMERIC_REGEX.matcher(text).matches()) throw new IllegalArgumentException("String contains unencodable characters in alphanumeric mode"); BitBuffer bb = new BitBuffer(); int i; for (i = 0; i + 2 <= text.length(); i += 2) { // Process groups of 2 int temp = ALPHANUMERIC_CHARSET.indexOf(text.charAt(i)) * 45; temp += ALPHANUMERIC_CHARSET.indexOf(text.charAt(i + 1)); bb.appendBits(temp, 11); } if (i < text.length()) // 1 character remaining bb.appendBits(ALPHANUMERIC_CHARSET.indexOf(text.charAt(i)), 6); return new QrSegment(Mode.ALPHANUMERIC, text.length(), bb.getBytes(), bb.bitLength()); } /** * Returns a new mutable list of zero or more segments to represent the specified Unicode text string. * The result may use various segment modes and switch modes to optimize the length of the bit stream. * @param text the text to be encoded, which can be any Unicode string * @return a list of segments containing the text * @throws NullPointerException if the text is {@code null} */ public static List