Skip to content

Commit

Permalink
Aztec Code: allow user to restrict to normal sizes or compact sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
gredler committed Apr 21, 2024
1 parent d42e94e commit 79b58c9
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 11 deletions.
69 changes: 58 additions & 11 deletions src/main/java/uk/org/okapibarcode/backend/AztecCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@
*/
public class AztecCode extends Symbol {

/** The types of Aztec Code symbol sizes allowed. */
public enum Mode {
/** Allow only normal Aztec Code symbol sizes. */
NORMAL,
/** Allow only compact Aztec Code symbol sizes. */
COMPACT,
/** Allow both normal and compact Aztec Code symbol sizes. */
ANY;
}

/* 27 x 27 data grid */
private static final int[] COMPACT_AZTEC_MAP = {
609, 608, 411, 413, 415, 417, 419, 421, 423, 425, 427, 429, 431, 433, 435, 437, 439, 441, 443, 445, 447, 449, 451, 453, 455, 457, 459,
Expand Down Expand Up @@ -324,19 +334,48 @@ private static int avoidReferenceGrid(int input) {
return output;
}

private Mode mode;
private int preferredSize = 0;
private int preferredEccLevel = 2;
private String structuredAppendMessageId;
private int structuredAppendPosition = 1;
private int structuredAppendTotal = 1;

/**
* Creates a new instance.
* Creates a new instance, using mode {@link Mode#ANY}.
*/
public AztecCode() {
this(Mode.ANY);
}

/**
* Creates a new instance, using the specified mode.
*
* @param mode whether to allow normal sizes, compact sizes, or all sizes
*/
public AztecCode(Mode mode) {
this.mode = mode;
this.humanReadableLocation = HumanReadableLocation.NONE;
}

/**
* Sets the mode (normal sizes only, compact sizes only, or all sizes).
*
* @param mode the mode (normal sizes only, compact sizes only, or all sizes)
*/
public void setMode(Mode mode) {
this.mode = mode;
}

/**
* Returns the mode (normal sizes only, compact sizes only, or all sizes).
*
* @return the mode (normal sizes only, compact sizes only, or all sizes)
*/
public Mode getMode() {
return mode;
}

/**
* <p>Sets a preferred symbol size. This value may be ignored if data string is
* too large to fit in the specified symbol size. Values correspond to symbol
Expand Down Expand Up @@ -568,19 +607,23 @@ protected void encode() {
layers = 0;
compact = false;

for (int i = 32; i > 0; i--) {
if (dataLength < dataSizes[i - 1]) {
layers = i;
compact = false;
dataMaxSize = dataSizes[i - 1];
if (mode == Mode.NORMAL || mode == Mode.ANY) {
for (int i = 32; i > 0; i--) {
if (dataLength < dataSizes[i - 1]) {
layers = i;
compact = false;
dataMaxSize = dataSizes[i - 1];
}
}
}

for (int i = compLoop; i > 0; i--) {
if (dataLength < compactDataSizes[i - 1]) {
layers = i;
compact = true;
dataMaxSize = compactDataSizes[i - 1];
if (mode == Mode.COMPACT || mode == Mode.ANY) {
for (int i = compLoop; i > 0; i--) {
if (dataLength < compactDataSizes[i - 1]) {
layers = i;
compact = true;
dataMaxSize = compactDataSizes[i - 1];
}
}
}

Expand Down Expand Up @@ -609,6 +652,10 @@ protected void encode() {
layers = preferredSize - 4;
}

if ((compact && mode == Mode.NORMAL) || (!compact && mode == Mode.COMPACT)) {
throw new OkapiInputException("Aztec mode " + mode + " and preferred size " + preferredSize + " are incompatible");
}

adjustedString = adjustBinaryString(binaryString, compact, layers);

/* Check if the data actually fits into the selected symbol size */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ PROPERTIES

content=this_is_just_a_test

mode=ANY
content=this_is_just_a_test

constructor=ANY
content=this_is_just_a_test

LOG

ECI Mode: 3
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
PROPERTIES

mode=COMPACT
preferredSize=5
content=1234567890123456789012345678901234567890

ERROR

Aztec mode COMPACT and preferred size 5 are incompatible
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
PROPERTIES

content=1234567890123456789012345678901234567890

constructor=COMPACT
content=1234567890123456789012345678901234567890

mode=COMPACT
content=1234567890123456789012345678901234567890

mode=COMPACT
preferredSize=2
content=1234567890123456789012345678901234567890

constructor=ANY
content=1234567890123456789012345678901234567890

mode=ANY
content=1234567890123456789012345678901234567890

mode=ANY
preferredSize=2
content=1234567890123456789012345678901234567890

LOG

ECI Mode: 3
ECI Charset: ISO-8859-1
Encoding: DL 3 4 5 6 7 8 9 10 11 2 3 4 5 6 7 8 9 10 11 2 3 4 5 6 7 8 9 10 11 2 3 4 5 6 7 8 9 10 11 2
Codewords: 60 26 10 51 49 13 22 17 40 43 15 4 53 25 6 34 44 60 19 21 36 26 10 51 49 13 22 23
Compact Mode: true
Layers: 2
Codeword Length: 6 bits
Data Codewords: 28
ECC Codewords: 12
Mode Message: 01011011
Blocks Merged: 94 -> 79

CODEWORDS

312313132
24111141112
021112231312
013141243
1321112111221
11>21
02211724
13215111121
1211111311321
22211111111221
022111131124
2131511311
211117141
0311:211
0124222321
02812411
22131112123
3111151123
031321312111
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
PROPERTIES

mode=NORMAL
preferredSize=1
content=1234567890123456789012345678901234567890

ERROR

Aztec mode NORMAL and preferred size 1 are incompatible
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
PROPERTIES

constructor=NORMAL
content=1234567890123456789012345678901234567890

mode=NORMAL
content=1234567890123456789012345678901234567890

mode=NORMAL
preferredSize=6
content=1234567890123456789012345678901234567890

LOG

ECI Mode: 3
ECI Charset: ISO-8859-1
Encoding: DL 3 4 5 6 7 8 9 10 11 2 3 4 5 6 7 8 9 10 11 2 3 4 5 6 7 8 9 10 11 2 3 4 5 6 7 8 9 10 11 2
Codewords: 60 26 10 51 49 13 22 17 40 43 15 4 53 25 6 34 44 60 19 21 36 26 10 51 49 13 22 23
Compact Mode: false
Layers: 2
Codeword Length: 6 bits
Data Codewords: 28
ECC Codewords: 20
Mode Message: 0000100000011011
Blocks Merged: 137 -> 98

CODEWORDS

23132231231
611131151111
0212652131
0111212111212421
1234171121
11A13
033;1122
1231912211
121111171142
211111115111123
03111111131111312
011111111111111111111111
32111113111124
3121115111213
0311111711141
0111111191141
03111;1113
2111>31
0133121314121
0111142112132111
01311111211145
01111511311151
13213223141

0 comments on commit 79b58c9

Please sign in to comment.