条形码(一维码)工具类

package com.util;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.jbarcode.JBarcode;
import org.jbarcode.JBarcodeFactory;
import org.jbarcode.paint.TextPainter;
import org.jbarcode.util.ImageUtil;

public class BarCode {

	private static final int BARCODE_HEIGHT = 24;
	private static final int BARCODE_DPI = ImageUtil.DEFAULT_DPI;
	private static final String FONT_FAMILY = "consola";
	private static final int FONT_SIZE = 12;

	private static JBarcode jbc = null;

	static JBarcode getJBarcode() {
		if (jbc == null) {
			jbc = JBarcodeFactory.getInstance().createCode128();
			jbc.setTextPainter(CustomTextPainter.getInstance());
			jbc.setBarHeight(BARCODE_HEIGHT);
		}
		return jbc;
	}

	public static void createBarcode(String message, File file) {
		try {
			FileOutputStream fos = new FileOutputStream(file);
			createBarcode(message, fos);
			fos.close();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public static void createBarcode(String message, OutputStream os) {
		try {
			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, 允许定义字体,大小等等。
	 */
	static class CustomTextPainter implements TextPainter {
		public static CustomTextPainter instance = new CustomTextPainter();

		public static CustomTextPainter getInstance() {
			return instance;
		}

		@Override
		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, center, barCodeImage.getHeight() - (height / 10) - 2);
		}
	}

	public static void main(String[] args) {
		String imgRootPath="C:/";
		String imgRootURL="D:/";
		SimpleDateFormat format=new SimpleDateFormat("yyyyMMddHHmmssSSS");
		String timenow=format.format(new Date());
		String imgFolder=imgRootPath+"/upload/img/barcode/";
		String imgPath=imgRootPath+"/upload/img/barcode/"+timenow+".png";
		String imgURL=imgRootURL+"/upload/img/barcode/"+timenow+".png";
		System.out.println(imgPath);
		System.out.println(imgURL);
		new File(imgFolder).mkdirs();
		BarCode.createBarcode(timenow, new File(imgPath));
	}
}
  依赖jar包: jbarcode-0.2.8.jar
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值