Skip to content

Commit

Permalink
fix MSI encoding issue with checksum, Postnet bar size to spec, gradl…
Browse files Browse the repository at this point in the history
…e 8.4
  • Loading branch information
barnhill committed Oct 18, 2023
1 parent 0884cbd commit bbeeb9b
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 81 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

buildscript {
extra["gradle"] = "8.3"
extra["gradle"] = "8.4"
extra["javaVersion"] = JavaVersion.VERSION_11
}

Expand All @@ -27,7 +27,7 @@ dependencies {
testImplementation(libs.junit)
}

val dokkaOutputDir = buildDir.resolve("docs")
val dokkaOutputDir = layout.buildDirectory.asFile.get().resolve("docs")
tasks {
wrapper {
gradleVersion = gradle
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
GROUP=com.pnuema.java
VERSION_NAME=2.3
VERSION_NAME=2.4
POM_ARTIFACT_ID=barcode

POM_NAME=Barcode-Java
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
14 changes: 7 additions & 7 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC2039,SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC2039,SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
Expand Down Expand Up @@ -202,11 +202,11 @@ fi
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'

# Collect all arguments for the java command;
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
# shell script including quotes and variable substitutions, so put them in
# double quotes to make sure that they get re-expanded; and
# * put everything else in single quotes, so that it's not re-expanded.
# Collect all arguments for the java command:
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# and any embedded shellness will be escaped.
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
# treated as '${Hostname}' itself on the command line.

set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
Expand Down
4 changes: 2 additions & 2 deletions local.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
#Thu Jul 13 11:22:30 CDT 2023
sdk.dir=/Users/b359923/Library/Android/sdk
#Tue Oct 17 23:18:39 CDT 2023
sdk.dir=C\:\\Users\\bradb\\AppData\\Local\\Android\\Sdk
2 changes: 1 addition & 1 deletion src/main/java/com/pnuema/java/barcode/Barcode.java
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,7 @@ private Image generateImage() {
if (getEncodedType() == EncodingType.PostNet) {
//draw half bars in postnet
if (getEncodedValue().charAt(pos) == '0') {
g.fillRect(pos * iBarWidth + shiftAdjustment, (ILHeight / 2) + topLabelAdjustment, iBarWidth / 2, (ILHeight / 2) + topLabelAdjustment);
g.fillRect(pos * iBarWidth + shiftAdjustment, (int) (ILHeight - ILHeight * 0.4f) + topLabelAdjustment, iBarWidth / 2, (int) (ILHeight - ILHeight * 0.4f) + topLabelAdjustment);
} else {
g.fillRect(pos * iBarWidth + shiftAdjustment, topLabelAdjustment, iBarWidth / 2, ILHeight + topLabelAdjustment);
}
Expand Down
126 changes: 59 additions & 67 deletions src/main/java/com/pnuema/java/barcode/symbologies/MSI.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,77 +27,27 @@ private String encodeMSI() {
error("EMSI-1: Numeric Data Only");
}

String PreEncoded = getRawData();

//get checksum
if (encodedType == EncodingType.MSI_Mod10 || encodedType == EncodingType.MSI_2Mod10) {
String odds = "";
String evens = "";
for (int i = PreEncoded.length() - 1; i >= 0; i -= 2) {
odds = PreEncoded.toCharArray()[i] + odds;
if (i - 1 >= 0)
evens = PreEncoded.toCharArray()[i - 1] + evens;
}

//multiply odds by 2
odds = String.valueOf(Integer.parseInt(odds) * 2);

int evensum = 0;
int oddsum = 0;
for (Character c : evens.toCharArray()) {
evensum += Integer.parseInt(c.toString());
}
for (Character c : odds.toCharArray()) {
oddsum += Integer.parseInt(c.toString());
}
int mod = (oddsum + evensum) % 10;
int checksum = mod == 0 ? 0 : 10 - mod;
PreEncoded += String.valueOf(checksum);
//Add Checksum
String withChecksum = "";

if (encodedType == EncodingType.MSI_Mod10) {
withChecksum = Mod10(getRawData());
} else if (encodedType == EncodingType.MSI_Mod11) {
withChecksum = Mod11(getRawData());
} else if (encodedType == EncodingType.MSI_2Mod10) {
withChecksum = Mod10(Mod10(getRawData()));
} else if (encodedType == EncodingType.MSI_Mod11_Mod10) {
withChecksum = Mod10(Mod11(getRawData()));
}

if (encodedType == EncodingType.MSI_Mod11 || encodedType == EncodingType.MSI_Mod11_Mod10) {
int sum = 0;
int weight = 2;
for (int i = PreEncoded.length() - 1; i >= 0; i--) {
if (weight > 7) {
weight = 2;
}
sum += Integer.parseInt(String.valueOf(PreEncoded.toCharArray()[i])) * weight++;
}
int mod = sum % 11;
int checksum = mod == 0 ? 0 : 11 - mod;

PreEncoded += String.valueOf(checksum);
}

if (encodedType == EncodingType.MSI_2Mod10 || encodedType == EncodingType.MSI_Mod11_Mod10) {
//get second check digit if 2 mod 10 was selected or Mod11/Mod10
String odds = "";
String evens = "";
for (int i = PreEncoded.length() - 1; i >= 0; i -= 2) {
odds = String.valueOf(PreEncoded.toCharArray()[i]) + odds;
if (i - 1 >= 0) {
evens = String.valueOf(PreEncoded.toCharArray()[i - 1]) + evens;
}
}

//multiply odds by 2
odds = String.valueOf(Integer.parseInt(odds) * 2);

int evensum = 0;
int oddsum = 0;
for (Character c : evens.toCharArray()) {
evensum += Integer.parseInt(c.toString());
}
for (Character c : odds.toCharArray()) {
oddsum += Integer.parseInt(c.toString());
}
int checksum = 10 - ((oddsum + evensum) % 10);
PreEncoded += String.valueOf(checksum);
}
if (withChecksum.isEmpty())
error("EMSI-2: Invalid MSI encoding type");

//add start character
String result = "110";
for (Character c : PreEncoded.toCharArray()) {

//add encoding
for (Character c : withChecksum.toCharArray()) {
result += MSI_Code[Integer.parseInt(c.toString())];
}

Expand All @@ -107,6 +57,48 @@ private String encodeMSI() {
return result;
}

private String Mod10(String code)
{
var odds = "";
var evens = "";
var chars = code.toCharArray();
for (var i = code.length() - 1; i >= 0; i -= 2)
{
odds = chars[i] + odds;
if (i - 1 >= 0)
evens = chars[i - 1] + evens;
}//for

//multiply odds by 2
odds = String.valueOf(Integer.parseInt(odds) * 2);

var evensum = 0;
var oddsum = 0;
for (Character c : evens.toCharArray())
evensum += Integer.parseInt(c.toString());
for (Character c : odds.toCharArray())
oddsum += Integer.parseInt(c.toString());
var mod = (oddsum + evensum) % 10;
var checksum = mod == 0 ? 0 : 10 - mod;
return code + checksum;
}

private String Mod11(String code)
{
var sum = 0;
var weight = 2;
var chars = code.toCharArray();
for (var i = code.length() - 1; i >= 0; i--)
{
if (weight > 7) weight = 2;
sum += Integer.parseInt(String.valueOf(chars[i])) * weight++;
}//foreach
var mod = sum % 11;
var checksum = mod == 0 ? 0 : 11 - mod;

return code + checksum;
}

public String getEncodedValue() {
return encodeMSI();
}
Expand Down

0 comments on commit bbeeb9b

Please sign in to comment.