用Java的方式生成条形码可以有两种方式:

1.用servlet的方式来生成

2.纯Java的方式来生成

第一种:

代码如下:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String code = request.getParameter("code");try {  ByteArrayOutputStream outputStream = null;  BufferedImage bi = null;  int len = code.length();  JBarcode productBarcode = new JBarcode(Code128Encoder.getInstance(),  WidthCodedPainter.getInstance(),  EAN13TextPainter.getInstance());  // 尺寸,面积,大小 密集程度  productBarcode.setXDimension(Double.valueOf("0.5").doubleValue());  // 高度 10.0 = 1cm 默认1.5cm  productBarcode.setBarHeight(Double.valueOf("30").doubleValue());  // 宽度  productBarcode.setWideRatio(Double.valueOf(30).doubleValue());
//              是否显示字体  productBarcode.setShowText(true);
//             显示字体样式  //System.out.println(BaseLineTextPainter.getInstance().toString());productBarcode.setTextPainter(BaseLineTextPainter.getInstance());   // 生成二维码  bi = productBarcode.createBarcode(code);  ImageIO.write(bi, "jpg", response.getOutputStream());  } catch (Exception e) {  e.printStackTrace();  }  }

这种方式生成的条形码会出现在jsp页面里,你可以在浏览器中访问你的servlet就可已看见它

第二种将条形码生成到本地:

既然是生成到本地,那肯定要用到IO流

代码如下:

package cn.wge.test;import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import org.jbarcode.JBarcode;
import org.jbarcode.encode.Code128Encoder;
import org.jbarcode.paint.EAN13TextPainter;
import org.jbarcode.paint.WidthCodedPainter;
import org.jbarcode.util.ImageUtil;
public class Util { /*** 生成商品条形码** @param filePath*            商品条形码图片存放路径:../xxx/yyy/* @param jbarCode*            商品条形码:8位、13位* @param format*            商品条形码图片格式:.gif/.png/.jpg/.jpeg* @return 图片存放路径+图片名称+图片文件类型*/public String createBarCode(String filePath, String jbarCode, String format) {String barCodeName = jbarCode + format;try {BufferedImage bi = null;          JBarcode productBarcode = new JBarcode(Code128Encoder.getInstance(),WidthCodedPainter.getInstance(),EAN13TextPainter.getInstance());bi = productBarcode.createBarcode(jbarCode);saveToJPG(bi, filePath, barCodeName);// 尺寸,面积,大小 密集程度  productBarcode.setXDimension(Double.valueOf("0.5").doubleValue());  // 高度 10.0 = 1cm 默认1.5cm  productBarcode.setBarHeight(Double.valueOf("30").doubleValue());  // 宽度  productBarcode.setWideRatio(Double.valueOf(30).doubleValue());  //是否显示字体  productBarcode.setShowText(true);return filePath + barCodeName;} catch (Exception localException) {localException.printStackTrace();return null;}}/*** 生成JPEG图片** @param paramBufferedImage* @param paramString*/@SuppressWarnings("unused")private void saveToJPG(BufferedImage paramBufferedImage, String filePath,String fileName) {saveToFile(paramBufferedImage, filePath, fileName, "jpeg");}/*** 生成PNG图片** @param paramBufferedImage* @param paramString*/@SuppressWarnings("unused")private void saveToPNG(BufferedImage paramBufferedImage, String filePath,String fileName) {saveToFile(paramBufferedImage, filePath, fileName, "png");}/*** 保存图片文件** @param paramBufferedImage*            图片流* @param filePath*            文件路径* @param imgName*            图片参数* @param imgFormat*            图片格式*/private  void saveToFile(BufferedImage paramBufferedImage, String filePath,String imgName, String imgFormat) {try {System.out.println("saveToFile");FileOutputStream fileOutputStream = null;try {String dirPath = filePath;File dirFile = new File(dirPath);if (!dirFile.exists()) {dirFile.mkdirs();}String imgPath = dirPath + imgName;fileOutputStream = new FileOutputStream(imgPath);} catch (Exception e) {System.out.println("Create Img File Error:" + e.toString());}ImageUtil.encodeAndWrite(paramBufferedImage, imgFormat,fileOutputStream, 96, 96);fileOutputStream.close();} catch (Exception localException) {System.out.println("Save Img File Error:" + localException);localException.printStackTrace();}}
}

Java的方式生成条形码相关推荐

  1. JAVA使用barcode4j生成条形码和二维码图片以及带logo的二维码,验证码图片

    二维码 1.Maven引入barcode4j依赖 <!-- 条形码生成 --><dependency><groupId>net.sf.barcode4j</g ...

  2. java使用jbarcode生成条形码

    准备工作: 开发工具:eclipse4.5+jdk1.7 所需jar包:jbarcode-0.2.8.jar 案例: package jbarcode; import java.awt.Color; ...

  3. java 使用zxing生成条形码(可自定义文字位置、边框样式)

    最新工作中遇到生成条形码的需求,经过一番摸索之后找到了zxing这个工具类,实现效果如下: 首先引入依赖: <!-- 条形码生成器 --><dependency><gro ...

  4. java wsimport方式生成webservice客户端代码

    wsimport方法 1.在jdk安装目录bin下wsimport.exe,执行命令(修改红色部分的信息即可) wsimport -keep -d D:\1 -s D:\2 -p com.exampl ...

  5. java ca认证_java编程方式生成CA证书

    下面是java编程方式生成CA证书的代码,使用的是BC的provider.生成CA证书与生成普通证书的区别是:1,生成CA证书时,issuer和subject一致:2,在ContentSigner.b ...

  6. php 生成条形码(支持任意php框架)

    一:插件安装 在php中我们可以使用php-barcode-generator插件来生成条形码,php-barcode-generator插件github地址:GitHub - picqer/php- ...

  7. swagger2markup(maven方式及java代码方式)

    任务:通过同事的json文件生成相应的html和pdf文档 前言     开始时swagger2markup和asciidoctorj是什么都不知道,只能百度,看官方文档(翻译...), 遇到问题就一 ...

  8. Java生成条形码code128

    先看效果,生成的条形码. 用支付宝扫码功能可以识别出数字,手头没有扫码枪类似的工具没有测试其它方式.亲们有工具可以留言. maven依赖 <dependency><groupId&g ...

  9. java生成条形码-使用zxing

    背景 目前二维码的应用场景已经遍布各类互联网平台,通常是将产品/商品的唯一编号存储于二维码中以做扫码识别. 但是在很多场景下依然需要使用条形码(一维码) 解决方案 java生成条形码可使用的方式:ba ...

最新文章

  1. OCtaveResNet 测试
  2. 【题解】Luogu P2730 魔板
  3. iphone文件夹如何添加服务器,iPhone怎么在文件夹内新建文件夹 iPhone在文件夹内新建文件夹方法...
  4. Android Studio开发第二篇创建新项目
  5. mysql文档批处理去重_数据导入经验总结
  6. kali2.0安装搜狗输入法
  7. 树莓派基金会来号召用键盘生物学家研究企鹅
  8. 从平庸到杰出,技术人应该专注的底层知识是什么?
  9. Hadoop集群配置搭建
  10. 浅谈压缩感知(二十二):压缩感知重构算法之正则化正交匹配追踪(ROMP)
  11. ubuntu卸载vmware player
  12. 数学分析(3): 函数极限
  13. 手机文件上传ftp服务器,安卓手机文件上传 ftp服务器
  14. python中encode函数_Python中encode()方法有哪些功能?
  15. 使用终端模拟器通过wifi连接eclipse
  16. 【转】Linux服务器性能评估与优化(一)
  17. map.......
  18. PTA-圆形体积计算器
  19. nodejs+vue+elementui社区小区电动车充电桩管理系统Express
  20. 小程序判断用户是否关注了关联的公众号

热门文章

  1. 【转】无法启动microsoft outlook 无法打开 outlook窗口 无法打开此文件夹集合 发生了意外错误...
  2. JTR(John The Ripper)的ssh密钥破解记录
  3. 科研资料|论文数模真的好难?那是你还不会Matlab!
  4. 信奥一本通 1396:病毒(virus)
  5. Ubuntu 20.04 虚拟机安装教程
  6. iscsi技术,磁盘阵列技术介绍
  7. SonicWALL 防火墙配置步骤
  8. SonicWALL防火墙配置NAT Policy
  9. option 82是dhcp报文中的中继代理
  10. 用XDOC预览文档的理由:简单、实用、免费