项目中功能要求生成一个二维码来访问某地址,特封装一个二维码生成工具类

使用开源的一维/二维码图形处理库zxing

GayHub地址:https://github.com/zxing/zxing

1.引入依赖

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

2.封装工具类

我好想换点积分啊

package com.app.utils;import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;/*** @title 生成二维码工具类* @author zch* @discribtion* @Date 2020年1月3日 下午4:26:05* @vision V1.0*/
public class QRCodeUtil
{private static final int width = 200; // 图像宽度private static final int height = 200; // 图像高度private static final int ON_COLOR = 0xFF000001;private static final int OFF_COLOR = 0xFFFFFFFF;/*** @title 生成二维码图片* @discribtion* @author zch* @Date 2020年1月3日 下午3:27:21* @param width 二维码宽度,默认为200* @param height 二维码高度,默认为200* @param content 二维码内容,必填* @param logoPath logo图片路径,若为空则生成不带logo的二维码* @param imgPath 生成二维码文件夹路径* @param imgName 生成二维码图片名称,必填* @param suffix 生成二维码图片后缀类型,例如:gif,必填* @vision V1.0*/public static boolean generateQRImage(Integer width, Integer height, String content, String logoPath, String imgPath, String imgName, String suffix){if (content == null || imgName == null || suffix == null){return false;}try{width = width == null ? QRCodeUtil.width : width;height = height == null ? QRCodeUtil.height : height;if (logoPath != null && !"".equals(logoPath.trim())){QREncode(width, height, content, logoPath, imgPath, imgName, suffix);}else{QREncode(width, height, content, imgPath, imgName, suffix);}return true;}catch (Exception e){e.printStackTrace();return false;}}/*** @title 生成二维码* @discribtion* @author zch* @Date 2020年1月3日 下午3:27:21* @vision V1.0*/private static void QREncode(int width, int height, String content, String imgPath, String imgName, String suffix)throws Exception{File filePath = new File(imgPath);if (!filePath.exists()){filePath.mkdirs();}File imageFile = new File(imgPath, imgName);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, suffix, imageFile.toPath());// 输出原图片}/*** @title 生成带logo的二维码* @discribtion* @author zch* @Date 2020年1月3日 下午3:27:21* @vision V1.0*/private static void QREncode(int width, int height, String content, String logoPath, String imgPath, String imgName, String suffix)throws Exception{File filePath = new File(imgPath);if (!filePath.exists()){filePath.mkdirs();}File imageFile = new File(imgPath, imgName);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);MatrixToImageConfig matrixToImageConfig = new MatrixToImageConfig(ON_COLOR, OFF_COLOR);BufferedImage bufferedImage = LogoMatrix(MatrixToImageWriter.toBufferedImage(bitMatrix, matrixToImageConfig), new File(logoPath));ImageIO.write(bufferedImage, suffix, imageFile);// 输出带logo图片}/*** @title 二维码图片添加logo* @discribtion  * @author zch* @Date 2020年1月3日 下午3:27:21* @param matrixImage 源二维码图片* @param logoFile logo图片* @vision V1.0*/private static BufferedImage LogoMatrix(BufferedImage matrixImage, File logoFile)throws IOException{// 读取二维码图片,并构建绘图对象Graphics2D gs = matrixImage.createGraphics();int matrixWidth = matrixImage.getWidth();int matrixHeigh = matrixImage.getHeight();int ratioWidth = matrixWidth * 2 / 10;int ratioHeight = matrixHeigh * 2 / 10;// 读取Logo图片BufferedImage logo = ImageIO.read(logoFile);int logoWidth = logo.getWidth(null) > ratioWidth ? ratioWidth : logo.getWidth(null);int logoHeight = logo.getHeight(null) > ratioHeight ? ratioHeight : logo.getHeight(null);int x = (matrixWidth - logoWidth) / 2;int y = (matrixHeigh - logoHeight) / 2;// 绘制gs.drawImage(logo, x, y, logoWidth, logoHeight, null);gs.setColor(Color.BLACK);gs.setBackground(Color.WHITE);gs.dispose();matrixImage.flush();return matrixImage;}
}

3.测试生成二维码

QRCodeUtil.generateQRImage(null, null, "https://blog.csdn.net/qq_34928194", null, "E:/", "test.gif", "gif");

4.二维码

Java端生成二维码相关推荐

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

    Java 生成二维码 zxing生成二维码 条形码 服务端生成二维码 Java生成条形码 一.关于ZXing 1.ZXing是谷歌开源的支持二维码.条形码 等图形的生成类库:支持生成.和解码功能. G ...

  2. 在java中生成二维码,并直接输出到jsp页面

    在java中生成的二维码不存到磁盘里要直接输出到页面上,这就需要把生成的二维码直接以流的形式输出到页面上,我用的是myeclipse 和 tomcat 它的原理是:在加载页面时,根据img的src(c ...

  3. java实现生成二维码及扫码登录

    java实现生成二维码及扫码登录 1. 场景描述 2. 实现思路 3. 代码实现过程 3.1 pom.xml 3.2 二维码工具类 3.3 生成二维码并下载为图片 3.4 扫码登录 1. 场景描述   ...

  4. Java解析生成二维码-log

    Java解析生成二维码 1.pom.xml依赖 <!-- 引入二维码相关的依赖--><dependency><groupId>com.google.zxing< ...

  5. java springMVC生成二维码

    Zxing是Google提供的工具,提供了二维码的生成与解析的方法,现在使用Java利用Zxing生成二维码 1),二维码的生成 将Zxing-core.jar 包加入到classpath下. 我的下 ...

  6. java零碎要点---用java实现生成二维码,与解析代码实现

    创梦综合技术qq交流群:CreDream:251572072 二维码,是一种采用黑白相间的平面几何图形通过相应的编码算法来记录文字.图片.网址等信息的条码图片.如下图 二维码的特点: 1.  高密度编 ...

  7. java后台生成二维码以及页面显示二维码方式

    上篇文章已经说明并发布了后台生成二维码工具类,大家可以直接去看或者去拿. 地址:最简单实用的java生成二维码工具 现在呢说明页面上展示二维码的两种方式: 1.使用img标签的src来请求生成二维码, ...

  8. java实现生成二维码

    1.引入 maven 坐标 <!--Java 生成二维码 --><dependency><groupId>com.google.zxing</groupId& ...

  9. java如何生成二维码

    编写生成二维码的Java代码: public class CreateCode {public static void main(String[] args) {String data = " ...

最新文章

  1. 有关于诚信:唐骏学历门
  2. 服务器虚拟化集成项目投标方案,服务器虚拟化投标技术方案(纯方案,16页).doc...
  3. android android:process=,关于android:Android-appprocess的使用
  4. 从思维导图学习操作系统(二)
  5. asp.net core mvc接口,请求响应统一日志管理
  6. 【tensorflow】常量张量的初始化
  7. 用python画图代码-【Matplotlib】利用Python进行绘图
  8. 【22】Vue 之 Vue Devtools
  9. 对因果报应和轮回的态度
  10. Java 调用SAP PI PO 的Rest JSON接口
  11. Anaconda卸载与安装
  12. ue修改倚天服务器时间,正途私服UE修改一些配置脚本
  13. 人工智能来了 微智全景首款刷脸支付终端亮相
  14. 会议纪要模板----邮件
  15. lm283_飞利浦LED泛光灯具 BVP283 LED户外照明灯具350W 超高功率泛光灯
  16. 网站还在,赶紧拿起Python采集所有壁纸
  17. 鼠标拖拽mousemove和移动端touchmove问题
  18. 京东数科“四位一体”布阵新基建沙场
  19. 3月3 pytorch模型保存的.pt, .pth, .pkl的pytorch模型文件,只是后缀不同而已(仅此而已),打开方式
  20. 自主可控!搭载龙芯二号,飞凌嵌入式FET-2K0500-C核心板发布

热门文章

  1. 聚合广告SDK API
  2. MSP430单片机中断学习笔记(一)
  3. 联通宽带拨号开机自动连接
  4. uefi下的开机顺序_科普贴:BIOS和UEFI的启动项
  5. Android获取内置sdcard跟外置sdcard路径
  6. 如何用pycharm将.ui文件转换为.py文件(内含出错解决方法)
  7. sklearn.neighbors.KNeighborsClassifier()函数解析
  8. 计算机c盘系统自带的有哪些,电脑C盘里哪些文件是可以删除的?C盘可以删除的文件大全...
  9. 火车头dede采集接口,图片加水印,远程图片本地化,远程无后缀的无图片本地化...
  10. 企业微信如何通过红包活动引流?