2016-04-10 06:11:57 +00:00
|
|
|
/*
|
|
|
|
* QR Code generator library (Java)
|
|
|
|
*
|
2017-04-24 20:41:54 +00:00
|
|
|
* Copyright (c) Project Nayuki. (MIT License)
|
2016-04-10 06:11:57 +00:00
|
|
|
* 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;
|
|
|
|
|
2016-04-16 01:44:24 +00:00
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2017-01-25 01:13:57 +00:00
|
|
|
import java.util.Objects;
|
2016-04-10 06:11:57 +00:00
|
|
|
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.
|
|
|
|
* <p>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.</p>
|
|
|
|
*/
|
|
|
|
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) {
|
2017-01-25 01:13:57 +00:00
|
|
|
Objects.requireNonNull(data);
|
2017-08-17 20:28:28 +00:00
|
|
|
BitBuffer bb = new BitBuffer();
|
|
|
|
for (byte b : data)
|
|
|
|
bb.appendBits(b & 0xFF, 8);
|
|
|
|
return new QrSegment(Mode.BYTE, data.length, bb);
|
2016-04-10 06:11:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2017-01-25 01:13:57 +00:00
|
|
|
Objects.requireNonNull(digits);
|
2016-04-10 06:11:57 +00:00
|
|
|
if (!NUMERIC_REGEX.matcher(digits).matches())
|
|
|
|
throw new IllegalArgumentException("String contains non-numeric characters");
|
|
|
|
|
|
|
|
BitBuffer bb = new BitBuffer();
|
|
|
|
int i;
|
2018-08-22 17:57:40 +00:00
|
|
|
for (i = 0; i <= digits.length() - 3; i += 3) // Process groups of 3
|
2016-04-10 06:11:57 +00:00
|
|
|
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);
|
2017-08-17 20:28:28 +00:00
|
|
|
return new QrSegment(Mode.NUMERIC, digits.length(), bb);
|
2016-04-10 06:11:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2017-08-31 20:13:05 +00:00
|
|
|
* 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.
|
2016-04-10 06:11:57 +00:00
|
|
|
* @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) {
|
2017-01-25 01:13:57 +00:00
|
|
|
Objects.requireNonNull(text);
|
2016-04-10 06:11:57 +00:00
|
|
|
if (!ALPHANUMERIC_REGEX.matcher(text).matches())
|
|
|
|
throw new IllegalArgumentException("String contains unencodable characters in alphanumeric mode");
|
|
|
|
|
|
|
|
BitBuffer bb = new BitBuffer();
|
|
|
|
int i;
|
2018-08-22 17:57:40 +00:00
|
|
|
for (i = 0; i <= text.length() - 2; i += 2) { // Process groups of 2
|
2017-05-08 06:20:50 +00:00
|
|
|
int temp = ALPHANUMERIC_CHARSET.indexOf(text.charAt(i)) * 45;
|
|
|
|
temp += ALPHANUMERIC_CHARSET.indexOf(text.charAt(i + 1));
|
2016-04-10 06:11:57 +00:00
|
|
|
bb.appendBits(temp, 11);
|
|
|
|
}
|
|
|
|
if (i < text.length()) // 1 character remaining
|
2017-05-08 06:20:50 +00:00
|
|
|
bb.appendBits(ALPHANUMERIC_CHARSET.indexOf(text.charAt(i)), 6);
|
2017-08-17 20:28:28 +00:00
|
|
|
return new QrSegment(Mode.ALPHANUMERIC, text.length(), bb);
|
2016-04-10 06:11:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-16 01:44:24 +00:00
|
|
|
/**
|
|
|
|
* 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<QrSegment> makeSegments(String text) {
|
2017-01-25 01:13:57 +00:00
|
|
|
Objects.requireNonNull(text);
|
2016-04-16 01:44:24 +00:00
|
|
|
|
|
|
|
// Select the most efficient segment encoding automatically
|
2016-04-21 18:39:47 +00:00
|
|
|
List<QrSegment> result = new ArrayList<>();
|
2017-08-26 18:57:52 +00:00
|
|
|
if (text.equals("")); // Leave result empty
|
2016-04-21 18:39:47 +00:00
|
|
|
else if (NUMERIC_REGEX.matcher(text).matches())
|
|
|
|
result.add(makeNumeric(text));
|
2016-04-16 01:44:24 +00:00
|
|
|
else if (ALPHANUMERIC_REGEX.matcher(text).matches())
|
2016-04-21 18:39:47 +00:00
|
|
|
result.add(makeAlphanumeric(text));
|
2016-04-16 01:44:24 +00:00
|
|
|
else
|
2016-04-21 18:39:47 +00:00
|
|
|
result.add(makeBytes(text.getBytes(StandardCharsets.UTF_8)));
|
|
|
|
return result;
|
2016-04-16 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-08 18:04:10 +00:00
|
|
|
/**
|
2017-08-31 20:19:17 +00:00
|
|
|
* Returns a segment representing an Extended Channel Interpretation
|
|
|
|
* (ECI) designator with the specified assignment value.
|
2017-05-08 18:04:10 +00:00
|
|
|
* @param assignVal the ECI assignment number (see the AIM ECI specification)
|
|
|
|
* @return a segment containing the data
|
|
|
|
* @throws IllegalArgumentException if the value is outside the range [0, 10<sup>6</sup>)
|
|
|
|
*/
|
|
|
|
public static QrSegment makeEci(int assignVal) {
|
2017-08-17 20:28:28 +00:00
|
|
|
BitBuffer bb = new BitBuffer();
|
2017-05-08 18:04:10 +00:00
|
|
|
if (0 <= assignVal && assignVal < (1 << 7))
|
2017-08-17 20:28:28 +00:00
|
|
|
bb.appendBits(assignVal, 8);
|
|
|
|
else if ((1 << 7) <= assignVal && assignVal < (1 << 14)) {
|
|
|
|
bb.appendBits(2, 2);
|
|
|
|
bb.appendBits(assignVal, 14);
|
2017-08-26 18:43:54 +00:00
|
|
|
} else if ((1 << 14) <= assignVal && assignVal < 1000000) {
|
2017-08-17 20:28:28 +00:00
|
|
|
bb.appendBits(6, 3);
|
|
|
|
bb.appendBits(assignVal, 21);
|
|
|
|
} else
|
2017-05-08 18:04:10 +00:00
|
|
|
throw new IllegalArgumentException("ECI assignment value out of range");
|
2017-08-17 20:28:28 +00:00
|
|
|
return new QrSegment(Mode.ECI, 0, bb);
|
2017-05-08 18:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-10 06:11:57 +00:00
|
|
|
|
|
|
|
/*---- Instance fields ----*/
|
|
|
|
|
|
|
|
/** The mode indicator for this segment. Never {@code null}. */
|
|
|
|
public final Mode mode;
|
|
|
|
|
|
|
|
/** The length of this segment's unencoded data, measured in characters. Always zero or positive. */
|
|
|
|
public final int numChars;
|
|
|
|
|
2017-08-17 20:34:52 +00:00
|
|
|
/** The data bits of this segment. Accessed through {@link getBits()}. Not {@code null}. */
|
|
|
|
final BitBuffer data;
|
2016-04-10 06:11:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*---- Constructor ----*/
|
|
|
|
|
2017-08-17 20:28:28 +00:00
|
|
|
/**
|
|
|
|
* Creates a new QR Code data segment with the specified parameters and data.
|
|
|
|
* @param md the mode, which is not {@code null}
|
|
|
|
* @param numCh the data length in characters, which is non-negative
|
|
|
|
* @param data the data bits of this segment, which is not {@code null}
|
|
|
|
* @throws NullPointerException if the mode or bit buffer is {@code null}
|
|
|
|
* @throws IllegalArgumentException if the character count is negative
|
|
|
|
*/
|
|
|
|
public QrSegment(Mode md, int numCh, BitBuffer data) {
|
2017-01-25 01:13:57 +00:00
|
|
|
Objects.requireNonNull(md);
|
2017-08-17 20:34:52 +00:00
|
|
|
Objects.requireNonNull(data);
|
|
|
|
if (numCh < 0)
|
2016-04-10 06:11:57 +00:00
|
|
|
throw new IllegalArgumentException("Invalid value");
|
|
|
|
mode = md;
|
|
|
|
numChars = numCh;
|
2017-08-17 20:34:52 +00:00
|
|
|
this.data = data.clone(); // Make defensive copy
|
2016-04-10 06:11:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-17 20:34:52 +00:00
|
|
|
/*---- Methods ----*/
|
2016-04-10 06:11:57 +00:00
|
|
|
|
|
|
|
/**
|
2017-08-17 20:34:52 +00:00
|
|
|
* Returns the data bits of this segment.
|
|
|
|
* @return the data bits of this segment (not {@code null})
|
2016-04-10 06:11:57 +00:00
|
|
|
*/
|
2017-08-17 20:34:52 +00:00
|
|
|
public BitBuffer getBits() {
|
|
|
|
return data.clone(); // Make defensive copy
|
2016-04-10 06:11:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-16 03:53:58 +00:00
|
|
|
// Package-private helper function.
|
|
|
|
static int getTotalBits(List<QrSegment> segs, int version) {
|
2017-01-25 01:13:57 +00:00
|
|
|
Objects.requireNonNull(segs);
|
2016-04-16 03:53:58 +00:00
|
|
|
if (version < 1 || version > 40)
|
|
|
|
throw new IllegalArgumentException("Version number out of range");
|
|
|
|
|
2017-04-21 15:37:59 +00:00
|
|
|
long result = 0;
|
2016-04-16 03:53:58 +00:00
|
|
|
for (QrSegment seg : segs) {
|
2017-01-25 01:13:57 +00:00
|
|
|
Objects.requireNonNull(seg);
|
2016-04-16 03:53:58 +00:00
|
|
|
int ccbits = seg.mode.numCharCountBits(version);
|
|
|
|
// Fail if segment length value doesn't fit in the length field's bit-width
|
|
|
|
if (seg.numChars >= (1 << ccbits))
|
|
|
|
return -1;
|
2017-08-17 20:34:52 +00:00
|
|
|
result += 4L + ccbits + seg.data.bitLength();
|
2017-04-21 15:37:59 +00:00
|
|
|
if (result > Integer.MAX_VALUE)
|
|
|
|
return -1;
|
2016-04-16 03:53:58 +00:00
|
|
|
}
|
2017-04-21 15:37:59 +00:00
|
|
|
return (int)result;
|
2016-04-16 03:53:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-10 06:11:57 +00:00
|
|
|
/*---- Constants ----*/
|
|
|
|
|
|
|
|
/** Can test whether a string is encodable in numeric mode (such as by using {@link #makeNumeric(String)}). */
|
|
|
|
public static final Pattern NUMERIC_REGEX = Pattern.compile("[0-9]*");
|
|
|
|
|
|
|
|
/** Can test whether a string is encodable in alphanumeric mode (such as by using {@link #makeAlphanumeric(String)}). */
|
|
|
|
public static final Pattern ALPHANUMERIC_REGEX = Pattern.compile("[A-Z0-9 $%*+./:-]*");
|
|
|
|
|
2017-05-08 06:20:50 +00:00
|
|
|
/** The set of all legal characters in alphanumeric mode, where each character value maps to the index in the string. */
|
|
|
|
private static final String ALPHANUMERIC_CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:";
|
2016-04-10 06:11:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*---- Public helper enumeration ----*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The mode field of a segment. Immutable. Provides methods to retrieve closely related values.
|
|
|
|
*/
|
|
|
|
public enum Mode {
|
2016-04-20 20:19:57 +00:00
|
|
|
|
|
|
|
/*-- Constants --*/
|
|
|
|
|
2016-04-10 06:11:57 +00:00
|
|
|
NUMERIC (0x1, 10, 12, 14),
|
|
|
|
ALPHANUMERIC(0x2, 9, 11, 13),
|
|
|
|
BYTE (0x4, 8, 16, 16),
|
2017-05-08 18:04:10 +00:00
|
|
|
KANJI (0x8, 8, 10, 12),
|
|
|
|
ECI (0x7, 0, 0, 0);
|
2016-04-10 06:11:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*-- Fields --*/
|
|
|
|
|
|
|
|
/** An unsigned 4-bit integer value (range 0 to 15) representing the mode indicator bits for this mode object. */
|
2016-04-20 20:40:49 +00:00
|
|
|
final int modeBits;
|
2016-04-10 06:11:57 +00:00
|
|
|
|
|
|
|
private final int[] numBitsCharCount;
|
|
|
|
|
|
|
|
|
2016-04-20 20:19:57 +00:00
|
|
|
/*-- Constructor --*/
|
|
|
|
|
2016-04-10 06:11:57 +00:00
|
|
|
private Mode(int mode, int... ccbits) {
|
|
|
|
this.modeBits = mode;
|
|
|
|
numBitsCharCount = ccbits;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*-- Method --*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the bit width of the segment character count field for this mode object at the specified version number.
|
|
|
|
* @param ver the version number, which is between 1 to 40, inclusive
|
|
|
|
* @return the number of bits for the character count, which is between 8 to 16, inclusive
|
|
|
|
* @throws IllegalArgumentException if the version number is out of range
|
|
|
|
*/
|
2016-04-20 20:40:49 +00:00
|
|
|
int numCharCountBits(int ver) {
|
2016-04-10 06:11:57 +00:00
|
|
|
if ( 1 <= ver && ver <= 9) return numBitsCharCount[0];
|
|
|
|
else if (10 <= ver && ver <= 26) return numBitsCharCount[1];
|
|
|
|
else if (27 <= ver && ver <= 40) return numBitsCharCount[2];
|
|
|
|
else throw new IllegalArgumentException("Version number out of range");
|
|
|
|
}
|
2016-04-20 20:19:57 +00:00
|
|
|
|
2016-04-10 06:11:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|