1. 程式人生 > >使用JBarcode生成一維碼

使用JBarcode生成一維碼

cep string () span des wstring body idt 文本

需要的jar包,只有jbarcode.jar

鏈接: https://pan.baidu.com/s/1o9oDPB8 密碼: x367

public class Main {

    //設置條形碼高度    
    private static final int BARCODE_HEIGHT = 40;    
    //設置條形碼默認分辨率    
    private static final int BARCODE_DPI = ImageUtil.DEFAULT_DPI;    
    //設置條形碼字體樣式    
    private static final String FONT_FAMILY = "console";    
    
//設置條形碼字體大小 private static final int FONT_SIZE = 15; //設置條形碼文本 public static String TEXT = ""; //創建jbarcode private static JBarcode jbc = null; public static void main(String[] args) { List<String> list=new ArrayList<String>(); list.add(
"KJ4.1-0127-0001"); list.add("KJ4.1-0128-0001"); list.add("KJ4.1-0129-0001"); list.add("KJ4.1-0130-0001"); if(list!=null && list.size()>0){ for(String message:list){ Main.createBarcode(message, new File("D:\\test\\"+message+".png"),"No"); } } }
public static JBarcode getJBarcode() throws InvalidAtributeException { /** * 參考設置樣式: *barcode.setEncoder(Code128Encoder.getInstance()); //設置編碼 *barcode.setPainter(WidthCodedPainter.getInstance());// 設置Painter *barcode.setTextPainter(BaseLineTextPainter.getInstance()); //設置TextPainter *barcode.setBarHeight(17); //設置高度 *barcode.setWideRatio(Double.valueOf(30).doubleValue());// 設置寬度比率 *barcode.setXDimension(Double.valueOf(2).doubleValue()); // 設置尺寸,大小 密集程度 *barcode.setShowText(true); //是否顯示文本 *barcode.setCheckDigit(true); //是否檢查數字 *barcode.setShowCheckDigit(false); //是否顯示檢查數字 */ if (jbc == null) { //生成code128 jbc = JBarcodeFactory.getInstance().createCode128(); jbc.setEncoder(Code128Encoder.getInstance()); jbc.setTextPainter(CustomTextPainter.getInstance()); jbc.setBarHeight(BARCODE_HEIGHT); jbc.setXDimension(Double.valueOf(0.8).doubleValue()); jbc.setShowText(true); } return jbc; } /** * @descript:生成條形碼文件 * @param message 條形碼內容 * @param file 生成文件 */ public static void createBarcode(String message, File file,String text) { try { FileOutputStream fos = new FileOutputStream(file); createBarcode(message, fos,text); fos.close(); } catch (IOException e) { throw new RuntimeException(e); } } /** * @descript:生成條形碼並寫入指定輸出流 * @param message 條形碼內容 * @param os 輸出流 */ public static void createBarcode(String message, OutputStream os,String text) { try { //設置條形碼文本 TEXT=text; //創建條形碼的BufferedImage圖像 BufferedImage image = getJBarcode().createBarcode(message); ImageUtil.encodeAndWrite(image, ImageUtil.PNG, os, BARCODE_DPI, BARCODE_DPI); os.flush(); } catch (Exception e) { throw new RuntimeException(e); } } /** * 靜態內部類 * 自定義的 TextPainter, 允許定義字體,大小,文本等 * 參考底層實現:BaseLineTextPainter.getInstance() */ protected static class CustomTextPainter implements TextPainter { private static CustomTextPainter instance =new CustomTextPainter(); public static CustomTextPainter getInstance() { return instance; } public void paintText(BufferedImage barCodeImage, String text, int width) { //繪圖 Graphics g2d = barCodeImage.getGraphics(); //創建字體 Font font = new Font(FONT_FAMILY, Font.PLAIN, FONT_SIZE * width); g2d.setFont(font); FontMetrics fm = g2d.getFontMetrics(); int height = fm.getHeight(); int center = (barCodeImage.getWidth() - fm.stringWidth(text)) / 2; g2d.setColor(Color.WHITE); g2d.fillRect(0, 0, barCodeImage.getWidth(), barCodeImage.getHeight() * 1 / 20); g2d.fillRect(0, barCodeImage.getHeight() - (height * 9 / 10), barCodeImage.getWidth(), (height * 9 / 10)); g2d.setColor(Color.BLACK); //繪制文本 g2d.drawString(TEXT, 0, 145); //繪制條形碼 g2d.drawString(text, center, barCodeImage.getHeight() - (height / 10) - 2); } } }

使用JBarcode生成一維碼