二维码在我们目前的生活工作中,随处可见,日常开发中难免会遇到需要生成二维码的场景,网上也有很多开源的平台可以使用,不过这里我们可以通过几个开源组件,自己来实现一下。

在动手之前我们先思考一下需要进行的操作,首先我们需要生成一个二维码,其次我们需要在这里二维码中间添加一个头像。

这里我们生成二维码使用工具 zxing,合成图片我们采用 thumbnailator,接下来我们实操一下吧。

生成二维码

首先我们先根据目标地址,生成一个二维码,这里我们使用的是组件 zxing,在 SpringBoot 的pom依赖中,我们加入下面的依赖。

<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>

然后编写工具类 QRCodeGenerator.java

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.HashMap;publicclass QRCodeGenerator {/*** 根据内容,大小生成二维码到指定路径** @param contents 跳转的链接* @param width    宽度* @param height   高度* @param filePath 路径* @throws WriterException* @throws IOException*/public static void generateQrWithImage(String contents, int width, int height, String filePath) throws WriterException, IOException {//构造二维码写码器MultiFormatWriter mutiWriter = new com.google.zxing.MultiFormatWriter();HashMap<EncodeHintType, Object> hint = new HashMap<>(16);hint.put(EncodeHintType.CHARACTER_SET, "UTF-8");hint.put(EncodeHintType.ERROR_CORRECTION, com.google.zxing.qrcode.decoder.ErrorCorrectionLevel.H);hint.put(EncodeHintType.MARGIN, 1);//生成二维码BitMatrix bitMatrix = mutiWriter.encode(contents, BarcodeFormat.QR_CODE, width, height, hint);Path path = FileSystems.getDefault().getPath(filePath);MatrixToImageWriter.writeToPath(bitMatrix, "jpeg", path);}
}

这个静态方法有四个参数,分别是:

  • 跳转的链接内容;
  • 二维码的宽度;
  • 二维码的高度;
  • 生成的二维码后的存放路径;

代码中还有几个常量,EncodeHintType.CHARACTER_SET:表示编码;EncodeHintType.ERROR_CORRECTION 表示二维码的容错率;EncodeHintType.MARGIN 表示二维码的边框。

解释一下什么是二维码的容错率,大家在日常生活或者工作中应该会发现,有些二维码轻轻一扫就扫成功了,有的二维码却很难扫成功,这背后就是二维码的容错率的原因(对,有时候并不是你的网络问题!)。

不同密度的二维码所包含的信息其编码的字符、容错率均不同。密度越低,编码的字符个数越少、容错率越低,二维码容错率表示二维码图标被遮挡多少后,仍可以被扫描出来的能力。目前,典型的二维码的容错率分为 7%、15%、25%、30% 四个等级,容错率越高,越容易被快速扫描。“但是,容错率越高,二维码里面的黑白格子也就越多。因此,对于目前主流手机,在绝大多数扫描场景下,仍普遍应用 7% 容错率的二维码就能满足需求。

感兴趣的小伙伴也可以自己尝试几个不同的容错率,看看扫码的难度有没有变化。

接下来,我们编写一个 main 方法来生成一个二维码

public static void main(String[] args) {try {QRCodeGenerator.generateQrWithImage("https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MzkzODE3OTI0Ng==&scene=124#wechat_redirect", 500, 500, "./QRCode.jpeg");} catch (Exception e) {}}

运行完上面的 main 方法,我们可以得到一个二维码,到这里我们第一步已经完成了,接下来就是给这个二维码加上我们的头像。

添加头像

添加头像我们需要准备一个头像的照片,阿粉这里就用阿粉的头像了,如果这里有现成大小的头像就直接拿来使用就行,如果没有也没有关系,我们可以自己裁剪,这里我们就需要用来图片处理工具 thumbnailator 了。

阿粉

先将头像处理成 100 x 100 的大小,然后再合成上去就行,代码如下:

public static void main(String[] args) {try {// 将大图片缩小到指定大小
//    ThumbnailsImageUtils.size("./阿粉.jpeg", 100, 100, 1, "./阿粉100.png");// 通过水印的形式,将头像加到生成的二维码上面ThumbnailsImageUtils.watermark("./QRCode.jpeg",500, 500, Positions.CENTER, "./阿粉100.png",1f, 1f, "./result.png");} catch (Exception e) {}}

这个就是最终生成的带头像的二维码啦,友情提示,扫码可以看到更多优质文章。

通过上面的代码可以看到,ThumbnailsImageUtils 是真的很多强大,一行代码就能搞定图片缩小和图片合成,更多关于 ThumbnailsImageUtils 工具类的完整代码如下,赶紧收藏起来。

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;/*** <br>* <b>功能:</b><br>* <b>作者:</b>@author ziyou<br>* <b>日期:</b>2018-05-25 16:17<br>* <b>详细说明:</b>使用google开源工具Thumbnailator实现图片的一系列处理<br>*/
publicclass ThumbnailsImageUtils {privatefinalstatic Logger logger = LoggerFactory.getLogger(ThumbnailsImageUtils.class);/*** 将原图根据指定大小生产新图** @param sourceFilePath 原始图片路径* @param width          指定图片宽度* @param height         指定图片高度* @param targetFilePath 目标图片路径* @return 目标图片路径* @throws IOException*/public static String thumb(String sourceFilePath, Integer width, Integer height, String targetFilePath) throws IOException {Thumbnails.of(sourceFilePath).forceSize(width, height).toFile(targetFilePath);return targetFilePath;}/*** 按照比例进行缩放** @param sourceFilePath 原始图片路径* @param scale          scale(比例)* @param targetFilePath 目标图片路径* @return 目标图片路径* @throws IOException*/public static String scale(String sourceFilePath, Double scale, String targetFilePath) throws IOException {Thumbnails.of(sourceFilePath).scale(scale).toFile(targetFilePath);return targetFilePath;}/*** 不按照比例,指定大小进行缩放** @param sourceFilePath 原始图片路径* @param width          指定图片宽度* @param height         指定图片高度* @param targetFilePath 目标图片路径*                       keepAspectRatio(false) 默认是按照比例缩放的* @return 目标图片路径* @throws IOException*/public static String size(String sourceFilePath, Integer width, Integer height, float quality, String targetFilePath) throws IOException {Thumbnails.of(sourceFilePath).size(width, height).outputQuality(quality).keepAspectRatio(false).toFile(targetFilePath);return targetFilePath;}/*** 指定大小和角度旋转** @param sourceFilePath 原始图片路径* @param width          指定图片宽度* @param height         指定图片高度* @param rotate         rotate(角度),正数:顺时针 负数:逆时针* @param targetFilePath 目标图片路径* @return 目标图片路径* @throws IOException*/public static String rotate(String sourceFilePath, Integer width, Integer height, Double rotate, String targetFilePath) throws IOException {Thumbnails.of(sourceFilePath).size(width, height).rotate(rotate).toFile(targetFilePath);return targetFilePath;}/*** 指定角度旋转** @param sourceFilePath 原始图片路径* @param rotate         rotate(角度),正数:顺时针 负数:逆时针* @param targetFilePath 目标图片路径* @return 目标图片路径* @throws IOException*/public static String rotate(String sourceFilePath, Double rotate, String targetFilePath) throws IOException {Thumbnails.of(sourceFilePath).rotate(rotate).toFile(targetFilePath);return targetFilePath;}/*** @param sourceFilePath 原始图片路径* @param width          指定图片宽度* @param height         指定图片高度* @param position       水印位置* @param waterFile      水印文件* @param opacity        水印透明度* @param quality        输出文件的质量* @param targetFilePath 目标图片路径* @return 目标图片路径* @throws IOException*/public static String watermark(String sourceFilePath, Integer width, Integer height, Positions position, String waterFile, float opacity, float quality, String targetFilePath) throws IOException {Thumbnails.of(sourceFilePath).size(width, height).watermark(position, ImageIO.read(new File(waterFile)), opacity).outputQuality(quality).toFile(targetFilePath);return targetFilePath;}public static BufferedImage watermarkList(BufferedImage buffImg, int length, File[] waterFileArray) throws IOException {int x = 0;int y = 0;if (buffImg == null) {// 获取底图buffImg = new BufferedImage(1200, 1200, BufferedImage.SCALE_SMOOTH);} else {x = (length % 30) * 40;y = (length / 30) * 40;}// 创建Graphics2D对象,用在底图对象上绘图Graphics2D g2d = buffImg.createGraphics();// 将图像填充为白色g2d.setColor(Color.WHITE);g2d.fillRect(x, y, 1200, 40 * (waterFileArray.length + length));for (int i = 0; i < waterFileArray.length; i++) {// 获取层图BufferedImage waterImg = ImageIO.read(waterFileArray[i]);// 获取层图的宽度int waterImgWidth = waterImg.getWidth();// 获取层图的高度int waterImgHeight = waterImg.getHeight();// 在图形和图像中实现混合和透明效果g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1));// 绘制Integer j = i / 30;Integer index = i - j * 30;g2d.drawImage(waterImg, waterImgWidth * index + 1, waterImgHeight * j, waterImgWidth, waterImgHeight, null);}// 释放图形上下文使用的系统资源g2d.dispose();return buffImg;}/*** @param sourceFilePath 原始图片路径* @param waterFile      水印文件* @param targetFilePath 目标图片路径*                       透明度默认值0.5f,质量默认0.8f* @return 目标图片路径* @throws IOException*/public static String watermark(String sourceFilePath, String waterFile, String targetFilePath) throws IOException {Image image = ImageIO.read(new File(waterFile));Integer width = image.getWidth(null);Integer height = image.getHeight(null);return watermark(sourceFilePath, width, height,Positions.BOTTOM_RIGHT, waterFile, 0.5f, 0.8f, targetFilePath);}/*** 将图片转化为指定大小和格式的图片** @param sourceFilePath* @param width* @param height* @param format* @param targetFilePath* @return* @throws IOException*/public static String changeFormat(String sourceFilePath, Integer width, Integer height, String format, String targetFilePath) throws IOException {Thumbnails.of(sourceFilePath).size(width, height).outputQuality(0.8f).outputFormat(format).toFile(targetFilePath);return targetFilePath;}/*** 根据原大小转化指定格式** @param sourceFilePath* @param format* @param targetFilePath* @return* @throws IOException*/public static String changeFormat(String sourceFilePath, String format, String targetFilePath) throws IOException {Image image = ImageIO.read(new File(sourceFilePath));Integer width = image.getWidth(null);Integer height = image.getHeight(null);Thumbnails.of(sourceFilePath).size(width, height).outputFormat(format).toFile(targetFilePath);return targetFilePath;}/*** 输出到输出流** @param sourceFilePath* @param targetFilePath* @return* @throws IOException*/public static String toOutputStream(String sourceFilePath, String targetFilePath) throws IOException {OutputStream os = new FileOutputStream(targetFilePath);Thumbnails.of(sourceFilePath).toOutputStream(os);return targetFilePath;}/*** 输出到BufferedImage** @param sourceFilePath* @param format* @param targetFilePath* @return* @throws IOException*/public static String asBufferedImage(String sourceFilePath, String format, String targetFilePath) throws IOException {/*** asBufferedImage() 返回BufferedImage*/BufferedImage thumbnail = Thumbnails.of(sourceFilePath).size(1280, 1024).asBufferedImage();ImageIO.write(thumbnail, format, new File(targetFilePath));return targetFilePath;}
}

总结

今天阿粉通过两个组件带大家实践了一个二维码的生成和图片的合成,这两个技巧在工作中难免会使用到,赶紧保存使用起来吧。

Java 代码基于开源组件生成带头像的二维码,推荐收藏相关推荐

  1. Java 代码基于开源组件生成带头像的二维码

    二维码在我们目前的生活工作中,随处可见,日常开发中难免会遇到需要生成二维码的场景,网上也有很多开源的平台可以使用,不过这里我们可以通过几个开源组件,自己来实现一下. 在动手之前我们先思考一下需要进行的 ...

  2. asp生成带参数的二维码并合成推广海报图片,asp合并合成推广海报图片asp代码

    最近做的一个项目中,客户要求用asp生成二维码,然后合并到一张背景图片上,合并生成一张推广海报来,可把我愁坏了,经过一个晚上的努力,成功了,下面把这个:asp生成带参数的二维码并合成推广海报,asp合 ...

  3. asp生成带参数的二维码并合成推广海报图片,asp合并合成推广海报图片asp代码...

    最近做的一个项目中,客户要求用asp生成二维码,然后合并到一张背景图片上,合并生成一张推广海报来,可把我愁坏了,经过一个晚上的努力,成功了,下面把这个:asp生成带参数的二维码并合成推广海报,asp合 ...

  4. jQuery生成带Logo的二维码

    用zxing生成二维码并解析:https://blog.csdn.net/qq_41879385/article/details/81320723 用QR Code生成和解析二维码文章地址:https ...

  5. zxing生成带logo的二维码

    倒Zxing依赖 implementation 'cn.bingoogolapple:bga-qrcode-zxing:1.2.1' 代码段 import android.graphics.Bitma ...

  6. 使用zxing生成带logo的二维码图片,自动调节logo图片相对二维码图片的大小

    使用zxing生成带logo的二维码图片,自动调节logo图片相对二维码图片的大小  * 可选是否带logo,可选是否保存二维码图片:结果返回base64编码的图片数据字符串  * 页面显示:< ...

  7. vue 生成带logo的二维码 qrcode-vue 支持下载图片 实例详解

    最近的项目上有个需求,生成带logo的二维码,网上大部分都是基于JQ插件jQuery.qrcode.对于vue项目并不适用,翻阅了大量资料后,我找到了qrcode-vue这款基于vue的生成二维码的插 ...

  8. Golang使用qrcode生成二维码,以及生成带logo的二维码

    添加并引用依赖 go get github.com/boombuler/barcodeimport ("github.com/skip2/go-qrcode" ) 1.生成字节形式 ...

  9. 微信公众号怎么生成带参数的二维码?

    每位运营过微信公众平台的野生小编都知道,微信提供给开发者的文档是很高深的,最近为了弄个带参数的二维码,同样技术小白的小编也死了不少脑细胞了,然而我终于知道官方生成带参数的二维码的方法了. 1.注意事项 ...

最新文章

  1. 最小二乘法的本质是什么?
  2. srm linux字符界面,如何使用srm安全的删除Linux中的文件
  3. TCP/UDP,SOCKET,HTTP,FTP协议简析
  4. C++实践参考——OOP版电子词典
  5. 湖南省普通招生2021高考成绩查询,湖南省2021八省联考成绩可查,附查询入口及往年分数线...
  6. 在Windows环境下用Editplus打造一个Python编辑调试环境
  7. freebsd 安装教程
  8. 数字电路技术可能出现的简答题_2013年9月份考试数字电子技术第二次作业
  9. [文摘20080908]哈佛大学成功25句
  10. 【算法/C语言】01背包问题(动态规划DP)
  11. KNN算法和kd树详解(例子+图示)
  12. Ds1302 时钟芯片的使用
  13. [精选转载]15天!我申论从60分到81.5分的复习经验
  14. ARM汇编 BIC和ORR指令
  15. 程序性天空盒,unity实现
  16. Linux 多线程 Pthread 互斥量
  17. 好佳居软装十大品牌 软装可以提升格调
  18. 深度解析Java游戏服务器开发
  19. 搜狗壁纸停运了,但我还是想推荐它……(附个人的一点想法)
  20. 特征选择-过滤式选择

热门文章

  1. 项目管理的前景如何?
  2. 【Rhapsody学习笔记(一)】OrionHealth-Rhapsody的组成及常用过滤器
  3. 单片机反相器_基于AT89S52单片机的新型智能家居安防系统
  4. 【男生女生表白攻略】手把手教你制作有创意的表白软件
  5. (烧脑)奇怪数 - C语言 - 回溯
  6. BZOJ_P1529 [POI2005]ska Piggy banks(并查集)
  7. 爱看影院影视网站模版去授权
  8. C语言程序设计(一)计算机思维导论
  9. 转载: 华为内部Web安全测试原则
  10. 计算机高级办公应用是什么,计算机高级办公软件和计算机第二级考试有什么区别?...