Java生成名片式的二维码源码分享
世界上25%的人都有拖延症——但我觉得这统计肯定少了,至少我就是一名拖延症患者。一直想把“Java生成名片式(带有背景图片、用户网络头像、用户昵称)的二维码”这篇博客分享出来,但一直拖啊拖,拖到现在,真应了苏格兰的那句谚语——“什么时候都能做的事,往往什么时候都不会去做。”
零、效果图
- 左上角为微信头像。
- 沉默王二是文字昵称。
- 附带URL为http://blog.csdn.net/qing_gee的二维码
- 还有指定的背景图。
使用场景:
点公众号的微信菜单“我的二维码”,然后展示一张名片式的二维码给用户。
一、源码下载
可以通过GitHub直接下载https://github.com/qinggee/qrcode-utils.
二、源码介绍
你肯定在网络上见到过不少Java生成带有logo的二维码的源码,这些都是生成二维码的初级应用。相对来说,生成“名片式(带有背景图片、用户网络头像、用户名称的二维码图片)的二维码”可能更高级一点,但内在的原理其实是相似的——在一张指定的图片对象Graphics2D利用drawImage()方法绘制上层图像,利用drawString绘制文字。
2.1 使用接口
文件位置: /qrcode-utils/src/test/QrcodeUtilsTest.java
MatrixToBgImageConfig config = new MatrixToBgImageConfig();
// 网络头像地址 config.setHeadimgUrl("https://avatars2.githubusercontent.com/u/6011374?v=4&u=7672049c1213f7663b79583d727e95ee739010ec&s=400");
// 二维码地址,扫描二维码跳转的地址
config.setQrcode_url("http://blog.csdn.net/qing_gee");
// 二维码名片上的名字
config.setRealname("沉默王二");
// 通过QrcodeUtils.createQrcode()生成二维码的字节码
byte[] bytes = QrcodeUtils.createQrcode(config);
// 二维码生成路径
Path path = Files.createTempFile("qrcode_with_bg_", ".jpg");
// 写入到文件
Files.write(path, bytes);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
如果你从GitHub上下载到源码后,可直接通过eclipse把工程导入到你的工作库,运行/qrcode-utils/src/test/QrcodeUtilsTest.java 即可生成二维码。
2.2 目录文件介绍
- 核心类为QrcodeUtils.java(用来生成二维码)
- 名片式二维码的参数类MatrixToBgImageConfig.java
- 测试用例QrcodeUtilsTest.java
- res资源包下有两张图片,bg.jpg为指定的背景图、default_headimg.jpg为默认的头像图
- /qrcode-utils/lib为所需的jar包
2.3 QrcodeUtils.java
2.3.1 获取背景
注意以下代码中的第一行代码。
InputStream inputStream = Thread.currentThread().getContextClassLoader()
.getResourceAsStream(config.getBgFile());
File bgFile = Files.createTempFile("bg_", ".jpg").toFile();
FileUtils.copyInputStreamToFile(inputStream, bgFile);
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
2.3.2 获取微信头像
通过建立HttpGet请求来获取微信头像。
CloseableHttpClient httpclient = HttpClientBuilder.create().build();
HttpGet httpget = new HttpGet(config.getHeadimgUrl());
httpget.addHeader("Content-Type", "text/html;charset=UTF-8");
// 配置请求的超时设置
RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(500)
.setConnectTimeout(500).setSocketTimeout(500).build();
httpget.setConfig(requestConfig);
try (CloseableHttpResponse response = httpclient.execute(httpget);
InputStream headimgStream = handleResponse(response);) {
Header[] contentTypeHeader = response.getHeaders("Content-Type");
if (contentTypeHeader != null && contentTypeHeader.length > 0) {
if (contentTypeHeader[0].getValue().startsWith(ContentType.APPLICATION_JSON.getMimeType())) {
// application/json; encoding=utf-8 下载媒体文件出错
String responseContent = handleUTF8Response(response);
logger.warn("下载网络头像出错{}", responseContent);
}
}
headimgFile = createTmpFile(headimgStream, "headimg_" + UUID.randomUUID(), "jpg");
} catch (Exception e) {
logger.error(e.getMessage(), e);
throw new Exception("头像文件读取有误!", e);
} finally {
httpget.releaseConnection();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
通过createTmpFile方法将图像下载到本地。
public static File createTmpFile(InputStream inputStream, String name, String ext) throws IOException {
File tmpFile = File.createTempFile(name, '.' + ext);
tmpFile.deleteOnExit();
try (FileOutputStream fos = new FileOutputStream(tmpFile)) {
int read = 0;
byte[] bytes = new byte[1024 * 100];
while ((read = inputStream.read(bytes)) != -1) {
fos.write(bytes, 0, read);
}
fos.flush();
return tmpFile;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
2.3.3 在背景图上绘制二维码、头像、昵称
private static void increasingImage(BufferedImage image, String format, String imagePath, File bgFile,
MatrixToBgImageConfig config, File headimgFile) throws Exception {
try {
BufferedImage bg = ImageIO.read(bgFile);
Graphics2D g = bg.createGraphics();
// 二维码的高度和宽度如何定义
int width = config.getQrcode_height();
int height = config.getQrcode_height();
// logo起始位置,此目的是为logo居中显示
int x = config.getQrcode_x();
int y = config.getQrcode_y();
// 绘制图
g.drawImage(image, x, y, width, height, null);
BufferedImage headimg = ImageIO.read(headimgFile);
int headimg_width = config.getHeadimg_height();
int headimg_height = config.getHeadimg_height();
int headimg_x = config.getHeadimg_x();
int headimg_y = config.getHeadimg_y();
// 绘制头像
g.drawImage(headimg, headimg_x, headimg_y, headimg_width, headimg_height, null);
// 绘制文字
g.setColor(Color.GRAY);// 文字颜色
Font font = new Font("宋体", Font.BOLD, 28);
g.setFont(font);
g.drawString(config.getRealname(), config.getRealname_x(), config.getRealname_y());
g.dispose();
// 写入二维码到bg图片
ImageIO.write(bg, format, new File(imagePath));
} catch (Exception e) {
throw new Exception("二维码添加bg时发生异常!", e);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
好了,源码就先介绍到这喽。
http://blog.csdn.net/qing_gee/article/details/77341821
Java生成名片式的二维码源码分享的更多相关文章
- Java生成与解析二维码
1.下载支持二维码的jar包qrcode.jar和qrcode_swetake.jar, 其中qrcode_swetake.jar用于生成二维码,rcode.jar用于解析二维码,jar包下载地址(免 ...
- Java生成、解析二维码
今天遇到需求,使用Java生成二维码图片,网搜之后,大神们早就做过,个人总结一下. 目标:借助Google提供的ZXing Core工具包,使用Java语言实现二维码的生成和解析. 步骤如下: 1.m ...
- java生成和解析二维码
前言 现在,二维码的应用已经非常广泛,在线生成器也是诸多,随手生成. 所以就和大家分享一个小案例,用zxing来做一个的二维码生成器,当然这个例子是比较简单,若是写的不好请多多包涵. ZXING项目是 ...
- 使用Google提供的ZXing Core,Java生成、解析二维码
1.maven项目中,pom.xml中引入ZXing Core工具包: <!-- https://mvnrepository.com/artifact/com.google.zxing/core ...
- java 生成微信的二维码 工具类
package com.app.wii.util; import java.io.File;import java.io.FileInputStream;import java.io.FileOutp ...
- java 生成和解析二维码
public class QRCode { /** * 解析二维码(QRCode) * @param imgPath * @return */ public static String decoder ...
- Java生成带logo二维码
目前生成二维码的方式有很多种,本例采用谷歌的zxing,去白边,添加logo等处理均在代码中有注释 demo连接 https://github.com/littlechaser/qrcode.git
- QR二维码生成器源码(中间可插入小图片)
二维码终于火了,现在大街小巷大小商品广告上的二维码标签都随处可见,而且大都不是简单的纯二维码,而是中间有个性图标的二维码. 我之前做了一个使用google开源项目zxing实现二维码.一维码编码解码的 ...
- Java集合框架之二:LinkedList源码解析
版权声明:本文为博主原创文章,转载请注明出处,欢迎交流学习! LinkedList底层是通过双向循环链表来实现的,其结构如下图所示: 链表的组成元素我们称之为节点,节点由三部分组成:前一个节点的引用地 ...
随机推荐
- Mahout Bayes分类
Mahout Bayes分类器是按照<Tackling the Poor Assumptions of Naive Bayes Text Classiers>论文写出来了,具体查看论文 实 ...
- 学习Qt,Getting started
在界面的设计中,现在用的比较多的是Qt和WPF(C#),以前的MFC已出现衰老趋势.本人最近在学习Qt,觉得很有用,遂决定将学习历程记录下来,或许有感于后之来者,不亦乐哉. 一.Hello Qt #i ...
- BT雷人的程序语言
原文:http://cocre.com/?p=1142 酷壳 这个世界从来都不会缺少另类的东西,人类自然世界如此,计算机世界也一样.编程语言方面,看过本站<6个变态的C语言Hello Worl ...
- obj-c编程09:块的语法
在obj-c中,有一种和C截然不同的东西--块.块可以在外边定义,也可以在函数或方法内部定义,可以被赋值给一个变量,然后用该变量调用.默认情况下块对外部变量的访问只能读不能写,除非用__block显示 ...
- C#解析HTML利器-Html Agility Pack
今天刚开始做毕设....好吧,的确有点晚.我的毕设设计需要爬取豆瓣的电影推荐,于是就需要解析爬取下来的html,之前用Python玩过解析,但目前我使用的是C#,我觉得C#不比python差,有微软大 ...
- [C#网络应用编程]2、对线程的管理
在System.Threading命名空间下,有一个Thread类,用于对线程进行管理,如创建线程.启动线程.终止线程.合并线程.让线程休眠等 Thread类 (假设Thread firTh = ne ...
- 如何在不使用三大地图的KEY和相关组件的情况下,直接传参数到相关的H5地图
以高德地图为例: window.location.href='http://m.amap.com/navigation/index/daddr=104.188206%2C30.858513%2C'+' ...
- 用sql获取一段时间内的数据
我把我CSDN写的 搬来博客园了.. SELECT * FROM 表名 WHERE timestampdiff(MINUTE, SYSDATE(), send_time) <=60 AND ...
- Vue.js与Jquery的比较 谁与争锋 js风暴
普遍认为jQuery是适合web初学者的起步工具.许多人甚至在学习jQuery之前,他们已经学习了一些轻量JavaScript知识.为什么?部分是因为jQuery的流行,但主要是源于经验开发人员的一个 ...
- arcengine之版本管理
public void VersionManagement(IVersionedWorkspace versionedWorkspace) { //creating the new version o ...