Skip to content

Commit

Permalink
cleanup lint warnings, markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
barnhill committed Jun 16, 2024
1 parent 2f2c436 commit b14b7a5
Show file tree
Hide file tree
Showing 18 changed files with 96 additions and 84 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

This library was designed to give an easy class for developers to use when they need to generate barcode images from a string of data.

| Supported | Symbology | List |
| :------------- | :------------- | :-----|
| Code 128 | Code 93 | Code 39 (Extended / Full ASCII) |
| Code11 | EAN-8 | FIM (Facing Identification Mark) |
| UPC-A | UPC-E | Pharmacode |
| MSI | PostNet | Standard 2 of 5 |
| ISBN | Codabar | Interleaved 2 of 5 |
| ITF-14 | Telepen | UPC Supplemental 2 |
| JAN-13 | EAN-13 | UPC Supplemental 5 |
| Supported | Symbology | List |
|:---------------:|:---------------:|:--------------------------------:|
| Code 128 | Code 93 | Code 39 (Extended / Full ASCII) |
| Code11 | EAN-8 | FIM (Facing Identification Mark) |
| UPC-A | UPC-E | Pharmacode |
| MSI | PostNet | Standard 2 of 5 |
| ISBN | Codabar | Interleaved 2 of 5 |
| ITF-14 | Telepen | UPC Supplemental 2 |
| JAN-13 | EAN-13 | UPC Supplemental 5 |

### Usage ###

Expand All @@ -30,7 +30,7 @@ Image img = barcode.encode(BarcodeLib.TYPE.UPCA, "038000356216");

![upca](https://user-images.githubusercontent.com/3878158/170283065-42d6c9f5-1e97-47dc-91da-f95ac68da909.jpg)

You can specify the width, height, foreground color, background color, and whether or not to include the label to display the data thats encoded with the image.
You can specify the width, height, foreground color, background color, and whether to include the label to display the data that's encoded with the image.

### Support ###
If you find this or any of my software useful and decide its worth supporting. You can do so here: [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=QKT9PSYTDNSXS)
Expand Down
14 changes: 5 additions & 9 deletions src/main/java/com/pnuema/java/barcode/Barcode.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import static java.awt.image.BufferedImage.TYPE_INT_ARGB;

/**
* Generates a barcode image of a specified symbology from a string of data.
*/
@SuppressWarnings({"WeakerAccess", "unused"})
@SuppressWarnings({"WeakerAccess", "unused", "DuplicatedCode"})
public class Barcode {

public enum SaveTypes {JPG, BMP, PNG, GIF, TIFF, UNSPECIFIED}
Expand Down Expand Up @@ -129,7 +126,7 @@ public String getCountryAssigningManufacturerCode() {
}

/**
* Gets the Encoded Type (ex. UPC-A, EAN-13 ... etc)
* Gets the Encoded Type (ex. UPC-A, EAN-13 ... etc.)
*
* @return encoded type
*/
Expand All @@ -138,7 +135,7 @@ public EncodingType getEncodedType() {
}

/**
* Sets the Encoded Type (ex. UPC-A, EAN-13 ... etc)
* Sets the Encoded Type (ex. UPC-A, EAN-13 ... etc.)
*
* @param encoded_Type encoded type
*/
Expand Down Expand Up @@ -709,11 +706,10 @@ private Image generateImage() {
int ILHeight = getHeight();
int topLabelAdjustment = 0;

int shiftAdjustment = 0;
int iBarWidth = getWidth() / encodedValue.length();

//set alignment
shiftAdjustment = getShiftAdjustment();
int shiftAdjustment = getShiftAdjustment();

if (isIncludeLabel()) {
if ((getAlternateLabel() == null || getRawData().startsWith(getAlternateLabel())) && isStandardizeLabel()) {
Expand Down Expand Up @@ -754,7 +750,7 @@ private Image generateImage() {

g.setColor(getForeColor());

//lines are fBarWidth wide so draw the appropriate color line vertically]
//lines are fBarWidth wide so draw the appropriate color line vertically
while (pos < getEncodedValue().length()) {
if (getEncodedValue().charAt(pos) == '1') {
g.fillRect(pos * iBarWidth + shiftAdjustment, topLabelAdjustment, iBarWidth, ILHeight + topLabelAdjustment);
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/pnuema/java/barcode/BarcodeCommon.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public abstract class BarcodeCommon implements IBarcode {
private String rawData = "";
private final List<String> errors = new ArrayList<>();

public abstract String getEncodedValue() throws BarcodeException;

protected void setRawData(String rawData) {
this.rawData = rawData;
}
Expand All @@ -24,7 +26,7 @@ public void clearErrors() {
errors.clear();
}

protected void error(String ErrorMessage) {
protected void error(String ErrorMessage) throws BarcodeException {
errors.add(ErrorMessage);
throw new BarcodeException(ErrorMessage);
}
Expand Down
21 changes: 8 additions & 13 deletions src/main/java/com/pnuema/java/barcode/Labels.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
/**
* Class to draw labels for differing barcode types
*/
@SuppressWarnings("DuplicatedCode")
class Labels {
public enum LabelPositions {TOP, BOTTOM}

Expand Down Expand Up @@ -53,10 +54,9 @@ public static Image Label_ITF14(Barcode Barcode, BufferedImage img) {
/**
* Draws Label for Generic barcodes
* @param Barcode Barcode to draw the label for
* @param img Image representation of the barcode without the labels
* @return Image representation of the barcode with labels applied
* @param img Image representation of the barcode to which the label will be applied
*/
static Image labelGeneric(Barcode Barcode, BufferedImage img) {
static void labelGeneric(Barcode Barcode, BufferedImage img) {
try {
Font font = Barcode.getLabelFont();

Expand Down Expand Up @@ -87,7 +87,6 @@ static Image labelGeneric(Barcode Barcode, BufferedImage img) {
drawCenteredString(g, Barcode.getAlternateLabel() == null ? Barcode.getRawData() : Barcode.getAlternateLabel(), rect, font);

g.dispose();
return img;
} catch (Exception ex) {
throw new RuntimeException("ELABEL_GENERIC-1: " + ex.getMessage());
}
Expand All @@ -96,10 +95,9 @@ static Image labelGeneric(Barcode Barcode, BufferedImage img) {
/**
* Draws Label for EAN-13 barcodes
* @param Barcode Barcode to draw the label for
* @param img Image representation of the barcode without the labels
* @return Image representation of the barcode with labels applied
* @param img Image representation of the barcode to which the label will be applied
*/
static Image Label_EAN13(Barcode Barcode, BufferedImage img) {
static void Label_EAN13(Barcode Barcode, BufferedImage img) {
try {
int iBarWidth = Barcode.getWidth() / Barcode.getEncodedValue().length();
String defTxt = Barcode.getRawData();
Expand Down Expand Up @@ -143,7 +141,6 @@ static Image Label_EAN13(Barcode Barcode, BufferedImage img) {
g.drawString(defTxt.substring(7), s3 - iBarWidth, (float)LabelY);

g.dispose();
return img;
} catch (Exception ex) {
throw new IllegalArgumentException("ELABEL_EAN13-1: " + ex.getMessage());
}
Expand All @@ -152,10 +149,9 @@ static Image Label_EAN13(Barcode Barcode, BufferedImage img) {
/**
* Draws Label for UPC-A barcodes
* @param Barcode Barcode to draw the label for
* @param img Image representation of the barcode without the labels
* @return Image representation of the barcode with labels applied
* @param img Image representation of the barcode to which the label will be applied
*/
public static Image Label_UPCA(Barcode Barcode, BufferedImage img) {
public static void Label_UPCA(Barcode Barcode, BufferedImage img) {
try {
int iBarWidth = Barcode.getWidth() / Barcode.getEncodedValue().length();
int halfBarWidth = (int)(iBarWidth * 0.5);
Expand Down Expand Up @@ -202,7 +198,6 @@ public static Image Label_UPCA(Barcode Barcode, BufferedImage img) {
g.drawString(defTxt.substring(11), s4, img.getHeight() - smallFont.getSize());

g.dispose();
return img;
} catch (Exception ex) {
throw new RuntimeException("ELABEL_UPCA-1: " + ex.getMessage());
}
Expand All @@ -212,7 +207,7 @@ public static int getFontsize(int wid, int hgt, String lbl) {
//Returns the optimal font size for the specified dimensions
int fontSize = 10;

if (lbl.length() > 0) {
if (!lbl.isEmpty()) {
BufferedImage fakeImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB); //As we cannot use CreateGraphics() in a class library, so the fake image is used to load the Graphics.

Graphics g = fakeImage.createGraphics();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public Codabar(String input) {
private String encodeCodabar() {
if (getRawData().length() < 2) error("ECODABAR-1: Data format invalid. (Invalid length)");

//check first char to make sure its a start/stop char
//check first char to make sure it is a start/stop char
switch (String.valueOf(getRawData().charAt(0)).toUpperCase().trim()) {
case "A":
case "B":
Expand All @@ -33,7 +33,7 @@ private String encodeCodabar() {
break;
}

//check the ending char to make sure its a start/stop char
//check the ending char to make sure it is a start/stop char
switch (String.valueOf(getRawData().charAt(getRawData().trim().length() - 1)).trim().toUpperCase()) {
case "A":
case "B":
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/pnuema/java/barcode/symbologies/Code11.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,14 @@ private String encodeCode11() {
}

//encode data
return doEncoding(dataToEncodeWithChecksums);
}

private String doEncoding(String dataToEncodeWithChecksums) {
String space = "0";
StringBuilder builder = new StringBuilder();
builder.append(C11_Code[11]);//start-stop char
builder.append(space); //interchar space
builder.append(space); //inter-character space

for (char c : dataToEncodeWithChecksums.toCharArray()) {
int index = (c == '-' ? 10 : Integer.parseInt(String.valueOf(c)));
Expand All @@ -83,7 +87,6 @@ private String encodeCode11() {

//stop bars
builder.append(C11_Code[11]);

return builder.toString();
}

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/pnuema/java/barcode/symbologies/Code128.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* Code 128 encoding
*/
public class Code128 extends BarcodeCommon {
public enum TYPES { DYNAMIC, A, B, C };
public enum TYPES { DYNAMIC, A, B, C }
private final List<Entry> C128_Code = new ArrayList<>();
private final List<String> formattedData = new ArrayList<>();
private Entry startCharacter = null;
Expand Down Expand Up @@ -77,7 +77,7 @@ public Code128(String input, TYPES type) {
}

private String encodeCode128() {
//initialize datastructure to hold encoding information
//initialize data structure to hold encoding information
initCode128();

return getEncoding();
Expand Down Expand Up @@ -350,7 +350,7 @@ private void breakUpDataForEncoding() {
}
}

private void insertStartandCodeCharacters() {
private void insertStartAndCodeCharacters() {
Entry currentCodeSet;
String currentCodeString;

Expand Down Expand Up @@ -379,7 +379,7 @@ private void insertStartandCodeCharacters() {
CodeCharacter codeCharacter = findStartorCodeCharacter(formattedData.get(i));
List<Entry> tempStartChars = codeCharacter.rows;

//check all the start characters and see if we need to stay with the same codeset or if a change of sets is required
//check all the start characters and see if we need to stay with the same code set or if a change of sets is required
boolean sameCodeSet = false;
for (Entry row : tempStartChars) {
if (row.getA().endsWith(currentCodeString) || row.getB().endsWith(currentCodeString) || row.getC().endsWith(currentCodeString)) {
Expand Down Expand Up @@ -418,7 +418,7 @@ private String getEncoding() {
breakUpDataForEncoding();

//insert the start characters
insertStartandCodeCharacters();
insertStartAndCodeCharacters();

StringBuilder encodedData = new StringBuilder();
for (String s : formattedData) {
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/com/pnuema/java/barcode/symbologies/Code39.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ private String encodeCode39() {
this.init_Code39();
this.init_ExtendedCode39();

String strNoAstr = getRawData().replace("*", "");
String strFormattedData = "*" + strNoAstr + (_EnableChecksum ? getChecksumChar(strNoAstr) : "") + "*";
String strNoAsterisk = getRawData().replace("*", "");
String strFormattedData = "*" + strNoAsterisk + (_EnableChecksum ? getChecksumChar(strNoAsterisk) : "") + "*";

if (_AllowExtended) {
InsertExtendedCharsIfNeeded(strFormattedData);
Expand Down Expand Up @@ -223,9 +223,9 @@ private void init_ExtendedCode39()
ExtC39_Translation.put("z", "+Z");
ExtC39_Translation.put(getChar(127), "%T"); //also %X, %Y, %Z
}
private void InsertExtendedCharsIfNeeded(String FormattedData) {
private String InsertExtendedCharsIfNeeded(String formattedData) {
StringBuilder output = new StringBuilder();
for (char c : FormattedData.toCharArray()) {
for (char c : formattedData.toCharArray()) {
try {
String s = C39_Code.get(c);
output.append(c);
Expand All @@ -236,18 +236,18 @@ private void InsertExtendedCharsIfNeeded(String FormattedData) {
}
}

FormattedData = output.toString();
return output.toString();
}

private char getChecksumChar(String strNoAstr) {
private char getChecksumChar(String strNoAsterisk) {
//checksum
String Code39_Charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%";
InsertExtendedCharsIfNeeded(strNoAstr);
String source = InsertExtendedCharsIfNeeded(strNoAsterisk);
int sum = 0;

//Calculate the checksum
for (int i = 0; i < strNoAstr.length(); ++i) {
sum = sum + Code39_Charset.indexOf(strNoAstr.toCharArray()[i]);
for (int i = 0; i < source.length(); ++i) {
sum = sum + Code39_Charset.indexOf(source.toCharArray()[i]);
}

//return the checksum char
Expand Down
28 changes: 14 additions & 14 deletions src/main/java/com/pnuema/java/barcode/symbologies/Code93.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,34 +137,34 @@ private void initCode93() {
C93_Code.add(new Entry( "40", "/", "101101110" ));
C93_Code.add(new Entry( "41", "+", "101110110" ));
C93_Code.add(new Entry( "42", "%", "110101110" ));
C93_Code.add(new Entry( "43", "(", "100100110" ));//dont know what character actually goes here
C93_Code.add(new Entry( "44", ")", "111011010" ));//dont know what character actually goes here
C93_Code.add(new Entry( "45", "#", "111010110" ));//dont know what character actually goes here
C93_Code.add(new Entry( "46", "@", "100110010" ));//dont know what character actually goes here
C93_Code.add(new Entry( "43", "(", "100100110" ));//don't know what character actually goes here
C93_Code.add(new Entry( "44", ")", "111011010" ));//don't know what character actually goes here
C93_Code.add(new Entry( "45", "#", "111010110" ));//don't know what character actually goes here
C93_Code.add(new Entry( "46", "@", "100110010" ));//don't know what character actually goes here
C93_Code.add(new Entry( "-", "*", "101011110" ));
}

private String addCheckDigits(String input) {
//populate the C weights
int[] aryCWeights = new int[input.length()];
int curweight = 1;
int currentWeight = 1;
for (int i = input.length() - 1; i >= 0; i--) {
if (curweight > 20) {
curweight = 1;
if (currentWeight > 20) {
currentWeight = 1;
}
aryCWeights[i] = curweight;
curweight++;
aryCWeights[i] = currentWeight;
currentWeight++;
}

//populate the K weights
int[] aryKWeights = new int[input.length() + 1];
curweight = 1;
currentWeight = 1;
for (int i = input.length(); i >= 0; i--) {
if (curweight > 15) {
curweight = 1;
if (currentWeight > 15) {
currentWeight = 1;
}
aryKWeights[i] = curweight;
curweight++;
aryKWeights[i] = currentWeight;
currentWeight++;
}

//calculate C checksum
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public String getAssigningCountry() {
}

/**
* Encode the raw data using the EAN-13 algorithm. (Can include the checksum already. If it doesnt exist in the data then it will calculate it for you. Accepted data lengths are 12 + 1 checksum or just the 12 data digits)
* Encode the raw data using the EAN-13 algorithm. (Can include the checksum already. If it doesn't exist in the data then it will calculate it for you. Accepted data lengths are 12 + 1 checksum or just the 12 data digits)
* @return Encoded value
*/
private String encodeEAN13()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.pnuema.java.barcode.symbologies;

import com.pnuema.java.barcode.BarcodeCommon;
import com.pnuema.java.barcode.EncodingType;
import com.pnuema.java.barcode.utils.Utils2of5;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private String encodeISBNBookland() {
setRawData(getRawData().substring(0, 12));
}

//check to see if its an unknown type
//check to see if it is an unknown type
if (type == null) {
error("EBOOKLANDISBN-2: Invalid input. Must start with 978 and be length must be 9, 10, 12, 13 characters.");
}
Expand Down
Loading

0 comments on commit b14b7a5

Please sign in to comment.