/**
* 二维码创建
* @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和文字描述的更多相关文章

  1. JAVA使用qrcode生成二维码(带logo/不带logo)

    /** * */ package qrcode; import java.awt.Color; import java.awt.Graphics2D; import java.awt.Image; i ...

  2. JAVA用QRCode生成二维码

    QRCode jar下载地址: 生成:http://www.swetake.com/qrcode/index-e.html 读取:https://zh.osdn.net/projects/qrcode ...

  3. Java与JS生成二维码

    1.二维码概念 二维码/二维条码是用某种特定的集合图形按一定规律在平面上(二维方向上)分布的黑白相间的图形记录数据符号信息的图片. 黑线是二进制的1,空白的地方是二进制的0,通过1.0这种数据组合用于 ...

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

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

  5. JAVA根据URL生成二维码图片、根据路径生成二维码图片

    引入jar包 zxing-2.3.0.jar.IKAnalyzer2012_u6.jar 下载地址:https://yvioo.lanzous.com/b00nlbp6h                ...

  6. java生成二维码(带logo)

    之前写过一篇不带logo的二维码实现方式,採用QRCode和ZXing两种方式 http://blog.csdn.net/xiaokui_wingfly/article/details/3947618 ...

  7. Java根据链接生成二维码

    Java根据链接生成二维码 相关 jar 包: core-3.1.0.jar 源码及 jar 包下载:http://files.cnblogs.com/files/liaolongjun/qrcode ...

  8. Java后台直接生成二维码介绍

    Java后台直接生成二维码 1.其实jquery也可以直接生成二维码的,但我测试的时候,二维码生成后太模糊,难以识别.所以在这里介绍在后来生成二维码的方式. 2.不善于文字描述,直接上代码了. imp ...

  9. C# 利用QRCode生成二维码图片

    网上生成二维码的组件是真多,可是真正好用的,并且生成速度很快的没几个,QRCode就是我在众多中找到的,它的生成速度快.但是网上关于它的使用说明,真的太少了,大都是千篇一律的复制粘贴.这是本要用它做了 ...

随机推荐

  1. [认证 & 授权] 3. 基于OAuth2的认证(译)

    OAuth 2.0 规范定义了一个授权(delegation)协议,对于使用Web的应用程序和API在网络上传递授权决策非常有用.OAuth被用在各钟各样的应用程序中,包括提供用户认证的机制.这导致许 ...

  2. Spring cloud Feign 深度学习与应用

    简介 Spring Cloud Feign是一个声明式的Web Service客户端,它的目的就是让Web Service调用更加简单.Feign提供了HTTP请求的模板,通过编写简单的接口和插入注解 ...

  3. SecureCRT登陆liunx 方向键失效

    使用SecureCRT登陆liunx(CenterOS)系统,发现删除(backspace)键.和上下左右键不起作用,解决如下: 先打开Options–>Session Options–> ...

  4. (转)两种高效过滤敏感词算法--DFA算法和AC自动机算法

    原文:https://blog.csdn.net/u013421629/article/details/83178970 一道bat面试题:快速替换10亿条标题中的5万个敏感词,有哪些解决思路? 有十 ...

  5. 深度好文:PHP写时拷贝与垃圾回收机制(转)

    原文地址:http://www.php100.com/9/20/87255.html 写入拷贝(Copy-on-write,简称COW)是一种计算机程序设计领域的优化策略.其核心思想是,如果有多个调用 ...

  6. JS---DOM---tab切换案例实现---排他

    tab切换案例实现 <!DOCTYPE html> <html> <head lang="en"> <meta charset=" ...

  7. Redis & memcached PK

    redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set ...

  8. ospf邻居建立过程

    1. hello报文的作用 邻居发现:自动发现邻居路由器. 邻居建立:完成Hello报文中的参数协商,建立邻居关系. 邻居保持:通过Keepalive机制,检测邻居运行状态. hello报文的发送时间 ...

  9. 【Java】导入项目时,出现The project cannot be built until build path errors are resolved错误解决方法

    先检查jar包,jar包的地址如果不一样需要remove后重新导入的,右键项目→Build Path. 看额外的jar包有没有×,地址正不正确,要是不正确,remove错误jar包,再点击Add Ex ...

  10. SqlException:ConnectionTimeout Expired. The timeout period elapsed during the post-login phase

    linux系统部署.netcore程序后,访问某台sqlserver 2008 R2数据库 Connection Timeout Expired. The timeout period elapsed ...