java 通过Qrcode生成二维码添加图片logo和文字描述
/**
* 二维码创建
* @author yhzm
*
*/
public class printServiceImpl extends BaseService {
public void barCodeGenera() {
init(false);
//先创建一个二维码
String text = strRequiredParam("barcode","二维码信息");
String desc = strRequiredParam("desc","文字内容");//二维码下面的文字描述
String logoPath = "d:\\aa.png";//二维码的logo地址
int logoWidth = 40; //logo的宽
int logoHeight = 40; //logo的高
try{
Qrcode qrcode = new Qrcode();
qrcode.setQrcodeErrorCorrect('M');//设置纠错等级(分为:L、M、H三个等级)
qrcode.setQrcodeEncodeMode('B');//N代表数字、A代表a-Z、B代表其他字符
qrcode.setQrcodeVersion(7);//设置版本 int width = 67+12*(7-1);//设置二维码的宽 二维码像素的大小和版本号有关 但是版本号越大 二维码也越是复杂 这个需要注意
int height = 67+12*(7-1);//设置二维码的高
//将内容变为特定UTF-8格式编码的字节码
byte [] qrData = text.getBytes("UTF-8"); BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
//创造画笔
Graphics2D gs = bufferedImage.createGraphics();
gs.setBackground(Color.WHITE);//设置背景
gs.setColor(Color.BLACK);//设置画笔颜色
gs.clearRect(0, 0, width, height);//清除画板内容
//设置偏移量
int pixoff = 2;
boolean [][] d = qrcode.calQrcode(qrData);
for(int y=0;y<d.length;y++) {
for(int x=0;x<d.length;x++) {
if(d[x][y]) {
gs.fillRect(x*3+pixoff, y*3+pixoff, 3, 3);
}
}
}
gs.dispose();
BufferedImage bm = bufferedImage;//二维码
File logoFile = new File(logoPath); //logo图片
BufferedImage logoImg = ImageIO.read(logoFile);
/* float ratio = 0.5; //倒圆角
if(ratio>0){
logoWidth = logoWidth>width*ratio ? (int)(width*ratio) : logoWidth;
logoHeight = logoHeight>height*ratio ? (int)(height*ratio) : logoHeight;
} */
int x = (width-logoWidth)/2;
int y = (height-logoHeight)/2;
Graphics g = bm.getGraphics();
g.drawImage(logoImg, x, y, logoWidth, logoHeight, null);
int whiteWidth = 0; //白边
Font font = new Font("黑体", Font.BOLD, 12);
int fontHeight = g.getFontMetrics(font).getHeight();//得到字体的高度
//计算需要多少行
int lineNum = 1;
int currentLineLen = 0;
for(int i=0;i<desc.length();i++){
char c = desc.charAt(i);
int charWidth = g.getFontMetrics(font).charWidth(c);
//循环文字得到文字区域的行数
if(currentLineLen+charWidth>width){
lineNum++;
currentLineLen = 0;
continue;
}
currentLineLen += charWidth;
}
int totalFontHeight = fontHeight*lineNum; //得到文字区域的高度
int wordTopMargin = 4;
BufferedImage bm1 = new BufferedImage(width, height+totalFontHeight+wordTopMargin-whiteWidth, BufferedImage.TYPE_INT_RGB); //创建将文字高度计算到其中的图片
Graphics g1 = bm1.getGraphics();
g1.setColor(Color.WHITE);
g1.fillRect(0, height, width, totalFontHeight+wordTopMargin-whiteWidth); //将文字部分的背景填充成白色
g1.setColor(Color.black);
g1.setFont(font);
g1.drawImage(bm, 0, 0, null); //将创建好的二维码从起点(0,0)开始画在图中
int currentLineIndex = 0;
//判断是否只有一行,只有一行就居中显示
currentLineLen = lineNum-1==currentLineIndex?(width-g.getFontMetrics(font).stringWidth(desc))/2:0;
int baseLo = g1.getFontMetrics().getAscent();
for(int i=0;i<desc.length();i++){
String c = desc.substring(i, i+1);
int charWidth = g.getFontMetrics(font).stringWidth(c);
//判断是否需要换行
if(currentLineLen+charWidth>width){
currentLineIndex++;
//判断是否是最后一行 最后一行居中显示
currentLineLen = lineNum-1==currentLineIndex?(width-g.getFontMetrics(font).stringWidth(desc.substring(i)))/2:0;
g1.drawString(c, currentLineLen + whiteWidth, height+baseLo+fontHeight*(currentLineIndex)+wordTopMargin);//将单个文字画到对应位置
currentLineLen = charWidth;
continue;
}
g1.drawString(c, currentLineLen+whiteWidth, height+baseLo+fontHeight*(currentLineIndex) + wordTopMargin);
currentLineLen += charWidth;
}
g1.dispose();
bm = bm1;
response.setContentType("image/jpeg");
//好了 ,现在通过IO流来传送数据
ImageIO.write(bm , "JPEG", response.getOutputStream());
}catch(Throwable e){
e.printStackTrace();
}
}
}
这个可以用来生成二维码,并且携带log和文字。
java 通过Qrcode生成二维码添加图片logo和文字描述的更多相关文章
- JAVA使用qrcode生成二维码(带logo/不带logo)
/** * */ package qrcode; import java.awt.Color; import java.awt.Graphics2D; import java.awt.Image; i ...
- JAVA用QRCode生成二维码
QRCode jar下载地址: 生成:http://www.swetake.com/qrcode/index-e.html 读取:https://zh.osdn.net/projects/qrcode ...
- Java与JS生成二维码
1.二维码概念 二维码/二维条码是用某种特定的集合图形按一定规律在平面上(二维方向上)分布的黑白相间的图形记录数据符号信息的图片. 黑线是二进制的1,空白的地方是二进制的0,通过1.0这种数据组合用于 ...
- java学习-zxing生成二维码矩阵的简单例子
这个例子需要使用google的开源项目zxing的核心jar包 core-3.2.0.jar 可以百度搜索下载jar文件,也可使用maven添加依赖 <dependency> <gr ...
- JAVA根据URL生成二维码图片、根据路径生成二维码图片
引入jar包 zxing-2.3.0.jar.IKAnalyzer2012_u6.jar 下载地址:https://yvioo.lanzous.com/b00nlbp6h ...
- java生成二维码(带logo)
之前写过一篇不带logo的二维码实现方式,採用QRCode和ZXing两种方式 http://blog.csdn.net/xiaokui_wingfly/article/details/3947618 ...
- Java根据链接生成二维码
Java根据链接生成二维码 相关 jar 包: core-3.1.0.jar 源码及 jar 包下载:http://files.cnblogs.com/files/liaolongjun/qrcode ...
- Java后台直接生成二维码介绍
Java后台直接生成二维码 1.其实jquery也可以直接生成二维码的,但我测试的时候,二维码生成后太模糊,难以识别.所以在这里介绍在后来生成二维码的方式. 2.不善于文字描述,直接上代码了. imp ...
- C# 利用QRCode生成二维码图片
网上生成二维码的组件是真多,可是真正好用的,并且生成速度很快的没几个,QRCode就是我在众多中找到的,它的生成速度快.但是网上关于它的使用说明,真的太少了,大都是千篇一律的复制粘贴.这是本要用它做了 ...
随机推荐
- Nginx模块及配置虚拟主机
1.Nginx的2组主要的模块 (1)core modules (必需,核心模块) 包括:Main.Events (2)Standard HTTP modules(虽然不是必需,但是缺省都会安装,不建 ...
- ASP.NET Core Web 应用程序系列(五)- 在ASP.NET Core中使用AutoMapper进行实体映射
本章主要简单介绍下在ASP.NET Core中如何使用AutoMapper进行实体映射.在正式进入主题之前我们来看下几个概念: 1.数据库持久化对象PO(Persistent Object):顾名思义 ...
- Git实战指南----跟着haibiscuit学Git(第八篇)
笔名: haibiscuit 博客园: https://www.cnblogs.com/haibiscuit/ Git地址: https://github.com/haibiscuit?tab=re ...
- dpwwn: 1 Vulnhub Walkthrough
主机层面扫描: ╰─ nmap -p1-65535 -sV -A 10.10.202.130 22/tcp open ssh OpenSSH 7.4 (protocol 2.0) 80/ ...
- Android WebView与H5联调技巧
版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/78 背景: 突然想写一篇关于Android WebView ...
- 【好书推荐】《剑指Offer》之硬技能(编程题12~16)
本文例子完整源码地址:https://github.com/yu-linfeng/BlogRepositories/tree/master/repositories/sword <[好书推荐]& ...
- application context not configured for this file于spring框架使用中的原因
spring配置文件中时常会出现这个提示,翻译过来大概意思就是没有配置该文件到项目中 于是进入到Project Structure中查看 可以很明显的看到下面有个感叹号,大概意思是下面的文件没有匹配 ...
- nginx 修改文件上传大小限制
修改nginx的配置文件,添加client_max_body_size 字段 注:client_max_body_size 必须要放在server下的server_name下,而不是放在locatio ...
- Educational Codeforces Round 77 (Rated for Div. 2) D A game with traps
题意:x正轴上有着一个陷阱的位置,开关和灵敏度,如果一个士兵灵敏度输给陷阱,他是过不去这个陷阱的幸运的是,你可以先过去把开关给关了,没错你是不怕陷阱的接下来呢你有操作,你移动一个,耗费一秒而你的团队需 ...
- Jenkins学习安装配置和使用
为了能够频繁地将软件的最新版本,及时.持续地交付给测试团队及质量控制团队,以供评审,所以引入持续集成工具Jenkins,从而实现公司新产品持续集成,自动化部署. 环境准备 ●操作系统:Windows1 ...