JavaScript控制打印机打印条形码----条形码产生部分

文章介绍了三种在Web环境下实现条形码打印的方法:一是利用barbecue-1.5-beta1框架,通过后台生成并在jsp页面显示;二是使用barcode4j,同样后台生成,涉及多个库文件;三是前端实现的jquery-barcode插件,直接在浏览器端生成条形码,但可能无法通过打印机直接打印。
摘要由CSDN通过智能技术生成

    要真正的要用b/s架构实现web打印,相了很长时间。总算实现了。但还是有些小问题。跟大家共享一下

   

    首先实现条形码的部分

    我首先采用的是java做后台的web project。即将生成条形码的功能在后台实现,后台再将产生的条形码返回到前台显示。在这里我主要用了两个框架

   

    第一种:barbecue-1.5-beta1

    整個jar包占得空間很少,下載的zip也只有大概860kb,解壓之後可以看到一個barbecue-1.5-beta1.jar。我們開發只需这一个就可以了。挺轻量的。

    将上面的jar包放到webRoot的bin目录下

    在web.xml加入下面代码:

    <servlet>
      <servlet-name>barbecueServlet</servlet-name>
      <servlet-class>net.sourceforge.barbecue.BarcodeServlet</servlet-class>
    </servlet>
 
    <servlet-mapping>
      <servlet-name>barbecueServlet</servlet-name>
      <url-pattern>/barbecue</url-pattern>
    </servlet-mapping>

    在你的jsp页面中加入下面一句

    <img src="<%=request.getContextPath()%>/barbecue?data=<%=request.getParameter("code")%>&type=code128" height="50px" width=200px/>

    src中前半部分就不用说了(servlet的东西),

   后面的参数:

   data(必選的,其他的可選):要显示的条形码数值

   type:采用什么条形码国际标准。支持一下格式: 

  • Code128 (默認)
  • Code128A
  • Code128B
  • Code128C
  • EAN128
  • USPS
  • ShipmentIdentificationNumber
  • SSCC18
  • SCCC14ShippingCode
  • GlobalTradeItemNumber
  • UCC128
  • PDF417
  • Code39
  • 3of9
  • USD3
  • Codabar
  • USD4
  • NW7
  • Monarch
  • 2of7
  •   

        appId: 只有 UCC128 格式需要.

        width: 最小的 bar 寬度 pixels.

        height: 輸出的圖檔高度 pixels.

        resolution: 輸出的圖檔解析度 dpi.

        headless: 預設為 true, 當設為 false 時將不會同時繪出文字, 但是有些 type 即使設為 true 也不會繪出文字. (1.0.6RC1 此處有bug)

        drawText: 預設為 false, 當設為 true 時將增加一個檢查碼, 只有 Code39 格式有用.

      優點:比較輕量。

      缺點:按照上面的解釋,好像當headless=false時,drawText為true時可以在條形碼下顯示對應的值可是沒顯示,不知道原因

    下面是英文的说明:

      Servlet class: net.sourceforge.barbecue.BarcodeServlet

    Required parameters:
    --------------------

    data     the data to encode


    Optional parameters:
    --------------------

    type    the barcode type - see below
    width    the width of the smallest bar in the barcode in pixels
    height    the height of the barcode in pixels
    resolution   the output resolution in DPI
    checksum   true to include a calculated checksum, false to omit (only used by Code39 and derivatives)
    headless   true for headless mode (default value), false to force non-headless mode
    drawText   (only takes effect if headless = false) true to draw text of barcode, false to omit


    Types:
    ------

    Valid barcode types are (case insensitive):

    Code128
    Code128A
    Code128B
    Code128C
    EAN128
    USPS
    ShipmentIdentificationNumber
    SSCC18
    SCCC14ShippingCode
    GlobalTradeItemNumber
    UCC128      Note: UCC128 requires an additional parameter: appid
    PDF417
    Code39
    3of9
    USD3
    Codabar
    Std2of5
    Int2of5
    USD4
    NW7
    Monarch
    2of7
    PostNet
    UPCA
    RandomWeightUPCA

    Note that if the type is omitted then the type will default to Code128 (optimal).

    Examples:
    ---------

    BarcodeServlet?data=12345
    BarcodeServlet?data=12345&width=3&height=100&resolution=100
    BarcodeServlet?data=12345&drawText=yes
    BarcodeServlet?data=12345&type=Code128B
    BarcodeServlet?data=12345&type=UCC128&appid=402
    BarcodeServlet?data=12345&type=PDF417
    BarcodeServlet?data=12345&headless=false&drawText=true

    Servlet Container Configuration:
    --------------------------------

    When running the servlet in headless mode (the default) you must also set the Java VM
    running the servlet to use headless mode, by setting the following VM parameter:

    -Djava.awt.headless=true

    第二种:barcode4j

    下載barcode4j-2.0-bin.zip,這個估計有7M多

    需要用到的包有avalon-framework-4.2.0.jar,barcode4j.jar,commons-cli-1.0.jar

    在web.xml中添加代碼

      <servlet>
      <servlet-name>barbecueServlet</servlet-name>
      <servlet-class>net.sourceforge.barbecue.BarcodeServlet</servlet-class>
      </servlet>
     
      <servlet-mapping>
      <servlet-name>barbecueServlet</servlet-name>
      <url-pattern>/barbecue</url-pattern>
      </servlet-mapping>

    在jsp頁面中添加

       <img src="<%=request.getContextPath()%>/barcode?msg=<%=request.getParameter("code")%>&type=code128&fmt=jpeg" height="50px" width=200px/>

    參數說明

    msg:打印的數據

    type:類型

    fmt:打印是以什麽格式打印

    還有很多參數,網上可以找到解釋

    上面兩種方式都是通過后台执行产生条形码的。在前台产生的可以使用jquery-barcode

    第三种:jquery-barcode方式

    首先下载jquery-barcode

    http://barcode-coder.com/en/barcode-jquery-plugin-201.html

    添加

    1. <script type="text/javascript" src="jquery-1.3.2.min.js"></script>     
    2. <script type="text/javascript" src="jquery-barcode.js"></script> 

    在body中添加

      <div id="barcodeTarget" class="barcodeTarget"></div>

    <script>$("#barcodeTarget").barcode("1234567", "ean8");</script>

    注意:顺序不能变

    具体的使用可以看上面的链接

    三种方式比较:

    前两种都是以图片方式显示的,所以打印机可以打印出来,后面一种是通过css样式定义出来的,所以不能通过打印机打印(其实可以以bmp方式打印,但是官网上说不能在IE下使用,无语,期待升级)。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
您可以使用一些 Java条形码生成库(如Zxing、Barbecue等)来生成多个条形码图片,并将这些图片返回给前台进行预览和打印。以下是一个简单的实现示例: ```java import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.WriterException; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; @Controller @RequestMapping("/barcode") public class BarcodeController { @GetMapping("/generate") @ResponseBody public List<String> generateBarcodes(@RequestParam("data") List<String> data, HttpServletResponse response) throws IOException { List<String> barcodeUrls = new ArrayList<>(); for (String d : data) { // 生成条形码 BufferedImage barcodeImage = generateBarcode(d); // 将图片转换为BASE64编码字符串 String barcodeUrl = imageToBase64(barcodeImage); barcodeUrls.add(barcodeUrl); } return barcodeUrls; } private BufferedImage generateBarcode(String data) { int width = 300; int height = 100; String format = "png"; BarcodeFormat barcodeFormat = BarcodeFormat.CODE_128; ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L; // 设置条形码参数 com.google.zxing.EncodeHintType hintType = EncodeHintType.ERROR_CORRECTION; com.google.zxing.EncodeHintType encodingType = EncodeHintType.CHARACTER_SET; com.google.zxing.EncodeHintType marginType = EncodeHintType.MARGIN; int margin = 2; java.util.Map<com.google.zxing.EncodeHintType, Object> hints = new java.util.HashMap<>(); hints.put(hintType, errorCorrectionLevel); hints.put(encodingType, "UTF-8"); hints.put(marginType, margin); BitMatrix bitMatrix = null; try { bitMatrix = new com.google.zxing.MultiFormatWriter().encode(data, barcodeFormat, width, height, hints); } catch (WriterException e) { e.printStackTrace(); } BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix); return image; } private String imageToBase64(BufferedImage image) throws IOException { String base64 = null; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try { ImageIO.write(image, "png", outputStream); byte[] bytes = outputStream.toByteArray(); base64 = java.util.Base64.getEncoder().encodeToString(bytes); } finally { outputStream.close(); } return base64; } } ``` 在上面的示例中,我们使用了Zxing库生成条形码,将生成的条形码图片转换为BASE64编码字符串,并将多个条形码的URL返回给前台。您可以使用JavaScript前端工具将这些URL转换为图片进行预览和打印
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ok060

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值