Java 生成二维码 zxing生成二维码 条形码 服务端生成二维码 Java生成条形码

一、关于ZXing

1、ZXing是谷歌开源的支持二维码、条形码 等图形的生成类库;支持生成、和解码功能。 Github主页 、 API文档 、 介绍文档 。

二、依赖 pom.xml

1、javase.jar

 <!-- https://mvnrepository.com/artifact/com.google.zxing/javase --><dependency><groupId>com.google.zxing</groupId><artifactId>javase</artifactId><version>3.3.0</version></dependency>

三、生成二维码、条形码 和解码

1、生成二维码

     /*** description: 生成二维码* @param content  二维码内容* @param width 宽* @param height 高* @param targetPath 生成二维码位置* @param imgType 图片类型-- jpg/png/gif 等* @throws Exception* @return void* @version v1.0* @author w* @date 2020年8月27日 上午10:41:37*/public static void genQRCode(String content ,String targetPath ,String imgType , int width ,int height)throws Exception{Map<EncodeHintType, Object> hints = new HashMap<>();//内容编码格式hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");// 指定纠错等级hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);//设置二维码边的空度,非负数hints.put(EncodeHintType.MARGIN, 1);BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);MatrixToImageWriter.writeToPath(bitMatrix, imgType, new File(targetPath).toPath());// 输出原图片}

2、解析二维码

     /*** description: 解析二维码* @param imgPath* @return* @throws Exception* @return String* @version v1.0* @author w* @date 2020年8月27日 上午11:22:42*/@SuppressWarnings("unchecked")public static String readQRCode(String imgPath) throws Exception {File file = new File(imgPath);if(!file.exists() && !file.isFile()) {throw new RuntimeException("file not found ,"+ imgPath);}MultiFormatReader formatReader = new MultiFormatReader();//读取指定的二维码文件BufferedImage bufferedImage =ImageIO.read(file);BinaryBitmap binaryBitmap= new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(bufferedImage)));//定义二维码参数Map  hints= new HashMap<>();hints.put(EncodeHintType.CHARACTER_SET, "utf-8");Result result = formatReader.decode(binaryBitmap , hints);return result.getText();} 

3、生成条形码

    /*** description: 生成条形码* @param contents  条形码内容* @param width  条形码宽* @param height  条形码高* @param imgPath 图片存储路径* @return void* @version v1.0* @author w* @date 2020年8月27日 上午11:30:12*/public static void genBarCode(String contents, int width, int height, String imgPath) {int codeWidth = 3 + // start guard(7 * 6) + // left bars5 + // middle guard(7 * 6) + // right bars3; // end guardcodeWidth = Math.max(codeWidth, width);try {BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,BarcodeFormat.EAN_13, codeWidth, height, null);MatrixToImageWriter.writeToFile(bitMatrix, "png", new File(imgPath));} catch (Exception e) {e.printStackTrace();}}

4、解析条形码

    /*** description: 解析条形码* @param imgPath* @return String* @version v1.0* @author w* @date 2020年8月27日 上午11:30:27*/public static String decode(String imgPath) {BufferedImage image = null;Result result = null;try {image = ImageIO.read(new File(imgPath));if (image == null) {System.out.println("the decode image may be not exit.");}LuminanceSource source = new BufferedImageLuminanceSource(image);BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));result = new MultiFormatReader().decode(bitmap, null);return result.getText();} catch (Exception e) {e.printStackTrace();}return null;}

四、web页面返回二维码

1、二维码转换为 Buffer

    /*** description: 生成二维码的 Buffer 二进制文件* @param content* @param imgType* @param width* @param height* @throws Exception* @return BufferedImage* @version v1.0* @author w* @date 2020年9月7日 下午5:33:03*/public static BufferedImage genQRCodeToBuffer(String content ,String imgType , int width ,int height)throws Exception{Map<EncodeHintType, Object> hints = new HashMap<>();//内容编码格式hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");// 指定纠错等级hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);//设置二维码边的空度,非负数hints.put(EncodeHintType.MARGIN, 1);BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix);return bufferedImage;}

2、SpringMVC 设置返回 图片

  @RequestMapping(value = {"/gen"})public void gen(String name ,HttpServletRequest request , HttpServletResponse response) throws Exception {System.out.println(" name :" + name);if(null == name || name == "") {response.setContentType("application/json;charset=UTF-8");response.setCharacterEncoding("UTF-8");response.getWriter().print(" name can't be blank ,名字不能为空");return; }response.setHeader("Pragma", "no-cache");response.setHeader("Cache-Control", "no-cache");response.setDateHeader("Expires", 0);response.setContentType("image/jpeg");BufferedImage genQRCodeToBuffer = ZXingUtils.genQRCodeToBuffer(name);ImageIO.write(genQRCodeToBuffer , "jpeg" ,response.getOutputStream());}

五、总结

1、本示例是简单记录了使用 zxing生成二维码、条形码编码和解码的过程;若有更高的需求,如:批量生成、解析等,二维码带Logo 等,可根据本示例代码进行修改定制,也可以私信哈 ^_^ !

前端生成二维码: 使用jquery qrcode生成二维码图片分享到朋友圈打印二维码_HaHa_Sir的博客-CSDN博客

Java 生成二维码 zxing生成二维码 条形码 服务端生成二维码 Java生成条形码相关推荐

  1. TCP/IP网络编程之多进程服务端(二)

    TCP/IP网络编程之多进程服务端(二) 信号处理 本章接上一章TCP/IP网络编程之多进程服务端(一),在上一章中,我们介绍了进程的创建和销毁,以及如何销毁僵尸进程.前面我们讲过,waitpid是非 ...

  2. 服务端Skynet(二)——消息调度机制

    服务端Skynet(二)--消息调度机制 文章目录 服务端Skynet(二)--消息调度机制 1.提前了解知识 1.1.互斥锁(mutex lock : **mut**ual **ex**clusio ...

  3. java计算机毕业设计融呗智慧金融微资讯移动平台服务端源码+系统+数据库+lw文档+mybatis+运行部署

    java计算机毕业设计融呗智慧金融微资讯移动平台服务端源码+系统+数据库+lw文档+mybatis+运行部署 java计算机毕业设计融呗智慧金融微资讯移动平台服务端源码+系统+数据库+lw文档+myb ...

  4. 基于JAVA融呗智慧金融微资讯移动平台服务端计算机毕业设计源码+数据库+lw文档+系统+部署

    基于JAVA融呗智慧金融微资讯移动平台服务端计算机毕业设计源码+数据库+lw文档+系统+部署 基于JAVA融呗智慧金融微资讯移动平台服务端计算机毕业设计源码+数据库+lw文档+系统+部署 本源码技术栈 ...

  5. java计算机毕业设计Vue.js网上书城管理系统设计与实现服务端MyBatis+系统+LW文档+源码+调试部署

    java计算机毕业设计Vue.js网上书城管理系统设计与实现服务端MyBatis+系统+LW文档+源码+调试部署 java计算机毕业设计Vue.js网上书城管理系统设计与实现服务端MyBatis+系统 ...

  6. 基于JAVA社区养老综合服务平台服务端计算机毕业设计源码+数据库+lw文档+系统+部署

    基于JAVA社区养老综合服务平台服务端计算机毕业设计源码+数据库+lw文档+系统+部署 基于JAVA社区养老综合服务平台服务端计算机毕业设计源码+数据库+lw文档+系统+部署 本源码技术栈: 项目架构 ...

  7. 送餐app+php,订餐APP源码Food Delivery App v2.1(客户端+服务端)

    android订餐APP源码Food Delivery App v2.1(客户端+服务端),包含android客户端源码.php+mysql服务端源码. Version 2.1 Food Delive ...

  8. 网维大师系统虚拟盘添加副服务器,网维大师系统虚拟盘组件【副】服务端安装.doc...

    网维大师系统虚拟盘组件[副]服务端安装 网吧爱好者搜集 整理 http://www.pc0359.cn Ⅰ.在网维大师官方网站http://www.icafe8.com下载中心里,下载网维大师系统虚拟 ...

  9. 基于ET框架致敬LOL的Moba游戏源码,包含完整的客户端与服务端交互

    运行环境 编辑器:Unity 2020.3.12 LTS 客户端:.Net Framework 4.7.2 IDE:JetBrain Rider 2020 服务端:.Net Core 3.1 已实现功 ...

最新文章

  1. 1.mysql数据库安装不成功的解决方案
  2. 如何识别数据中心的能源浪费?
  3. 【C语言简单说】四:常量
  4. python求最小公倍数_python求最大公约数和最小公倍数的简单方法
  5. 计算机组网配置,计算机组网技术与配置教案..doc
  6. 【Python-3.3】字典中存储列表
  7. 摘抄《MacTalk 人生元编程》
  8. windows命令_Windows自带强大的入侵检测工具——Netstat 命令 查询是否中木马
  9. linux备份根目录与还原脚本,Shell脚本备份和还原MBR(主引导记录)
  10. Html实现Excel模板下载
  11. 周志华:关于机器学习的一点思考
  12. 矛与盾:黑客攻防命令大曝光
  13. 如何熟练使用EXCEL
  14. Ubuntu20.04--开机自动运行脚本(命令)--方法/实例
  15. 魅族18Max什么时候发布?
  16. ff14 掉线 服务器维护,《FF14》29日更新维护 暂不推出手工补丁
  17. pythonturtle画图库使用技巧_Python画图库turtle使用方法简介
  18. selenium 与浏览器 以及浏览器驱动版本问题
  19. 招聘面试技巧(转载)
  20. floor()函数的使用

热门文章

  1. Wordpress模板主题中functions.php常用功能代码与常用插件[ 后台篇](持续收集整理)
  2. 获取Excel种文字的拼音首字母函数
  3. 解决md导入CSDN中图片大小过大 改变图片的大小
  4. 高等数学阶段复习, 函数极限, 连续, 导数,微分
  5. QT5 OpenGL (四, 绘制立体图形)
  6. 2022安全员-A证考试题模拟考试题库模拟考试平台操作
  7. 影响MRP计算的因素———提前期
  8. 没有水印的夸克免费扫描
  9. 浏览器翻译插件 沙拉查词;图片翻译;pdf 阅读器软件、pdf翻译工具
  10. adguard自定义_openwrt上装adguard以及实用教程