Created
July 26, 2016 13:11
-
-
Save xSAVIKx/30c1ba4f4056412b49697bf0d54ac6b1 to your computer and use it in GitHub Desktop.
BarcodeGenerator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.imageio.ImageIO; | |
import java.awt.*; | |
import java.awt.image.BufferedImage; | |
import java.awt.image.RenderedImage; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.HashMap; | |
public class BarcodeGenerator { | |
public static void main(String[] args) throws IOException { | |
BarcodeGenerator barcodeGenerator = new BarcodeGenerator(2, 100); | |
RenderedImage barcodeImage = barcodeGenerator.generateBarcodeImage("123456789"); | |
File file = new File("barcode.png"); | |
ImageIO.write(barcodeImage, "png", file); | |
} | |
private static final int DEFAULT_BARCODE_LINE_WIDTH = 5; | |
private static final int DEFAULT_BARCODE_LINE_HEIGHT = 100; | |
private final int barcodeLineWidth; | |
private final int barcodeLineHeight; | |
public BarcodeGenerator() { | |
this.barcodeLineWidth = DEFAULT_BARCODE_LINE_WIDTH; | |
this.barcodeLineHeight = DEFAULT_BARCODE_LINE_HEIGHT; | |
} | |
public BarcodeGenerator(int barcodeLineWidth, int barcodeLineHeight) { | |
this.barcodeLineWidth = barcodeLineWidth; | |
this.barcodeLineHeight = barcodeLineHeight; | |
} | |
public RenderedImage generateBarcodeImage(String message) { | |
Code39 code39 = new Code39(message); | |
int[] barcode = code39.barcode(); | |
int width = barcodeLineWidth * barcode.length; | |
int height = barcodeLineHeight; | |
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); | |
Graphics2D g2d = bufferedImage.createGraphics(); | |
for (int i = 0; i < barcode.length; i++) { | |
if (barcode[i] == 0) { | |
g2d.setColor(Color.WHITE); | |
} else { | |
g2d.setColor(Color.BLACK); | |
} | |
g2d.fillRect(i * barcodeLineWidth, 0, barcodeLineWidth, height); | |
} | |
g2d.dispose(); | |
return bufferedImage; | |
} | |
public static class Code39 { | |
private static final int INTEGERS_PER_SYMBOL = 12; | |
private String code; | |
private int[] bars; | |
public Code39(String code) { | |
this.code = code.toUpperCase().trim(); | |
bars = new int[(code.length() + 2) * INTEGERS_PER_SYMBOL]; | |
this.buildSequence(); | |
} | |
public void buildSequence() { | |
String encoded = "*" + code + "*"; | |
int p = 0; | |
for (int i = 0; i < encoded.length(); i++) { | |
int[] sequence = mapSequence(encoded.charAt(i), i); | |
System.arraycopy(sequence, 0, bars, p, sequence.length); | |
p += sequence.length; | |
} | |
} | |
public int[] mapSequence(char c, int pos) { | |
HashMap<Character, int[]> sequence = new HashMap<Character, int[]>(); | |
sequence.put('0', new int[]{1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1}); | |
sequence.put('1', new int[]{1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1}); | |
sequence.put('2', new int[]{1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1}); | |
sequence.put('3', new int[]{1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1}); | |
sequence.put('4', new int[]{1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1}); | |
sequence.put('5', new int[]{1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1}); | |
sequence.put('6', new int[]{1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1}); | |
sequence.put('7', new int[]{1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1}); | |
sequence.put('8', new int[]{1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1}); | |
sequence.put('9', new int[]{1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1}); | |
sequence.put('A', new int[]{1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1}); | |
sequence.put('B', new int[]{1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1}); | |
sequence.put('C', new int[]{1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1}); | |
sequence.put('D', new int[]{1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1}); | |
sequence.put('E', new int[]{1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1}); | |
sequence.put('F', new int[]{1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1}); | |
sequence.put('G', new int[]{1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1}); | |
sequence.put('H', new int[]{1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1}); | |
sequence.put('I', new int[]{1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1}); | |
sequence.put('J', new int[]{1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1}); | |
sequence.put('K', new int[]{1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1}); | |
sequence.put('L', new int[]{1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1}); | |
sequence.put('M', new int[]{1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1}); | |
sequence.put('N', new int[]{1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1}); | |
sequence.put('O', new int[]{1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1}); | |
sequence.put('P', new int[]{1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1}); | |
sequence.put('Q', new int[]{1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1}); | |
sequence.put('R', new int[]{1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1}); | |
sequence.put('S', new int[]{1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1}); | |
sequence.put('T', new int[]{1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1}); | |
sequence.put('U', new int[]{1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1}); | |
sequence.put('V', new int[]{1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1}); | |
sequence.put('W', new int[]{1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1}); | |
sequence.put('X', new int[]{1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1}); | |
sequence.put('Y', new int[]{1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1}); | |
sequence.put('Z', new int[]{1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1}); | |
sequence.put('-', new int[]{1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1}); | |
sequence.put('.', new int[]{1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1}); | |
sequence.put(' ', new int[]{1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1}); | |
sequence.put('$', new int[]{1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1}); | |
sequence.put('/', new int[]{1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1}); | |
sequence.put('+', new int[]{1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1}); | |
sequence.put('%', new int[]{1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1}); | |
sequence.put('*', new int[]{1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1}); | |
return sequence.get(c); | |
} | |
public int[] barcode() { | |
return bars; | |
} | |
public String code() { | |
return code; | |
} | |
public int calculateCheckDigit() { | |
return 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment