Merge pull request #1 from minsu4107/master

Extract method in QrsegmentAdvanced
pull/90/head
gerzees 5 years ago committed by GitHub
commit ff15a7faca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -204,8 +204,6 @@ public final class QrCode {
return new QrCode(version, ecl, dataCodewords, mask); return new QrCode(version, ecl, dataCodewords, mask);
} }
/*---- Instance fields ----*/ /*---- Instance fields ----*/
// Public immutable scalar parameters: // Public immutable scalar parameters:

@ -64,14 +64,14 @@ public final class QrSegmentAdvanced {
// Check arguments // Check arguments
Objects.requireNonNull(text); Objects.requireNonNull(text);
Objects.requireNonNull(ecl); Objects.requireNonNull(ecl);
if (!(QrCode.MIN_VERSION <= minVersion && minVersion <= maxVersion && maxVersion <= QrCode.MAX_VERSION)) if (not_Valid_Version(minVersion, maxVersion))
throw new IllegalArgumentException("Invalid value"); throw new IllegalArgumentException("Invalid value");
// Iterate through version numbers, and make tentative segments // Iterate through version numbers, and make tentative segments
List<QrSegment> segs = null; List<QrSegment> segs = null;
int[] codePoints = toCodePoints(text); int[] codePoints = toCodePoints(text);
for (int version = minVersion; ; version++) { for (int version = minVersion; ; version++) {
if (version == minVersion || version == 10 || version == 27) if (is_valid_version(minVersion, version))
segs = makeSegmentsOptimally(codePoints, version); segs = makeSegmentsOptimally(codePoints, version);
assert segs != null; assert segs != null;
@ -88,6 +88,16 @@ public final class QrSegmentAdvanced {
} }
} }
} }
private static boolean is_valid_version(int minVersion, int version) {
return version == minVersion || version == 10 || version == 27;
}
private static boolean not_Valid_Version(int minVersion, int maxVersion) {
return !(QrCode.MIN_VERSION <= minVersion && minVersion <= maxVersion && maxVersion <= QrCode.MAX_VERSION);
}
// Returns a new list of segments that is optimal for the given text at the given version number. // Returns a new list of segments that is optimal for the given text at the given version number.
@ -123,22 +133,22 @@ public final class QrSegmentAdvanced {
// Calculate costs using dynamic programming // Calculate costs using dynamic programming
for (int i = 0; i < codePoints.length; i++) { for (int i = 0; i < codePoints.length; i++) {
int c = codePoints[i]; int cPoint = codePoints[i];
int[] curCosts = new int[numModes]; int[] curCosts = new int[numModes];
{ // Always extend a byte mode segment { // Always extend a byte mode segment
curCosts[0] = prevCosts[0] + countUtf8Bytes(c) * 8 * 6; curCosts[0] = prevCosts[0] + countUtf8Bytes(cPoint) * 8 * 6;
charModes[i][0] = modeTypes[0]; charModes[i][0] = modeTypes[0];
} }
// Extend a segment if possible // Extend a segment if possible
if (QrSegment.ALPHANUMERIC_CHARSET.indexOf(c) != -1) { // Is alphanumeric if (is_alphanumeric(cPoint)) { // Is alphanumeric
curCosts[1] = prevCosts[1] + 33; // 5.5 bits per alphanumeric char curCosts[1] = prevCosts[1] + 33; // 5.5 bits per alphanumeric char
charModes[i][1] = modeTypes[1]; charModes[i][1] = modeTypes[1];
} }
if ('0' <= c && c <= '9') { // Is numeric if (is_numeric(cPoint)) { // Is numeric
curCosts[2] = prevCosts[2] + 20; // 3.33 bits per digit curCosts[2] = prevCosts[2] + 20; // 3.33 bits per digit
charModes[i][2] = modeTypes[2]; charModes[i][2] = modeTypes[2];
} }
if (isKanji(c)) { if (isKanji(cPoint)) {
curCosts[3] = prevCosts[3] + 78; // 13 bits per Shift JIS char curCosts[3] = prevCosts[3] + 78; // 13 bits per Shift JIS char
charModes[i][3] = modeTypes[3]; charModes[i][3] = modeTypes[3];
} }
@ -147,7 +157,8 @@ public final class QrSegmentAdvanced {
for (int j = 0; j < numModes; j++) { // To mode for (int j = 0; j < numModes; j++) { // To mode
for (int k = 0; k < numModes; k++) { // From mode for (int k = 0; k < numModes; k++) { // From mode
int newCost = (curCosts[k] + 5) / 6 * 6 + headCosts[j]; int newCost = (curCosts[k] + 5) / 6 * 6 + headCosts[j];
if (charModes[i][k] != null && (charModes[i][j] == null || newCost < curCosts[j])) { boolean is_notNull_or_lower = charModes[i][k] != null && (charModes[i][j] == null || newCost < curCosts[j]);
if (is_notNull_or_lower) {
curCosts[j] = newCost; curCosts[j] = newCost;
charModes[i][j] = modeTypes[k]; charModes[i][j] = modeTypes[k];
} }
@ -179,6 +190,16 @@ public final class QrSegmentAdvanced {
} }
return result; return result;
} }
private static boolean is_numeric(int c) {
return '0' <= c && c <= '9';
}
private static boolean is_alphanumeric(int c) {
return QrSegment.ALPHANUMERIC_CHARSET.indexOf(c) != -1;
}
// Returns a new list of segments based on the given text and modes, such that // Returns a new list of segments based on the given text and modes, such that
@ -277,8 +298,7 @@ public final class QrSegmentAdvanced {
*/ */
public static boolean isEncodableAsKanji(String text) { public static boolean isEncodableAsKanji(String text) {
Objects.requireNonNull(text); Objects.requireNonNull(text);
return text.chars().allMatch( return text.chars().allMatch(c -> isKanji((char)c));
c -> isKanji((char)c));
} }

Loading…
Cancel
Save