integrate Barcode4J in a Java application

   Barcode4J资源地址: http://barcode4j.sourceforge.net/index.html
一、using the JavaBean API
	public static void generateCode128Barcode(File file, String code) {
		Code128Bean bean = new Code128Bean();
		final int dpi = 150;
		
		//barcode
		bean.setModuleWidth(0.21);
		bean.setHeight(15);
		bean.doQuietZone(true);
		bean.setQuietZone(2);//两边空白区
		//human-readable
		bean.setFontName("Helvetica");
		bean.setFontSize(3);
		bean.setMsgPosition(HumanReadablePlacement.HRP_BOTTOM);
		
		OutputStream out = null;

		try {
			out = new FileOutputStream(file);

			BitmapCanvasProvider canvas = new BitmapCanvasProvider(out,
					"image/jpeg", dpi, BufferedImage.TYPE_BYTE_BINARY, true, 0);
			bean.generateBarcode(canvas, code);
			canvas.finish();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null)
					out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	public static void generateCode39Barcode(int mode, File file, String code) {
		Code39Bean bean = new Code39Bean();
		// Dot Per Inch每英寸所打印的点数或线数,用来表示打印机打印分辨率。
		final int dpi = 150;
		// bean.setModuleWidth(UnitConv.in2mm(1.0f / dpi));
		bean.setModuleWidth(0.2);
		bean.setHeight(15);

		bean.setWideFactor(3);
		bean.doQuietZone(true);

		OutputStream out = null;

		try {
			out = new FileOutputStream(file);

			if (mode == 0) {
				BitmapCanvasProvider canvas = new BitmapCanvasProvider(out,
						"image/jpeg", dpi, BufferedImage.TYPE_BYTE_GRAY, false,
						0);

				bean.generateBarcode(canvas, code);

				canvas.finish();

			} else {
				BitmapCanvasProvider canvas = new BitmapCanvasProvider(dpi,
						BufferedImage.TYPE_BYTE_GRAY, true, 0);
				bean.generateBarcode(canvas, code);
				canvas.finish();
				BufferedImage barcodeImage = canvas.getBufferedImage();

				ImageIO.write(barcodeImage, "jpg", out);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null)
					out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

二、use XML API
1.code39.xml
<?xml version="1.0" encoding="UTF-8"?>
<barcode>
  <code39>
      <height>15mm</height>
      <module-width>0.19mm</module-width>
      <wide-factor>2.5</wide-factor>
      <interchar-gap-width>1mw</interchar-gap-width>
      <quiet-zone enabled="true">10mw</quiet-zone>
      <checksum>ignore</checksum>
      <human-readable>
        <placement>bottom</placement>
        <font-name>Helvetica</font-name>
        <font-size>8pt</font-size>
        <display-start-stop>false</display-start-stop>
        <display-checksum>false</display-checksum>
      </human-readable>
  </code39>
</barcode>

2.java
	/**
	 * 不同的类型,其属性定义有所有同,最好是加载xml文件的方式来配置
	 */
	public static Configuration buildCfg(String barcode_type) {
		DefaultConfiguration cfg = new DefaultConfiguration("barcode");
		DefaultConfiguration barcodeType = new DefaultConfiguration(barcode_type);

		/** *********属性设置************* */
		addChild(barcodeType,"bar-height",15);
		addChild(barcodeType,"module-width","0.19");
		addChild(barcodeType,"quiet-zone",10);
		addChild(barcodeType,"wide-factor","2.5");
		addChild(barcodeType,"interchar-gap-width",1);
		
		DefaultConfiguration humanReadable = new DefaultConfiguration("human-readable");
		addChild(humanReadable,"placement","bottom");
		addChild(humanReadable,"font-name","Helvetica");
		addChild(humanReadable,"font-size","3mm");
		
		barcodeType.addChild(humanReadable);
		cfg.addChild(barcodeType);

		return cfg;
	}

	/**
	 * 添加子节点
	 * @param parent
	 * @param attrName
	 * @param attrValue
	 */
	public static void addChild(DefaultConfiguration parent,String attrName,Object attrValue){
		DefaultConfiguration attr;
		attr = new DefaultConfiguration(attrName);
		if(attrValue instanceof String) {
			attr.setValue((String)attrValue);
		}else{
			attr.setValue((Integer)attrValue);
		}
		parent.addChild(attr);
	}

	/**
	 * 加载xml配置的条形码属性文件
	 * @param file
	 * @return
	 */
	public static Configuration buildCfgFromFile(File file) {
		DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
		Configuration cfg = null;
		try {
			cfg = builder.buildFromFile(file);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cfg;
	}

	/**
	 * 生成条形码
	 * @param barcodeType
	 * @param code
	 * @param file
	 */
	public static void generateBarcode(String barcodeType, String code,
			File file) {
		byte[] data;
		ByteArrayOutputStream baos = null;
		BitmapCanvasProvider bitmap = null;
		String FORMAT = MimeTypes.MIME_JPEG;
		int RESOLUTION = 150;
		int ORIENTATION = 0;
		try {
			//加载文件方式
			//Configuration cfg = buildCfgFromFile(getResourceFile(barcodeType.concat(".xml")));
			
			Configuration cfg = buildCfg(barcodeType);//程序中的配置属性
			BarcodeUtil util = BarcodeUtil.getInstance();
			BarcodeGenerator gen = util.createBarcodeGenerator(cfg);

			baos = new ByteArrayOutputStream();

			bitmap = new BitmapCanvasProvider(baos, FORMAT, RESOLUTION,
					BufferedImage.TYPE_BYTE_GRAY, true, ORIENTATION);

			gen.generateBarcode(bitmap, code);
			bitmap.finish();

			data = baos.toByteArray();
			FileOutputStream out = new FileOutputStream(file);
			out.write(data);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (baos != null) {
					baos.close();
				}
				bitmap = null;
			} catch (Exception e) {
			}
		}

	}

	/**
	 * 取资源文件
	 * @param fileName
	 * @return
	 */
	public static File getResourceFile(String fileName) {
		String path = ClassLoader.getSystemResource("").getPath().substring(1) + fileName;
		return new File(path);
	}

3.测试
	public static void main(String[] args) {
		String code = "ISN8859-52036";
		generateCode39Barcode(0, new File("d:/barcode/code39_0.jpg"), code);
		generateCode39Barcode(1, new File("d:/barcode/code39_1.jpg"), code);
		generateCode128Barcode(new File("d:/barcode/code128.jpg"), code);

		generateBarcode("code39", code, new File("d:/barcode/code39.jpg"));
	}

你可能感兴趣的:(java,xml,.net,bean)