什么是ZXing?

ZXing是一个开源的,用Java实现的多种格式的1D/2D条码图像处理库。

利用ZXing这个工具库来生成二维码,具体操作如下:

引入相关资源包

<dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.4.0</version>
</dependency>
<dependency><groupId>com.google.zxing</groupId><artifactId>javase</artifactId><version>3.4.0</version>
</dependency>

编写工具类

package org.tianshun.common.utils;import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.OutputStream;
import java.util.Hashtable;
import java.util.Random;/*** 生成二维码的工具类*/
public class QRCodeUtils {private static final String CHARSET = "utf-8";private static final String FORMAT_NAME = "JPG";// 二维码尺寸private static final int QRCODE_SIZE = 400;// LOGO宽度private static final int WIDTH = 60;// LOGO高度private static final int HEIGHT = 60;public static BufferedImage createImage(String content, String logoPath,boolean needCompress) throws Exception {Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);hints.put(EncodeHintType.CHARACTER_SET, CHARSET);hints.put(EncodeHintType.MARGIN, 1);BitMatrix bitMatrix = new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);int width = bitMatrix.getWidth();int height = bitMatrix.getHeight();BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);for (int x = 0; x < width; x++) {for (int y = 0; y < height; y++) {image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000: 0xFFFFFFFF);}}if (logoPath == null || "".equals(logoPath)) {return image;}// 插入图片QRCodeUtils.insertImage(image, logoPath, needCompress);return image;}/*** 插入LOGO** @param source       二维码图片* @param imgPath      LOGO图片地址* @param needCompress 是否压缩* @throws Exception*/private static void insertImage(BufferedImage source, String imgPath,boolean needCompress) throws Exception {File file = new File(imgPath);if (!file.exists()) {System.err.println("" + imgPath + "   该文件不存在!");return;}Image src = ImageIO.read(new File(imgPath));int width = src.getWidth(null);int height = src.getHeight(null);if (needCompress) { // 压缩LOGOif (width > WIDTH) {width = WIDTH;}if (height > HEIGHT) {height = HEIGHT;}Image image = src.getScaledInstance(width, height,Image.SCALE_SMOOTH);BufferedImage tag = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);Graphics g = tag.getGraphics();g.drawImage(image, 0, 0, null); // 绘制缩小后的图g.dispose();src = image;}// 插入LOGOGraphics2D graph = source.createGraphics();int x = (QRCODE_SIZE - width) / 2;int y = (QRCODE_SIZE - height) / 2;graph.drawImage(src, x, y, width, height, null);Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);graph.setStroke(new BasicStroke(3f));graph.draw(shape);graph.dispose();}/*** 生成二维码(内嵌LOGO)** @param content      内容* @param logoPath      LOGO地址* @param destPath     存放目录* @param needCompress 是否压缩LOGO* @throws Exception*/public static String encode(String content, String logoPath, String destPath,boolean needCompress) throws Exception {BufferedImage image = QRCodeUtils.createImage(content, logoPath,needCompress);mkdirs(destPath);String file = new Random().nextInt(99999999) + ".jpg";ImageIO.write(image, FORMAT_NAME, new File(destPath + "/" + file));return file;}/*** 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)** @param destPath 存放目录* @date 2013-12-11 上午10:16:36*/public static void mkdirs(String destPath) {File file = new File(destPath);//当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)if (!file.exists() && !file.isDirectory()) {file.mkdirs();}}/*** 生成二维码(内嵌LOGO)** @param content  内容* @param imgPath  LOGO地址* @param destPath 存储地址* @throws Exception*/public static void encode(String content, String imgPath, String destPath)throws Exception {QRCodeUtils.encode(content, imgPath, destPath, false);}/*** 生成二维码** @param content      内容* @param destPath     存储地址* @param needCompress 是否压缩LOGO* @throws Exception*/public static void encode(String content, String destPath,boolean needCompress) throws Exception {QRCodeUtils.encode(content, null, destPath, needCompress);}/*** 生成二维码** @param content  内容* @param destPath 存储地址* @throws Exception*/public static void encode(String content, String destPath) throws Exception {QRCodeUtils.encode(content, null, destPath, false);}/*** 生成二维码(内嵌LOGO)** @param content      内容* @param imgPath      LOGO地址* @param output       输出流* @param needCompress 是否压缩LOGO* @throws Exception*/public static void encode(String content, String imgPath,OutputStream output, boolean needCompress) throws Exception {BufferedImage image = QRCodeUtils.createImage(content, imgPath,needCompress);ImageIO.write(image, FORMAT_NAME, output);}/*** 生成二维码** @param content 内容* @param output  输出流* @throws Exception*/public static void encode(String content, OutputStream output)throws Exception {QRCodeUtils.encode(content, null, output, false);}/*** 解析二维码** @param file 二维码图片* @return* @throws Exception*/public static String decode(File file) throws Exception {BufferedImage image;image = ImageIO.read(file);if (image == null) {return null;}BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));Result result;Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();hints.put(DecodeHintType.CHARACTER_SET, CHARSET);result = new MultiFormatReader().decode(bitmap, hints);String resultStr = result.getText();return resultStr;}/*** 解析二维码** @param path 二维码图片地址* @return* @throws Exception*/public static String decode(String path) throws Exception {return QRCodeUtils.decode(new File(path));}public static void main(String[] args) throws Exception {String text ="www.baidu.com";String logoPath = "C:\\Users\\admin\\Desktop\\test\\test.jpg";String destPath = "D:\\";System.out.println(QRCodeUtils.encode(text, null, destPath, true));}
}

执行main方法生成二维码图片并保存到本地相应位置。

java利用zxing生成二维码相关推荐

  1. Java利用Zxing生成二维码及解析二维码内容

    前言 Java 操作二维码的开源项目很多,如 SwetakeQRCode.BarCode4j.Zxing 等等 本篇文章是介绍利用Zxing来生成二维码图片在web网页上展示,同时解析二维码图片. Z ...

  2. Spring Boot电商项目57:订单模块六:【前台:生成支付二维码】接口;(支付url的拼凑;利用zxing生成二维码;二维码图片的存储;真实地址与可访问地址的转换;)

    说明: (1)本篇博客主要内容是:开发[前台:生成支付二维码]接口: (2)本篇博客需要注意的点有: ● 支付url的拼凑: ● 利用zxing生成二维码: ● 二维码图片的存储:真实地址与可访问地址 ...

  3. java使用zxing生成二维码,可带logo和底部文字

    java使用zxing生成二维码,可带logo和底部文字 springboot中整合zxing生成二维码 一.导入依赖 <properties><zxing.version>3 ...

  4. java学习-zxing生成二维码矩阵的简单例子

    这个例子需要使用google的开源项目zxing的核心jar包 core-3.2.0.jar 可以百度搜索下载jar文件,也可使用maven添加依赖 <dependency><gro ...

  5. Android利用zxing生成二维码

    感谢大佬:https://blog.csdn.net/mountain_hua/article/details/80646089 **gayhub上的zxing可用于生成二维码,识别二维码 gayhu ...

  6. Android利用zxing生成二维码,识别二维码,中间填充图片超详细、超简易教程

    gayhub上的zxing可用于生成二维码,识别二维码 gayhub地址:https://github.com/zxing/zxing 此文只是简易教程,文末附有完整代码和demo下载地址,进入正题: ...

  7. java使用zxing生成二维码

    zxing是Google提供的免费开源且操作十分方便的一个针对二维码的项目 代码也比较简单,这里我就直接把注释写上,直接根据自己的需要改动就行了. maven依赖: <dependencies& ...

  8. 利用zxing生成二维码

    这是一个测试过得的工具类,拿来可以直接使用. pom.xml添加 <dependency><groupId>com.google.zxing</groupId>&l ...

  9. Java利用Qrcode生成二维码

    1.jar包下载http://ishare.iask.sina.com.cn/download/explain.php?fileid=35428376 package ewm; import java ...

最新文章

  1. oracle省市表,省市之一 创建全国省市Sql表
  2. CSS 7:网页布局(传统布局,flex布局,布局套路)
  3. 群英服务器网站,群英:域名、DNS及URL功能说明
  4. python基本使用-python基本用法笔记合集
  5. SLAM-ch2-cmake中使用库
  6. JAVASCRIPT:VOID(0)含义解析
  7. TechWeb:转载合作须知!
  8. 全新 Veeam Availability Suite 9.5 成为率先全面集成 Windows Server 2016和Hyper-V技术的 可用性解决方案之一...
  9. MySQL5.7 group by新特性报错1055的解决办法
  10. jetty源码阅读总结1
  11. 惠普怎样启动计算机上的无线功能,如何打开hp无线网卡?如何打开惠普笔记本电脑无线网卡...
  12. Java课程设计之你画我猜
  13. Unity InputField光标位置不对
  14. 马来西亚动画片《Ribbit》将在尼亚加拉电影节中首映
  15. LightOJ 1197 Help Hanzo
  16. 优秀课程案例|如何用scratch画折线统计图
  17. Maven 的下载安装教程
  18. 2015智能手机操作系统
  19. 51单片机的新手入门前所有疑问整理
  20. 微型计算机原理存储器寻址,微机原理存储器部分 微型计算机原理及应用教材.ppt...

热门文章

  1. RPA 项目经验分享
  2. vc6.0,vs2005下插入excel表格
  3. Go实战--golang中使用echo框架中JSONP(labstack/echo)
  4. 世界上最有名的十大思想实验
  5. python :脚本运行出现语法错误:IndentationError:unexpected indent(缩进问题)
  6. 蓝牙BQB认证原因及流程
  7. 在Ubuntu20.04运行VINS-Fusion
  8. python 压缩图片为指定大小
  9. 开发脚手架及封装自动化构建工作流
  10. ASP.NET Core : 八.图说管道,唐僧扫塔的故事