Java barcode generation technology-Barcode4j
Bar code encoding method-----The difference between Code 39 and code 128 barcodes
java generates barcode with JBarcode component
Java barcode generation (1D barcode)
Tools:
import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import javax.imageio.ImageIO; import org.apache.commons.lang.StringUtils; import org.jbarcode.JBarcode; import org.jbarcode.encode.Code128Encoder; import org.jbarcode.encode.EAN13Encoder; import org.jbarcode.encode.InvalidAtributeException; import org.jbarcode.paint.BaseLineTextPainter; import org.jbarcode.paint.EAN13TextPainter; import org.jbarcode.paint.WidthCodedPainter; import sun.misc.BASE64Encoder; public class BarcodeUtil { /** * 128 barcode * * @param strBarCode * Barcode: 0-100 digits * @param dimension * Product barcode: size * @param barheight * Product barcode: height * @return image (Base64 encoding) */ public static String generateBarCode128(String strBarCode,String dimension,String barheight) { try { ByteArrayOutputStream outputStream = null; BufferedImage bi = null; int len = strBarCode.length(); JBarcode productBarcode = new JBarcode(Code128Encoder.getInstance(), WidthCodedPainter.getInstance(), EAN13TextPainter.getInstance()); // size, area, size density productBarcode.setXDimension(Double.valueOf(dimension).doubleValue()); // Height 10.0 = 1cm default 1.5cm //productBarcode.setBarHeight(Double.valueOf(barheight).doubleValue()); // width //productBarcode.setWideRatio(Double.valueOf(30).doubleValue()); // whether to display the font productBarcode.setShowText(true); // show font style productBarcode.setTextPainter(BaseLineTextPainter.getInstance()); // generate QR code bi = productBarcode.createBarcode(strBarCode); outputStream = new ByteArrayOutputStream(); ImageIO.write(bi, "jpg", outputStream); BASE64Encoder encoder = new BASE64Encoder(); // System.err.println(encoder.encode(outputStream.toByteArray())); return encoder.encode(outputStream.toByteArray()); } catch (Exception e) { e.printStackTrace (); return "encodeError"; } } /** * Product barcode * @param strBarCode * Product barcode: 13 digits * @param dimension * Product barcode: size * @param barheight * Product barcode: height * @return image (Base64 encoding) */ public static String generateBarCode(String strBarCode,String dimension,String barheight) { // Is isNumeric a numeric value // Check. . . . . try { ByteArrayOutputStream outputStream = null; BufferedImage bi = null; int len = strBarCode.length(); JBarcode productBarcode = new JBarcode(EAN13Encoder.getInstance(), WidthCodedPainter.getInstance(), EAN13TextPainter.getInstance()); String barCode = strBarCode.substring(0, len - 1); String code = strBarCode.substring (len - 1, len); //check 13 digits String checkCode = productBarcode.calcCheckSum(barCode); if (!code.equals(checkCode)) { return "checkCodeError"; } // size, area, size productBarcode.setXDimension(Double.valueOf(dimension).doubleValue()); // Height 10.0 = 1cm default 1.5cm productBarcode.setBarHeight(Double.valueOf(barheight).doubleValue()); // width productBarcode.setWideRatio(Double.valueOf(25).doubleValue()); // Whether to check 13 digits, default false productBarcode.setShowCheckDigit(true); //Display whether the check code content is displayed in the string content // productBarcode.setShowCheckDigit(true); // generate QR code bi = productBarcode.createBarcode(barCode); outputStream = new ByteArrayOutputStream(); ImageIO.write(bi, "jpg", outputStream); BASE64Encoder encoder = new BASE64Encoder(); // System.err.println(encoder.encode(outputStream.toByteArray())); return encoder.encode(outputStream.toByteArray()); } catch (Exception e) { e.printStackTrace (); return "encodeError"; } } /** * @param args * @throws InvalidAtributeException */ public static void main(String[] args) throws InvalidAtributeException { //String encode = BarcodeUtil.generateBarCode("6936983800013","0.5","30"); String encode2 = BarcodeUtil.generateBarCode128("69369833450938430579753045230800013","0.5","30"); //System.out.println(encode); System.out.println(encode2); } }
See attachment for JAR package.