diff --git a/src/main/java/uk/org/okapibarcode/backend/ChannelCode.java b/src/main/java/uk/org/okapibarcode/backend/ChannelCode.java index a2dbd6f8..ef4f27dc 100644 --- a/src/main/java/uk/org/okapibarcode/backend/ChannelCode.java +++ b/src/main/java/uk/org/okapibarcode/backend/ChannelCode.java @@ -28,8 +28,8 @@ public class ChannelCode extends Symbol { private int[] space = new int[11]; private int[] bar = new int[11]; - private double currentValue; - private double targetValue; + private int currentValue; + private int targetValue; /** * Sets the preferred number of channels used to encode data. This setting will be @@ -39,7 +39,7 @@ public class ChannelCode extends Symbol { */ public void setPreferredNumberOfChannels(int channels) { if (channels < 3 || channels > 8) { - throw new IllegalArgumentException("Invalid Channel Code number of channels: " + channels); + throw new IllegalArgumentException("Invalid number of channels: " + channels); } preferredNumberOfChannels = channels; } @@ -56,26 +56,16 @@ public int getPreferredNumberOfChannels() { @Override protected void encode() { - int channels; - int i; - int leadingZeroCount; - if (content.length() > 7) { - throw new OkapiException("Input too long"); + throw new OkapiException("Input data too long"); } if (!content.matches("[0-9]+")) { - throw new OkapiException("Invalid characters in input"); - } - - if (preferredNumberOfChannels <= 2 || preferredNumberOfChannels > 8) { - channels = 3; - } else { - channels = preferredNumberOfChannels; + throw new OkapiException("Invalid characters in data"); } + int channels = preferredNumberOfChannels; targetValue = Integer.parseInt(content); - switch (channels) { case 3: if (targetValue > 26) { @@ -109,7 +99,7 @@ protected void encode() { infoLine("Channels Used: " + channels); - for (i = 0; i < 11; i++) { + for (int i = 0; i < 11; i++) { bar[i] = 0; space[i] = 0; } @@ -119,14 +109,14 @@ protected void encode() { pattern = new String[1]; nextSpace(channels, 3, channels, channels); - leadingZeroCount = channels - 1 - content.length(); - - readable = ""; - for (i = 0; i < leadingZeroCount; i++) { - readable += "0"; + StringBuilder text = new StringBuilder(); + int leadingZeroCount = channels - 1 - content.length(); + for (int i = 0; i < leadingZeroCount; i++) { + text.append('0'); } - readable += content; + text.append(content); + readable = text.toString(); row_count = 1; row_height = new int[] { -1 }; } diff --git a/src/test/resources/uk/org/okapibarcode/backend/channelcode/basic-3-channels-to-4.png b/src/test/resources/uk/org/okapibarcode/backend/channelcode/basic-3-channels-to-4.png new file mode 100644 index 00000000..6d2907dd Binary files /dev/null and b/src/test/resources/uk/org/okapibarcode/backend/channelcode/basic-3-channels-to-4.png differ diff --git a/src/test/resources/uk/org/okapibarcode/backend/channelcode/basic-3-channels-to-4.properties b/src/test/resources/uk/org/okapibarcode/backend/channelcode/basic-3-channels-to-4.properties new file mode 100644 index 00000000..f9f445ad --- /dev/null +++ b/src/test/resources/uk/org/okapibarcode/backend/channelcode/basic-3-channels-to-4.properties @@ -0,0 +1,12 @@ +PROPERTIES + +preferredNumberOfChannels=3 +content=27 + +LOG + +Channels Used: 4 + +CODEWORDS + +111100111111221212300000000 diff --git a/src/test/resources/uk/org/okapibarcode/backend/channelcode/basic-3-channels-to-8.png b/src/test/resources/uk/org/okapibarcode/backend/channelcode/basic-3-channels-to-8.png new file mode 100644 index 00000000..e2138acb Binary files /dev/null and b/src/test/resources/uk/org/okapibarcode/backend/channelcode/basic-3-channels-to-8.png differ diff --git a/src/test/resources/uk/org/okapibarcode/backend/channelcode/basic-3-channels-to-8.properties b/src/test/resources/uk/org/okapibarcode/backend/channelcode/basic-3-channels-to-8.properties new file mode 100644 index 00000000..a9511833 --- /dev/null +++ b/src/test/resources/uk/org/okapibarcode/backend/channelcode/basic-3-channels-to-8.properties @@ -0,0 +1,12 @@ +PROPERTIES + +preferredNumberOfChannels=3 +content=576689 + +LOG + +Channels Used: 8 + +CODEWORDS + +111100111111213211212213143 diff --git a/src/test/resources/uk/org/okapibarcode/backend/channelcode/basic-4-channels-to-5.png b/src/test/resources/uk/org/okapibarcode/backend/channelcode/basic-4-channels-to-5.png new file mode 100644 index 00000000..46676093 Binary files /dev/null and b/src/test/resources/uk/org/okapibarcode/backend/channelcode/basic-4-channels-to-5.png differ diff --git a/src/test/resources/uk/org/okapibarcode/backend/channelcode/basic-4-channels-to-5.properties b/src/test/resources/uk/org/okapibarcode/backend/channelcode/basic-4-channels-to-5.properties new file mode 100644 index 00000000..7bd9fcce --- /dev/null +++ b/src/test/resources/uk/org/okapibarcode/backend/channelcode/basic-4-channels-to-5.properties @@ -0,0 +1,12 @@ +PROPERTIES + +preferredNumberOfChannels=4 +content=293 + +LOG + +Channels Used: 5 + +CODEWORDS + +111100111111214311131000000 diff --git a/src/test/resources/uk/org/okapibarcode/backend/channelcode/basic-5-channels-to-6.png b/src/test/resources/uk/org/okapibarcode/backend/channelcode/basic-5-channels-to-6.png new file mode 100644 index 00000000..7391ef71 Binary files /dev/null and b/src/test/resources/uk/org/okapibarcode/backend/channelcode/basic-5-channels-to-6.png differ diff --git a/src/test/resources/uk/org/okapibarcode/backend/channelcode/basic-5-channels-to-6.properties b/src/test/resources/uk/org/okapibarcode/backend/channelcode/basic-5-channels-to-6.properties new file mode 100644 index 00000000..b463fb9e --- /dev/null +++ b/src/test/resources/uk/org/okapibarcode/backend/channelcode/basic-5-channels-to-6.properties @@ -0,0 +1,12 @@ +PROPERTIES + +preferredNumberOfChannels=5 +content=3494 + +LOG + +Channels Used: 6 + +CODEWORDS + +111100111111213411232110000 diff --git a/src/test/resources/uk/org/okapibarcode/backend/channelcode/basic-6-channels-to-7.png b/src/test/resources/uk/org/okapibarcode/backend/channelcode/basic-6-channels-to-7.png new file mode 100644 index 00000000..5d528424 Binary files /dev/null and b/src/test/resources/uk/org/okapibarcode/backend/channelcode/basic-6-channels-to-7.png differ diff --git a/src/test/resources/uk/org/okapibarcode/backend/channelcode/basic-6-channels-to-7.properties b/src/test/resources/uk/org/okapibarcode/backend/channelcode/basic-6-channels-to-7.properties new file mode 100644 index 00000000..f30dc180 --- /dev/null +++ b/src/test/resources/uk/org/okapibarcode/backend/channelcode/basic-6-channels-to-7.properties @@ -0,0 +1,12 @@ +PROPERTIES + +preferredNumberOfChannels=6 +content=44073 + +LOG + +Channels Used: 7 + +CODEWORDS + +111100111111213223142111200 diff --git a/src/test/resources/uk/org/okapibarcode/backend/channelcode/basic-7-channels-to-8.png b/src/test/resources/uk/org/okapibarcode/backend/channelcode/basic-7-channels-to-8.png new file mode 100644 index 00000000..e2138acb Binary files /dev/null and b/src/test/resources/uk/org/okapibarcode/backend/channelcode/basic-7-channels-to-8.png differ diff --git a/src/test/resources/uk/org/okapibarcode/backend/channelcode/basic-7-channels-to-8.properties b/src/test/resources/uk/org/okapibarcode/backend/channelcode/basic-7-channels-to-8.properties new file mode 100644 index 00000000..f76dc631 --- /dev/null +++ b/src/test/resources/uk/org/okapibarcode/backend/channelcode/basic-7-channels-to-8.properties @@ -0,0 +1,12 @@ +PROPERTIES + +preferredNumberOfChannels=7 +content=576689 + +LOG + +Channels Used: 8 + +CODEWORDS + +111100111111213211212213143 diff --git a/src/test/resources/uk/org/okapibarcode/backend/channelcode/invalid-chars.properties b/src/test/resources/uk/org/okapibarcode/backend/channelcode/invalid-chars.properties new file mode 100644 index 00000000..7d877c17 --- /dev/null +++ b/src/test/resources/uk/org/okapibarcode/backend/channelcode/invalid-chars.properties @@ -0,0 +1,7 @@ +PROPERTIES + +content=1.2 + +ERROR + +Invalid characters in data diff --git a/src/test/resources/uk/org/okapibarcode/backend/channelcode/too-few-channels.properties b/src/test/resources/uk/org/okapibarcode/backend/channelcode/too-few-channels.properties new file mode 100644 index 00000000..8eba40f1 --- /dev/null +++ b/src/test/resources/uk/org/okapibarcode/backend/channelcode/too-few-channels.properties @@ -0,0 +1,8 @@ +PROPERTIES + +preferredNumberOfChannels=2 +content=7742862 + +ERROR + +Invalid number of channels: 2 diff --git a/src/test/resources/uk/org/okapibarcode/backend/channelcode/too-long.properties b/src/test/resources/uk/org/okapibarcode/backend/channelcode/too-long.properties new file mode 100644 index 00000000..8e73db78 --- /dev/null +++ b/src/test/resources/uk/org/okapibarcode/backend/channelcode/too-long.properties @@ -0,0 +1,7 @@ +PROPERTIES + +content=12345678 + +ERROR + +Input data too long diff --git a/src/test/resources/uk/org/okapibarcode/backend/channelcode/too-many-channels.properties b/src/test/resources/uk/org/okapibarcode/backend/channelcode/too-many-channels.properties new file mode 100644 index 00000000..6e6fa0df --- /dev/null +++ b/src/test/resources/uk/org/okapibarcode/backend/channelcode/too-many-channels.properties @@ -0,0 +1,8 @@ +PROPERTIES + +preferredNumberOfChannels=9 +content=7742862 + +ERROR + +Invalid number of channels: 9