1、Zxing(之前已经实现,效果不理想)
2、Barcode4j
3、JBarcode
4、Barbecue
---->以上是笔者用过的包,其中Barcode4j/JBarcode 较为灵活
----->下面是JBarcode生成条码及打印 (条码纸长:40mm 宽30mm 下面代码只能打印短位条码,第二篇将带来另一种设置打印机的方式(可打长码))
import java.awt.BasicStroke;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.jbarcode.JBarcode;
import org.jbarcode.encode.Code128Encoder;
import org.jbarcode.encode.Code39Encoder;
import org.jbarcode.encode.EAN13Encoder;
import org.jbarcode.paint.BaseLineTextPainter;
import org.jbarcode.paint.EAN13TextPainter;
import org.jbarcode.paint.WideRatioCodedPainter;
import org.jbarcode.paint.WidthCodedPainter;
import org.jbarcode.util.ImageUtil;
import com.ms.common.contant.util.ThreeDes;
public class BarCodeImage2 implements Printable{
/**
* 生成商品条形码
*
* @param filePath
* 商品条形码图片存放路径:../xxx/yyy/
* @param jbarCode
* 商品条形码:8位、13位
* @param format
* 商品条形码图片格式:.gif/.png/.jpg/.jpeg
* @return 图片存放路径+图片名称+图片文件类型
*/
public static String code;
public static String url =new ThreeDes().getProp().getProperty("barcode.url");
public String createBarCode(String filePath, String jbarCode, String format) {
System.out.println(url);
code = jbarCode ;
System.out.println(code);
String barCodeName = jbarCode + format;
try {
BufferedImage bi = null;
int len = jbarCode.length();
if (len == 12) {
} else if (len == 13) {
int backCode = checkCode(jbarCode);
// int oldCode = Integer.parseInt(jbarCode.substring(len - 1, len));
// if (oldCode != backCode) {
// return null;
// }
// barCode = jbarCode.substring(0, jbarCode.length() - 1);
}
JBarcode localJBarcode13 = new JBarcode(Code128Encoder.getInstance(),
WidthCodedPainter.getInstance(),
BaseLineTextPainter.getInstance());
//localJBarcode13.setWideRatio(0.1);
//localJBarcode13.setXDimension(0.21);
//localJBarcode13.setBarHeight(40);
//localJBarcode13.setXDimension(0.22f);
//localJBarcode13.setCheckDigit(false);
bi = localJBarcode13.createBarcode(jbarCode);
saveToFile(bi, filePath, barCodeName, "jpeg");
/* localJBarcode13.setEncoder(EAN13Encoder.getInstance());
localJBarcode13.setPainter(WideRatioCodedPainter.getInstance());
localJBarcode13.setTextPainter(EAN13TextPainter.getInstance());
localJBarcode13.setShowCheckDigit(false);*/
// printBarCode();
return filePath + barCodeName;
} catch (Exception localException) {
localException.printStackTrace();
return null;
}
}
/**
* 保存图片文件
*
* @param paramBufferedImage
* 图片流
* @param filePath
* 文件路径
* @param imgName
* 图片参数
* @param imgFormat
* 图片格式
*/
private void saveToFile(BufferedImage paramBufferedImage, String filePath,
String imgName, String imgFormat) {
try {
FileOutputStream fileOutputStream = null;
try {
File dirFile = new File(filePath);
if (!dirFile.exists()) {
dirFile.mkdirs();
}
String imgPath = filePath + "/" + imgName;
fileOutputStream = new FileOutputStream(imgPath);
} catch (Exception e) {
System.out.println("Create Img File Error:" + e.toString());
}
ImageUtil.encodeAndWrite(paramBufferedImage, imgFormat,
fileOutputStream, 96, 96);
fileOutputStream.close();
} catch (Exception localException) {
System.out.println("Save Img File Error:" + localException);
localException.printStackTrace();
}
}
// /**
// * 返回校验码
// *
// * @param code
// * 商品条形码
// * @return 校验码: -1:格式不正确,条形码为全部数字 -2:参数不能为空
// *
// */
// private int checkCode(String code) {
// int checkCode = -1;
// if (code == null || "".equals(code)) {
// return -2;
// } else if (!Pattern.compile("^[0-9]*$").matcher(code).matches()) {
// checkCode = -1;
// } else {
// try {
// int evensum = 0; // 偶数位的和
// int oddsum = 0; // 奇数位的和
// evensum += Integer.parseInt(code.substring(11, 12));
// evensum += Integer.parseInt(code.substring(9, 10));
// evensum += Integer.parseInt(code.substring(7, 8));
// evensum += Integer.parseInt(code.substring(5, 6));
// evensum += Integer.parseInt(code.substring(3, 4));
// evensum += Integer.parseInt(code.substring(1, 2));
// evensum *= 3;
// oddsum += Integer.parseInt(code.substring(10, 11));
// oddsum += Integer.parseInt(code.substring(8, 9));
// oddsum += Integer.parseInt(code.substring(6, 7));
// oddsum += Integer.parseInt(code.substring(4, 5));
// oddsum += Integer.parseInt(code.substring(2, 3));
// oddsum += Integer.parseInt(code.substring(0, 1));
// int sum = evensum + oddsum;
// int ck = 0;
// if (sum % 10 == 0) {
// ck = sum;
// } else {
// ck = (sum / 10 + 1) * 10;
// }
// checkCode = ck - sum;
// } catch (NumberFormatException e) {
// System.out.println("BarCode Format Error:" + e.toString());
// } catch (Exception e) {
// System.out.println("Get Check Code Error:" + e.toString());
// }
// }
// return checkCode;
// }
public void printBarCode(){
// 通俗理解就是书、文档
Book book = new Book();
// 设置成竖打
PageFormat pf = new PageFormat();
pf.setOrientation(PageFormat.PORTRAIT);
// 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
Paper p = new Paper();
p.setSize(182,80);// 纸张大小
p.setImageableArea(-10, 10, 1217, 100);// 设置打印区域
pf.setPaper(p);
// 把 PageFormat 和 Printable 添加到书中,组成一个页面
book.append(new BarCodeImage2(), pf);
// 获取打印服务对象
PrinterJob job = PrinterJob.getPrinterJob();
// 设置打印类
job.setPageable(book);
try {
// 可以用printDialog显示打印对话框,在用户确认后打印;也可以直接打印
// boolean a=job.printDialog();
// if(a)
//{
job.print();
//}
} catch (PrinterException e) {
e.printStackTrace();
}
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
new BarCodeImage2().createBarCode("D:/barcode/","(-_-)zz..",".jpeg");
// 通俗理解就是书、文档 aa00000188
Book book = new Book();
// 设置成竖打
PageFormat pf = new PageFormat();
pf.setOrientation(PageFormat.PORTRAIT);
// 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
Paper p = new Paper();
p.setSize(113,85);// 纸张大小localJBarcode13 182,80
p.setImageableArea(-8, 10, 30, 50);// 设置打印区域
pf.setPaper(p);
// 把 PageFormat 和 Printable 添加到书中,组成一个页面
book.append(new BarCodeImage2(), pf);
// 获取打印服务对象
PrinterJob job = PrinterJob.getPrinterJob();
// 设置打印类
job.setPageable(book);
try {
// 可以用printDialog显示打印对话框,在用户确认后打印;也可以直接打印
// boolean a=job.printDialog();
// if(a)
//{
job.print();
//}
} catch (PrinterException e) {
e.printStackTrace();
}
}
/**
* @param Graphic指明打印的图形环境
* @param
* PageFormat指明打印页格式(页面大小以点为计量单位,1点为1英才的1/72,1英寸为25.4毫米。A4纸大致为595×842点)
* @param pageIndex指明页号
*/
/* (non-Javadoc)
* @see java.awt.print.Printable#print(java.awt.Graphics, java.awt.print.PageFormat, int) url + code + ".png"
*/
public int print(Graphics gra, PageFormat pf, int pageIndex)throws PrinterException {
Component c = null;
Graphics2D g2 = (Graphics2D) gra;
// 打印起点坐标
double x = pf.getImageableX();
double y = pf.getImageableY();
switch (pageIndex) {
case 0:
g2.setStroke(new BasicStroke(0.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1f, null, 0.0f));
Image src = Toolkit.getDefaultToolkit().getImage(url + code + ".jpeg");
g2.drawImage(src, (int) x, (int) y, c);
System.out.println(url+code+".jpeg");
return PAGE_EXISTS;
default:
return NO_SUCH_PAGE;
}
}
}