barcode使用方法
JAR包:barcode4J.jar  avalon-framework-4.2.0.jar
l添加上:
<servlet>
        <servlet-name>BarcodeServlet</servlet-name>
        <servlet-class>lehot.jdt2.web.servlet.BarcodeServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>BarcodeServlet</servlet-name>
        <url-pattern>/barcode</url-pattern>
    </servlet-mapping>
创建一个BarcodeServlet,源码如下:
lehot.jdt2.web.servlet;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
l.transform.Result;
l.transform.Source;
l.transform.Transformer;
l.transform.TransformerFactory;
l.transform.dom.DOMSource;
l.transform.stream.StreamResult;
import org.apache.figuration.Configuration;
import org.apache.figuration.DefaultConfiguration;
import org.apache.avalon.framework.logger.ConsoleLogger;
import org.apache.avalon.framework.logger.Logger;
import org.krysalis.barcode4j.BarcodeGenerator;
import org.krysalis.barcode4j.BarcodeUtil;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import org.krysalis.barcode4j.output.eps.EPSCanvasProvider;
import org.krysalis.barcode4j.output.svg.SVGCanvasProvider;
import org.ls.MimeTypes;
import org.w3c.dom.DocumentFragment;
public class BarcodeServlet extends HttpServlet
{
  private static final long serialVersionUID = -1612710758060435089L;
  public static final String BARCODE_MSG = "msg";
  public static final String BARCODE_TYPE = "type";
  public static final String BARCODE_HEIGHT = "height";
  public static final String BARCODE_MODULE_WIDTH = "mw";
  public static final String BARCODE_WIDE_FACTOR = "wf";
  public static final String BARCODE_QUIET_ZONE = "qz";
  public static final String BARCODE_HUMAN_READABLE_POS = "hrp";
  public static final String BARCODE_FORMAT = "fmt";
  public static final String BARCODE_IMAGE_RESOLUTION = "res";
  public static final String BARCODE_IMAGE_GRAYSCALE = "gray";
  public static final String BARCODE_HUMAN_READABLE_SIZE = "hrsize";
  public static final String BARCODE_HUMAN_READABLE_FONT = "hrfont";
  public static final String BARCODE_HUMAN_READABLE_PATTERN = "hrpattern";
  private transient Logger log = new ConsoleLogger(1);
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
    try
    {
      String format = determineFormat(request);
      int orientation = 0;
      Configuration cfg = buildCfg(request);
      String msg = Parameter("msg");
      if (msg == null) {
        msg = "0123456789";
      }
      BarcodeUtil util = Instance();
      BarcodeGenerator gen = ateBarcodeGenerator(cfg);
      ByteArrayOutputStream bout = new ByteArrayOutputStream(4096);
      try {
        if (format.equals("image/svg+xml"))
        {
          SVGCanvasProvider svg = new SVGCanvasProvider(false, orientation);
          ateBarcode(svg, msg);
          DocumentFragment frag = DOMFragment();
          TransformerFactory factory = wInstance();
          Transformer trans = wTransformer();
          Source src = new DOMSource(frag);
svg canvas
          Result res = new StreamResult(bout);
          ansform(src, res);
        } else if (format.equals("image/x-eps")) {
          EPSCanvasProvider eps = new EPSCanvasProvider(bout, orientation);
          ateBarcode(eps, msg);
          eps.finish();
        } else {
          String resText = Parameter("res");
          int resolution = 300;
          if (resText != null) {
            resolution = Integer.parseInt(resText);
          }
          if (resolution > 2400) {
            throw new IllegalArgumentException("Resolutions above 2400dpi are not allowed");
          }
          if (resolution < 10) {
            throw new IllegalArgumentException("Minimum resolution must be 10dpi");
          }
          String gray = Parameter("gray");

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。