JAVA生成Code128條形碼,用於申通快遞單單號

package com.wl.cn.business.express.util;


import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import org.jbarcode.JBarcode;
import org.jbarcode.encode.Code128Encoder;
import org.jbarcode.paint.BaseLineTextPainter;
import org.jbarcode.paint.WideRatioCodedPainter;
import org.jbarcode.paint.WidthCodedPainter;
import org.jbarcode.util.ImageUtil;


public class PrintBarcodeUtil {
public static ByteArrayOutputStream getPrintBarcodeUtil(String str){
try {
JBarcode localJBarcode = new JBarcode(Code128Encoder.getInstance(),
WidthCodedPainter.getInstance(),
BaseLineTextPainter.getInstance());
BufferedImage localBufferedImage = localJBarcode.createBarcode(str);
//saveToPNG(localBufferedImage, "Test.png");
ByteArrayOutputStream bt=saveToFile(localBufferedImage, "Code128Encoder", "png");
localJBarcode.setEncoder(Code128Encoder.getInstance());
localJBarcode.setPainter(WideRatioCodedPainter.getInstance());
localJBarcode.setTextPainter(BaseLineTextPainter.getInstance());
localJBarcode.setShowCheckDigit(false);
// xx
str = "JBARCODE-39";
localBufferedImage = localJBarcode.createBarcode(str);
return bt;
} catch (Exception localException) {
localException.printStackTrace();
}
return null;
}
static ByteArrayOutputStream saveToFile(BufferedImage paramBufferedImage,
String paramString1, String paramString2) {
ByteArrayOutputStream localFileOutputStream=null;
try {
localFileOutputStream = new ByteArrayOutputStream();
ImageUtil.encodeAndWrite(paramBufferedImage, paramString2, localFileOutputStream, 96, 96); 
} catch (Exception localException) {
localException.printStackTrace();
}
return localFileOutputStream;
}

}



//我是生成後放入流中,自己試驗可以放入到一個fileout中

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章