barcode4j能生成条形码,二维码,条形二维码等,这里我为大家展示条形二维码的代码(条形二维码国内参考资料太少,如各位好友有更好的方法请回帖)
barcode4j能生成条形码主要需要barcode4j.jar和avalon-framework-4.2.0.jar两个jar包(jar包你可以去barcode4j官网下载,也可以在我的资源中下载都什么免费的哦)
java代码:
import java.awt.p_w_picpath.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.configuration.DefaultConfiguration;
import org.krysalis.barcode4j.BarcodeException;
import org.krysalis.barcode4j.BarcodeGenerator;
import org.krysalis.barcode4j.BarcodeUtil;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
public class Barcode4jCreatePDF417 {
public static void main(String[] args) throws IOException, ConfigurationException, BarcodeException {
BarcodeUtil util = BarcodeUtil.getInstance();
BarcodeGenerator gen = util.createBarcodeGenerator(buildCfg("pdf417"));
OutputStream fout = new FileOutputStream("d:/pdf417.jpg");
int resolution = 200;
BitmapCanvasProvider canvas = new BitmapCanvasProvider( fout, "p_w_picpath/jpeg", resolution,
BufferedImage.TYPE_BYTE_BINARY, false, 0);
gen.generateBarcode(canvas, "400763000011"); canvas.finish();
}
private static Configuration buildCfg(String type) {
DefaultConfiguration cfg = new DefaultConfiguration("barcode");
//条码类型
DefaultConfiguration child = new DefaultConfiguration(type);
cfg.addChild(child);
//条形码位置
DefaultConfiguration attr = new DefaultConfiguration("human-readable");
DefaultConfiguration subAttr = new DefaultConfiguration("placement");
subAttr.setValue("bottom");
attr.addChild(subAttr);
child.addChild(attr);
return cfg;
}
}