- 浏览: 428627 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (170)
- java (77)
- javascript (5)
- jsp (1)
- servlet (6)
- struts (8)
- hibernate (3)
- spring (4)
- ajax (5)
- jquery (3)
- apache cxf (0)
- ext.js (1)
- hadoop (0)
- android (0)
- html5 (2)
- linux (5)
- flex (1)
- tomcat (1)
- jboss (0)
- nginx (0)
- mysql (16)
- sql server (3)
- oracle (4)
- div+css (0)
- mybatis (4)
- design patterns (22)
- xml (2)
- postgresql (3)
- velocity (1)
- freemarker (1)
- kendo-ui (2)
- ibatis (1)
- socket (1)
- C and C++ (1)
- C# (2)
- 程序设计----算法 (0)
- jersey (1)
- dd (0)
- perl (1)
- shell (0)
最新评论
-
书策稠浊:
兄弟,这tm是Java?
java调用百度地图和谷歌地图 -
fengyunlouyanyu:
jquery----删除指定id的div下的img -
yangjianzhouctgu:
Neoman 写道hi,我看你引入了kendo.web.min ...
kendo-ui中kendoGrid的用法 -
Neoman:
hi,我看你引入了kendo.web.min.js 这个js, ...
kendo-ui中kendoGrid的用法 -
yangjianzhouctgu:
llscp 写道这是JS吧...对的呀
java调用百度地图和谷歌地图
index.jsp代码如下:
BarcodeServlet.java的代码如下:
web.xml配置如下:
需要导入的包有:
当在地址栏中输入:http://localhost:8080/barCode后按下enter后
显示的结果为:
[img]
[/img]
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>barcode</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <table border="1"> <tr> <td> <h1> code39 </h1> <img src="<%=request.getContextPath()%>/barcodeServlet?msg=0123456789&type=code39" height="100px" width=300px /> </td> <td> <h1> code128 </h1> <img src="<%=request.getContextPath()%>/barcodeServlet?msg=0123456789&type=code128" height="100px" width=300px /> </td> <td> <h1> Codabar </h1> <img src="<%=request.getContextPath()%>/barcodeServlet?msg=0123456789&type=codabar" height="100px" width=300px /> </td> </tr> <tr> <td> <h1> intl2of5 </h1> <img src="<%=request.getContextPath()%>/barcodeServlet?msg=01234567890540&type=intl2of5" height="100px" width=300px /> </td> <td> <h1> upc-a </h1> <img src="<%=request.getContextPath()%>/barcodeServlet?msg=012345678912&type=upc-a" height="100px" width=300px /> </td> <td> <h1> ean-13 </h1> <img src="<%=request.getContextPath()%>/barcodeServlet?msg=200123457893&type=ean-13" height="100px" width=300px /> </td> <td> <h1> ean-8 </h1> <img src="<%=request.getContextPath()%>/barcodeServlet?msg=20123451&type=ean-8" height="100px" width=300px /> </td> </tr> <tr> <td> <h1> postnet </h1> <img src="<%=request.getContextPath()%>/barcodeServlet?msg=01234567890540&type=postnet" height="100px" width=300px /> </td> <td> <h1> royal-mail-cbc </h1> <img src="<%=request.getContextPath()%>/barcodeServlet?msg=012345AS678912&type=royal-mail-cbc" height="100px" width=300px /> </td> <td> <h1> pdf417 </h1> <img src="<%=request.getContextPath()%>/barcodeServlet?msg=200123457893&type=pdf417" height="100px" width=300px /> </td> <td> <h1> datamatrix </h1> <img src="<%=request.getContextPath()%>/barcodeServlet?msg=20123451&type=datamatrix" height="100px" width=300px /> </td> </tr> </table> </body> </html>
BarcodeServlet.java的代码如下:
/* * Copyright 2002-2007 Jeremias Maerki or contributors to Barcode4J, as applicable * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.krysalis.barcode4j.servlet; import java.awt.image.BufferedImage; 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; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import org.apache.avalon.framework.configuration.Configuration; import org.apache.avalon.framework.configuration.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.krysalis.barcode4j.tools.MimeTypes; /** * Simple barcode servlet. * * @version $Id: BarcodeServlet.java,v 1.6 2007/01/19 12:26:55 jmaerki Exp $ */ public class BarcodeServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; /** Parameter name for the message */ public static final String BARCODE_MSG = "msg"; /** Parameter name for the barcode type */ public static final String BARCODE_TYPE = "type"; /** Parameter name for the barcode height */ public static final String BARCODE_HEIGHT = "height"; /** Parameter name for the module width */ public static final String BARCODE_MODULE_WIDTH = "mw"; /** Parameter name for the wide factor */ public static final String BARCODE_WIDE_FACTOR = "wf"; /** Parameter name for the quiet zone */ public static final String BARCODE_QUIET_ZONE = "qz"; /** Parameter name for the human-readable placement */ public static final String BARCODE_HUMAN_READABLE_POS = "hrp"; /** Parameter name for the output format */ public static final String BARCODE_FORMAT = "fmt"; /** Parameter name for the image resolution (for bitmaps) */ public static final String BARCODE_IMAGE_RESOLUTION = "res"; /** Parameter name for the grayscale or b/w image (for bitmaps) */ public static final String BARCODE_IMAGE_GRAYSCALE = "gray"; /** Parameter name for the font size of the human readable display */ public static final String BARCODE_HUMAN_READABLE_SIZE = "hrsize"; /** Parameter name for the font name of the human readable display */ public static final String BARCODE_HUMAN_READABLE_FONT = "hrfont"; /** Parameter name for the pattern to format the human readable message */ public static final String BARCODE_HUMAN_READABLE_PATTERN = "hrpattern"; private Logger log = new ConsoleLogger(ConsoleLogger.LEVEL_INFO); /** * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest, HttpServletResponse) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { String format = determineFormat(request); int orientation = 0; Configuration cfg = buildCfg(request); String msg = request.getParameter(BARCODE_MSG); if (msg == null) msg = "6935840910605"; //6901687230107 BarcodeUtil util = BarcodeUtil.getInstance(); BarcodeGenerator gen = util.createBarcodeGenerator(cfg); ByteArrayOutputStream bout = new ByteArrayOutputStream(4096); try { if (format.equals(MimeTypes.MIME_SVG)) { //Create Barcode and render it to SVG SVGCanvasProvider svg = new SVGCanvasProvider(false, orientation); gen.generateBarcode(svg, msg); org.w3c.dom.DocumentFragment frag = svg.getDOMFragment(); //Serialize SVG barcode TransformerFactory factory = TransformerFactory.newInstance(); Transformer trans = factory.newTransformer(); Source src = new javax.xml.transform.dom.DOMSource(frag); Result res = new javax.xml.transform.stream.StreamResult(bout); trans.transform(src, res); } else if (format.equals(MimeTypes.MIME_EPS)) { EPSCanvasProvider eps = new EPSCanvasProvider(bout, orientation); gen.generateBarcode(eps, msg); eps.finish(); } else { String resText = request.getParameter(BARCODE_IMAGE_RESOLUTION); int resolution = 300; //dpi 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 = request.getParameter(BARCODE_IMAGE_GRAYSCALE); BitmapCanvasProvider bitmap = ("true".equalsIgnoreCase(gray) ? new BitmapCanvasProvider( bout, format, resolution, BufferedImage.TYPE_BYTE_GRAY, true, orientation) : new BitmapCanvasProvider( bout, format, resolution, BufferedImage.TYPE_BYTE_BINARY, false, orientation)); gen.generateBarcode(bitmap, msg); bitmap.finish(); } } finally { bout.close(); } response.setContentType(format); response.setContentLength(bout.size()); response.getOutputStream().write(bout.toByteArray()); response.getOutputStream().flush(); } catch (Exception e) { log.error("Error while generating barcode", e); throw new ServletException(e); } catch (Throwable t) { log.error("Error while generating barcode", t); throw new ServletException(t); } } /** * Check the request for the desired output format. * @param request the request to use * @return MIME type of the desired output format. */ protected String determineFormat(HttpServletRequest request) { String format = request.getParameter(BARCODE_FORMAT); format = MimeTypes.expandFormat(format); if (format == null) format = MimeTypes.MIME_JPEG; return format; } /** * Build an Avalon Configuration object from the request. * @param request the request to use * @return the newly built COnfiguration object * @todo Change to bean API */ protected Configuration buildCfg(HttpServletRequest request) { DefaultConfiguration cfg = new DefaultConfiguration("barcode"); //Get type String type = request.getParameter(BARCODE_TYPE); if (type == null) type = "code128"; DefaultConfiguration child = new DefaultConfiguration(type); cfg.addChild(child); //Get additional attributes DefaultConfiguration attr; String height = request.getParameter(BARCODE_HEIGHT); if (height != null) { attr = new DefaultConfiguration("height"); attr.setValue(height); child.addChild(attr); } String moduleWidth = request.getParameter(BARCODE_MODULE_WIDTH); if (moduleWidth != null) { attr = new DefaultConfiguration("module-width"); attr.setValue(moduleWidth); child.addChild(attr); } String wideFactor = request.getParameter(BARCODE_WIDE_FACTOR); if (wideFactor != null) { attr = new DefaultConfiguration("wide-factor"); attr.setValue(wideFactor); child.addChild(attr); } String quietZone = request.getParameter(BARCODE_QUIET_ZONE); if (quietZone != null) { attr = new DefaultConfiguration("quiet-zone"); if (quietZone.startsWith("disable")) { attr.setAttribute("enabled", "false"); } else { attr.setValue(quietZone); } child.addChild(attr); } // creating human readable configuration according to the new Barcode Element Mappings // where the human-readable has children for font name, font size, placement and // custom pattern. String humanReadablePosition = request.getParameter(BARCODE_HUMAN_READABLE_POS); String pattern = request.getParameter(BARCODE_HUMAN_READABLE_PATTERN); String humanReadableSize = request.getParameter(BARCODE_HUMAN_READABLE_SIZE); String humanReadableFont = request.getParameter(BARCODE_HUMAN_READABLE_FONT); if (!((humanReadablePosition == null) && (pattern == null) && (humanReadableSize == null) && (humanReadableFont == null))) { attr = new DefaultConfiguration("human-readable"); DefaultConfiguration subAttr; if (pattern != null) { subAttr = new DefaultConfiguration("pattern"); subAttr.setValue(pattern); attr.addChild(subAttr); } if (humanReadableSize != null) { subAttr = new DefaultConfiguration("font-size"); subAttr.setValue(humanReadableSize); attr.addChild(subAttr); } if (humanReadableFont != null) { subAttr = new DefaultConfiguration("font-name"); subAttr.setValue(humanReadableFont); attr.addChild(subAttr); } if (humanReadablePosition != null) { subAttr = new DefaultConfiguration("placement"); subAttr.setValue(humanReadablePosition); attr.addChild(subAttr); } child.addChild(attr); } return cfg; } }
web.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>barcodeServlet</servlet-name> <servlet-class>org.krysalis.barcode4j.servlet.BarcodeServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>barcodeServlet</servlet-name> <url-pattern>/barcodeServlet</url-pattern> </servlet-mapping> </web-app>
需要导入的包有:
avalon-framework-4.2.0.jar barcode4j.jar
当在地址栏中输入:http://localhost:8080/barCode后按下enter后
显示的结果为:
[img]
[/img]
发表评论
-
spring boot应用测试框架介绍
2018-07-19 14:44 751个人原创博客:spring boot应用测试框架介绍 -
可执行jar包的配置与运行
2017-06-04 19:42 1007spring boot项目可以以jar包的形式执行运行。s ... -
多线程并发
2016-05-21 23:49 0Splitter.on('|').trimResults(). ... -
jdk动态代理实现原理
2016-05-09 23:12 773jdk的动态代理即使用反射来实现,具体由Proxy、Invoc ... -
spring常见注解
2016-05-01 23:33 12321.Autowired 通过spring的依赖注入功能来 ... -
spring常见配置作用
2016-04-29 23:08 933一般应用中常见spring的 ... -
数据来自两个系统时的内存分页算法
2016-04-24 23:12 842业务数据来自a-app与b-app,其中a-app中数据的业务 ... -
linux下java web开发环境搭建
2016-04-10 14:09 1134一般的java web开发涉及到的开发工具有:jdk、tomc ... -
linux下md5sum和DigestUtils.md5Hex的关系
2015-12-19 22:30 8520本文对linux下md5sum命令和java中DigestUt ... -
基于jersey的web service
2015-11-22 22:55 1007本文是基于jersey的web service 的两个小例子, ... -
面试总结----spring
2015-05-19 22:17 911spring在面试中经常被 ... -
面试总结----多线程
2015-05-18 22:10 897面试过程中,多线程被问到的概率非常大,差不多都会问的。 下面 ... -
面试总结----java虚拟机
2015-05-17 23:20 741在面试过程中,java虚拟机被问到的概率非常大,应该是每场面试 ... -
面试总结----java集合
2015-05-17 11:57 676春节刚过,打算换一份工作,于是就开始了一段准备面试的生活,准备 ... -
json串与对象之间转换的几种实现方式
2015-01-24 18:56 1878这里使用了gson,fastjson,jackson,json ... -
google关于事件的生产者消费者模式实现例子
2015-01-24 11:28 976google使用生产者/消费者模式实现了事件的产生传播处理过程 ... -
图形化显示---冒泡排序
2014-12-05 22:17 917代码: package com.thread.singal ... -
多线程----wait/notify
2014-11-06 22:06 686线程同步:两个线程依次对同一变量进行操作。 packag ... -
多线程-----阻塞队列
2014-11-05 22:43 850使用一个线程将一个指定目录下面的所有文件放在一个阻塞队列中,用 ... -
迷宫的最短路径
2014-08-19 00:31 3763代码如下: package com.chapterO ...
相关推荐
Java生成条码并将其保存在PDF上的技术是一个实用的编程任务,常见于自动化文档处理、库存管理和产品标签等领域。在本示例中,我们将探讨如何使用Java实现这一功能,并涉及如何添加水印以增强PDF的安全性和标识性。 ...
- **jFreeChart**:虽然jFreeChart主要是一个图表库,但也可以用于生成条形码,特别是Code 128和Code 39。 2. **使用ZXing生成条码** 首先,你需要在项目中引入ZXing库。然后,你可以创建一个`BitMatrix`对象,将...
以下是关于使用Java生成条形码的三个主要方法的详细解释:barcode4j、jbarcode和google-zxing。 1. **barcode4j**: Barcode4J是一个开源的Java库,基于Apache 2.0许可,专门用于生成各种类型的条形码。它使用XML...
Java生成条形码的Demo是一种在Java环境中实现条形码生成的应用示例。条形码在各种领域,如零售、物流、库存管理等,都扮演着重要的角色,它能够快速准确地识别商品信息。本Demo旨在帮助开发者了解如何在Java项目中...
本文将详细介绍如何在Java环境中利用特定的库,如barcode4j,来创建和生成条形码。 首先,我们要理解什么是条形码。条形码是一种图形化数据表示方式,通过不同宽度和间隔的条纹组合来编码信息。它使得数据能够快速...
总的来说,Java结合ZXing库能够轻松地生成条形码和二维码,同时支持自定义各种参数以满足不同需求。在实际项目中,根据业务场景,我们还可以进一步扩展这些功能,如增加扫描和解码能力,或者与其他系统集成,实现更...
目前比较通用的用Java实现条形码打印的集成插件主要有:barbecue和barcode4j. 但是用barbecue,条形码可以显示出来,可是下面对应数字却不能显示出来,当然可以自己手动加上去,不过那就不是一张图片里面的了,并且...
在IT行业中,生成条码是一项常见的任务,尤其...综上所述,Java生成条码是一个相对简单的过程,关键在于选择合适的库和理解条码编码规范。通过合理的编程,可以轻松地将条码集成到各种Java应用中,提升业务流程的效率。
ZXing是一个开源项目,提供了多种条码和二维码的读写功能,而Barbecue则专注于生成条形码。在这里,我们假设你已经选择了适合的库,并正确地将其引入到你的Java项目中。 一、ZXing(Zebra Crossing)库的使用 1. ...
"Java生成条形码.rar"这个压缩包文件显然包含了用于在Java中生成条形码的相关工具和示例。主要知识点包括: 1. **条形码原理**:条形码是一种光学可读的数据表示形式,通过不同宽度的黑白条纹来代表数字或字母信息...
5. 如果需要,也可以生成条形码,同样使用Graphics2D绘制条形码的线条和间隔。 6. 添加其他信息,如收件人和寄件人的地址、时间戳等,可以使用Graphics2D的文本绘制功能。 7. 最后,将整个打印单的内容保存为一个...
以下是一些使用Java生成条形码的基本步骤: 1. **引入库**:首先,你需要将库添加到你的项目依赖中。如果是Maven项目,可以在pom.xml文件中添加对应的依赖。对于`BarcodeLib`这样的库,可能需要下载JAR文件并将其...
Java生成条形码code128 Java生成条形码code128是一种常用的条形码生成方式,通过使用Java语言和Barcode4J库,可以生成高质量的Code 128条形码。在实际应用中,Code 128条形码广泛应用于物流、生产流程控制、资产...
maven项目首先要导入pom资源 <groupId>net.sf.barcode4j</groupId> <artifactId>barcode4j-light <version>2.0 </dependency>