一、前言
通过avalon-framework(4.2.0)、barcode4j.jar包定义Barcode条形码生成器类,生成图形加文字数组编码图形用于条形码打印机打印输出。
二、代码示例
import java.awt.image.BufferedImage;@b@import java.io.ByteArrayOutputStream;@b@import java.io.File;@b@import java.io.FileOutputStream;@b@import java.io.IOException;@b@@b@import org.krysalis.barcode4j.HumanReadablePlacement;@b@import org.krysalis.barcode4j.TextAlignment;@b@import org.krysalis.barcode4j.impl.code128.Code128Bean;@b@import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;@b@import org.krysalis.barcode4j.tools.MimeTypes;@b@@b@public class Barcode { @b@ @b@ private static double QUIET_ZONE = 4.0 ;@b@ private static double VERTICAL_QUIET_ZONE = 5.0 ;@b@ private static double BAR_HEIGHT = 20 ;@b@ private static double MODULE_WIDTH = 0.4 ;@b@ private static double FONT_SIZE=4;@b@ private static int DPI=300;@b@ @b@ public static void main(String[] args) { @b@ Barcode t =new Barcode(); @b@ t.getBarcode("s7813243124312340","小木人印象"); @b@ } @b@ @b@ public void getBarcode(String text,String prodName) { @b@ @b@ Code128Bean bean = new Code128Bean(); @b@ //barcode @b@ bean.setModuleWidth(MODULE_WIDTH); @b@ bean.setBarHeight(BAR_HEIGHT);@b@ bean.doQuietZone(true);@b@ bean.setQuietZone(QUIET_ZONE);//两边空白区@b@ bean.setVerticalQuietZone(VERTICAL_QUIET_ZONE);@b@ //human-readable @b@ bean.setFontName("Helvetica"); @b@ bean.setMsgPosition(HumanReadablePlacement.HRP_NONE); @b@ @b@ @b@ String format = MimeTypes.MIME_JPEG;@b@ ByteArrayOutputStream bout = null; @b@ try { @b@ bout = new ByteArrayOutputStream(4096); @b@ @b@ int orientation = 0; @b@ BitmapCanvasProvider bitmap = new BitmapCanvasProvider(bout, @b@ format, DPI, BufferedImage.TYPE_BYTE_BINARY, false, @b@ orientation); @b@ bean.generateBarcode(bitmap, text);@b@ bitmap.deviceText(prodName, QUIET_ZONE, QUIET_ZONE, VERTICAL_QUIET_ZONE-1, null, FONT_SIZE, TextAlignment.TA_LEFT);@b@ bitmap.deviceText(text, QUIET_ZONE, QUIET_ZONE, BAR_HEIGHT + VERTICAL_QUIET_ZONE*2-1, null, FONT_SIZE, TextAlignment.TA_LEFT);@b@ bitmap.finish();@b@ File file = new File("c://temp//"+text+".jpg"); @b@ FileOutputStream fos = new FileOutputStream(file); @b@ fos.write(bout.toByteArray(), 0, bout.size()); @b@ } @b@ catch (Exception e) { @b@ e.printStackTrace();@b@ } @b@ finally { @b@ if (bout != null) { @b@ try { @b@ bout.close(); @b@ } @b@ catch (IOException e) { @b@ e.printStackTrace(); @b@ } @b@ } @b@ } @b@ @b@ } @b@@b@}