java生成二维码(带logo)
之前写过一篇不带logo的二维码实现方式,採用QRCode和ZXing两种方式
http://blog.csdn.net/xiaokui_wingfly/article/details/39476185
这里介绍一下ZXing的带logo实现方式,详细实现參考一下代码,測试使用ZXingCodeUtil的main方法。
视频链接地址:http://www.tudou.com/programs/view/WSMfx1RL-ao/。视频地址中包括图片二维码流输出方式
- LogoConfig logo背景配置类
- ZXingConfig 二维码配置信息
- BufferedImageLuminanceSource 二维码解析使用类
- ZXingCodeUtil二维码生成
LogoConfig.java
package com.weixin.pay.orcode.logo;
import java.awt.Color;
/**
* logo背景配置
*
* @author X-rapido
*/
public class LogoConfig {
public static final Color DEFAULT_BORDERCOLOR = Color.WHITE; // logo默认边框颜色
public static final int DEFAULT_BORDER = 2; // logo默认边框宽度
public static final int DEFAULT_LOGOPART = 5; // logo大小默觉得照片的1/5
private final int border = DEFAULT_BORDER; // 默认边框宽度
private final Color borderColor; // 边框颜色
private final int logoPart; // 边框外围宽度
/**
* 二维码无參构造函数 默认设置Logo图片底色白色宽度2
*/
public LogoConfig() {
this(DEFAULT_BORDERCOLOR, DEFAULT_LOGOPART);
}
/**
* 二维码有參构造函数
*
* @param borderColor 边框颜色
* @param logoPart 边框宽度
*/
public LogoConfig(Color borderColor, int logoPart) {
// 设置边框
this.borderColor = borderColor;
// 设置边框宽度
this.logoPart = logoPart;
}
/**
* 获取边框颜色
*
* @return 获取边框颜色
*/
public Color getBorderColor() {
return borderColor;
}
/**
* 获取边框
*
* @return 获取边框
*/
public int getBorder() {
return border;
}
/**
* 外围边宽
*
* @return 外围边宽
*/
public int getLogoPart() {
return logoPart;
}
}
BufferedImageLuminanceSource.java
package com.weixin.pay.orcode.logo;
import com.google.zxing.LuminanceSource;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
/**
* 二维码解析使用类
*
* @author X-rapido
*
*/
public class BufferedImageLuminanceSource extends LuminanceSource {
private final BufferedImage image;
private final int left;
private final int top;
public BufferedImageLuminanceSource(BufferedImage image) {
this(image, 0, 0, image.getWidth(), image.getHeight());
}
public BufferedImageLuminanceSource(BufferedImage image, int left, int top, int width, int height) {
super(width, height);
int sourceWidth = image.getWidth();
int sourceHeight = image.getHeight();
if (left + width > sourceWidth || top + height > sourceHeight) {
throw new IllegalArgumentException("Crop rectangle does not fit within image data.");
}
for (int y = top; y < top + height; y++) {
for (int x = left; x < left + width; x++) {
if ((image.getRGB(x, y) & 0xFF000000) == 0) {
image.setRGB(x, y, 0xFFFFFFFF); // = white
}
}
}
this.image = new BufferedImage(sourceWidth, sourceHeight, BufferedImage.TYPE_BYTE_GRAY);
this.image.getGraphics().drawImage(image, 0, 0, null);
this.left = left;
this.top = top;
}
@Override
public byte[] getRow(int y, byte[] row) {
if (y < 0 || y >= getHeight()) {
throw new IllegalArgumentException("Requested row is outside the image: " + y);
}
int width = getWidth();
if (row == null || row.length < width) {
row = new byte[width];
}
image.getRaster().getDataElements(left, top + y, width, 1, row);
return row;
}
@Override
public byte[] getMatrix() {
int width = getWidth();
int height = getHeight();
int area = width * height;
byte[] matrix = new byte[area];
image.getRaster().getDataElements(left, top, width, height, matrix);
return matrix;
}
@Override
public boolean isCropSupported() {
return true;
}
@Override
public LuminanceSource crop(int left, int top, int width, int height) {
return new BufferedImageLuminanceSource(image, this.left + left, this.top + top, width, height);
}
@Override
public boolean isRotateSupported() {
return true;
}
@Override
public LuminanceSource rotateCounterClockwise() {
int sourceWidth = image.getWidth();
int sourceHeight = image.getHeight();
AffineTransform transform = new AffineTransform(0.0, -1.0, 1.0, 0.0, 0.0, sourceWidth);
BufferedImage rotatedImage = new BufferedImage(sourceHeight, sourceWidth, BufferedImage.TYPE_BYTE_GRAY);
Graphics2D g = rotatedImage.createGraphics();
g.drawImage(image, transform, null);
g.dispose();
int width = getWidth();
return new BufferedImageLuminanceSource(rotatedImage, top, sourceWidth - (left + width), getHeight(), width);
}
}
ZXingConfig.java
package com.weixin.pay.orcode.logo;
import java.util.Map;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
/**
* 二维码配置信息
*
* @author X-rapido
*
*/
public class ZXingConfig {
private boolean logoFlg = false; // 是否加入Log图片
private String content; // 二维码编码内容
private BarcodeFormat barcodeformat = BarcodeFormat.QR_CODE; //编码类型
private int width = 200; // 生成图片宽度
private int height = 200; // 生成图片高度
private Map<EncodeHintType, ?> hints; // 设置參数
private String logoPath; // Logo图片路径
private String putPath; // 图片输出路径
private LogoConfig LogoConfig; // logo图片參数
/**
* 获取 是否加入Log图片
*
* @return logoFlg 是否加入Log图片
*/
public boolean isLogoFlg() {
return logoFlg;
}
/**
* 设置 是否加入Log图片
*
* @param logoFlg 是否加入Log图片
*/
public void setLogoFlg(boolean logoFlg) {
this.logoFlg = logoFlg;
}
/**
* 获取 二维码编码内容
*
* @return content 二维码编码内容
*/
public String getContent() {
return content;
}
/**
* 设置 二维码编码内容
*
* @param content 二维码编码内容
*/
public void setContent(String content) {
this.content = content;
}
/**
* 获取 编码类型
*
* @return barcodeformat 编码类型
*/
public BarcodeFormat getBarcodeformat() {
return barcodeformat;
}
/**
* 设置 编码类型
*
* @param barcodeformat 编码类型
*/
public void setBarcodeformat(BarcodeFormat barcodeformat) {
this.barcodeformat = barcodeformat;
}
/**
* 获取 生成图片宽度
*
* @return width 生成图片宽度
*/
public int getWidth() {
return width;
}
/**
* 设置 生成图片宽度
*
* @param width 生成图片宽度
*/
public void setWidth(int width) {
this.width = width;
}
/**
* 获取 生成图片高度
*
* @return height 生成图片高度
*/
public int getHeight() {
return height;
}
/**
* 设置 生成图片高度
*
* @param height 生成图片高度
*/
public void setHeight(int height) {
this.height = height;
}
/**
* 获取 设置參数
*
* @return hints 设置參数
*/
public Map<EncodeHintType, ?> getHints() {
return hints;
}
/**
* 设置 设置參数
*
* @param hints 设置參数
*/
public void setHints(Map<EncodeHintType, ?
> hints) {
this.hints = hints;
}
/**
* 获取 Logo图片路径
*
* @return logoPath Logo图片路径
*/
public String getLogoPath() {
return logoPath;
}
/**
* 设置 Logo图片路径
*
* @param logoPath Logo图片路径
*/
public void setLogoPath(String logoPath) {
this.logoPath = logoPath;
}
/**
* 获取 图片输出路劲
*
* @return putPath 图片输出路劲
*/
public String getPutPath() {
return putPath;
}
/**
* 设置 图片输出路劲
*
* @param putPath 图片输出路劲
*/
public void setPutPath(String putPath) {
this.putPath = putPath;
}
/**
* 获取 logoConfig
*
* @return logoConfig logoConfig
*/
public LogoConfig getLogoConfig() {
return LogoConfig;
}
/**
* 设置 logoConfig
*
* @param logoConfig logoConfig
*/
public void setLogoConfig(LogoConfig logoConfig) {
LogoConfig = logoConfig;
}
}
ZXingCodeUtil.java
package com.weixin.pay.orcode.logo;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.weixin.pay.orcode.ZXingConfig;
/**
* 二维码生成google zxing
*
* @author X-rapido
*
*/
public class ZXingCodeUtil {
/**
* 二维码图片加入Logo
*
* @param bim 图片流
* @param logoPic Logo图片物理位置
* @param logoConfig Logo图片设置參数
* @throws Exception 异常上抛
*/
private void addLogo_QRCode(BufferedImage bim, File logoPic, LogoConfig logoConfig) throws Exception {
try {
// 对象流传输
BufferedImage image = bim;
Graphics2D g = image.createGraphics();
// 读取Logo图片
BufferedImage logo = ImageIO.read(logoPic);
// 设置logo的大小,本人设置为二维码图片的20%,由于过大会盖掉二维码
int widthLogo = logo.getWidth(null) > image.getWidth() * 2 / 10 ? (image.getWidth() * 2 / 10) : logo.getWidth(null), heightLogo = logo
.getHeight(null) > image.getHeight() * 2 / 10 ?
(image.getHeight() * 2 / 10) : logo.getWidth(null);
// 计算图片放置位置
// logo放在中心
int x = (image.getWidth() - widthLogo) / 2;
int y = (image.getHeight() - heightLogo) / 2;
// 開始绘制图片
g.drawImage(logo, x, y, widthLogo, heightLogo, null);
g.drawRoundRect(x, y, widthLogo, heightLogo, 15, 15);
g.setStroke(new BasicStroke(logoConfig.getBorder()));
g.setColor(logoConfig.getBorderColor());
g.drawRect(x, y, widthLogo, heightLogo);
g.dispose();
logo.flush();
image.flush();
} catch (Exception e) {
throw e;
}
}
/**
* 二维码的解析
*
* @param image 图片文件流
* @return 解析后的Result结果集
* @throws Exception 错误异常上抛
*/
@SuppressWarnings("unchecked")
public Result parseQR_CODEImage(BufferedImage image) throws Exception {
// 设置解析
Result result = null;
try {
MultiFormatReader formatReader = new MultiFormatReader();
LuminanceSource source = new BufferedImageLuminanceSource(image);
Binarizer binarizer = new HybridBinarizer(source);
BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
@SuppressWarnings("rawtypes")
Map hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
result = formatReader.decode(binaryBitmap, hints);
System.out.println("resultFormat = " + result.getBarcodeFormat());
System.out.println("resultText = " + result.getText());
} catch (Exception e) {
throw e;
}
return result;
}
/**
* 生成二维码bufferedImage图片
* @param zxingconfig 二维码配置信息
* @return 生成后的 BufferedImage
* @throws Exception 异常上抛
*/
public BufferedImage getQR_CODEBufferedImage(ZXingConfig zxingconfig) throws Exception {
// Google配置文件
MultiFormatWriter multiFormatWriter = null;
BitMatrix bm = null;
BufferedImage image = null;
try {
multiFormatWriter = new MultiFormatWriter();
// 參数顺序分别为:编码内容。编码类型,生成图片宽度,生成图片高度,设置參数
bm = multiFormatWriter.encode(zxingconfig.getContent(), zxingconfig.getBarcodeformat(), zxingconfig.getWidth(), zxingconfig.getHeight(),
zxingconfig.getHints());
int w = bm.getWidth();
int h = bm.getHeight();
image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
// 開始利用二维码数据创建Bitmap图片,分别设为黑白两色
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
image.setRGB(x, y, bm.get(x, y) ?
Color.BLACK.getRGB() : Color.WHITE.getRGB());
}
}
// 是否设置Logo图片
if (zxingconfig.isLogoFlg()) {
this.addLogo_QRCode(image, new File(zxingconfig.getLogoPath()), zxingconfig.getLogoConfig());
}
} catch (WriterException e) {
throw e;
}
return image;
}
/**
* 设置二维码的格式參数
*
* @return
*/
public Map<EncodeHintType, Object> getDecodeHintType() {
// 用于设置QR二维码參数
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
// 设置QR二维码的纠错级别(H为最高级别)详细级别信息
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
// 设置编码方式
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.MARGIN, 0);
hints.put(EncodeHintType.MAX_SIZE, 350);
hints.put(EncodeHintType.MIN_SIZE, 100);
return hints;
}
/**
* 这个工具类 ZXingCodeUtil 是提供二维码图片生成的工具 首先使用的时候须要实例化
* ZXingCodeUtil 然后实例化參数 ZXingConfig 和 LogoConfig 通过以下的演示能够详细看參数是依照什么循序进行设置 最后调用
* ZXingCodeUtil 中方法 getQR_CODEBufferedImage来生成二维码
*/
public static void main(String[] args) throws WriterException {
String content = "http://www.baidu.com";
System.out.println("inputParam:" + content);
try {
// 生成二维码
File file = new File("D:/Michael_QRCode.png");
ZXingCodeUtil zp = new ZXingCodeUtil(); // 实例化二维码工具
ZXingConfig zxingconfig = new ZXingConfig(); // 实例化二维码配置參数
zxingconfig.setHints(zp.getDecodeHintType()); // 设置二维码的格式參数
zxingconfig.setContent(content);// 设置二维码生成内容
zxingconfig.setLogoPath("D:/logo.jpg"); // 设置Logo图片
zxingconfig.setLogoConfig(new LogoConfig()); // Logo图片參数设置
zxingconfig.setLogoFlg(true); // 设置生成Logo图片
BufferedImage bim = zp.getQR_CODEBufferedImage(zxingconfig);// 生成二维码
ImageIO.write(bim, "png", file); // 图片写出
Thread.sleep(500); // 缓冲
zp.parseQR_CODEImage(bim); // 解析调用
} catch (Exception e) {
e.printStackTrace();
}
}
}
资源Demo实例,点击二维码自己主动切换验证码,下载链接:
http://download.csdn.net/detail/xiaokui_wingfly/8846895
java生成二维码(带logo)的更多相关文章
- C# 生成二维码(带Logo)
C# 生成二维码(带Logo) 第一种方式 我们需要引用 ThoughtWorks.QRCode.dll 生成带logo二维码(framework4.0以上) 下载地址:https://pan.ba ...
- (转)js jquery.qrcode生成二维码 带logo 支持中文
场景:公司最最近在开发二维码支付业务,所以需要做一个html5中的二维码生成和部署! 前天用js生成二维码,节省服务器资源及带宽 原版jquery.qrcode不能生成logo,本文采用的是修改版 1 ...
- jquery.qrcode 生成二维码带logo
<div id="container">这里是二维码显示位置</div> <script language="JavaScript" ...
- java 生成二维码、可带LOGO、可去白边
1.准备工作 所需jar包: JDK 1.6: commons-codec-1.11.jar core-2.2.jar javase-2.2.jar JDK 1.7: commons-codec- ...
- java 生成二维码后叠加LOGO并转换成base64
1.代码 见文末推荐 2.测试 测试1:生成base64码 public static void main(String[] args) throws Exception { String dat ...
- jquery动态生成二维码添加自定义logo
动态生成二维码中间带logo. jquery.qrcode.js 动态生成二维码api很简单. 引入jquer(版本任意),引入jquery.qrcode.js 不需要中间带logo这样就可以了.带l ...
- 利用JAVA生成二维码
本文章整理于慕课网的学习视频<JAVA生成二维码>,如果想看视频内容请移步慕课网. 维基百科上对于二维码的解释. 二维条码是指在一维条码的基础上扩展出另一维具有可读性的条码,使用黑白矩形图 ...
- java生成二维码打印到浏览器
java生成二维码打印到浏览器 解决方法: pom.xml的依赖两个jar包: <!-- https://mvnrepository.com/artifact/com.google.zxin ...
- 二维码相关---java生成二维码名片,而且自己主动保存到手机通讯录中...
版权声明:本文为博主原创文章,未经博主credreamer 同意不得转载 违者追究法律责任. https://blog.csdn.net/lidew521/article/details/244418 ...
随机推荐
- Javascript 优化
Javascript 优化 作者:@gzdaijie本文为作者原创,转载请注明出处:http://www.cnblogs.com/gzdaijie/p/5324489.html 目录 1.全局变量污染 ...
- Mysql insert声明优化
1) 假设你同一时候从同一客户插入非常多行,使用多个值表的INSERT语句. 这比使用分开INSERT语句快(在一些情况中几倍). Insert into test values(1,2),(1 ...
- go - 内置基础类型
Go 语言中包括以下内置基础类型: 布尔型:bool 整型:int int64 int32 int16 int8 uint8(byte) uint16 uint32 uint64 uint 浮点型:f ...
- Java 并发专题 : Semaphore 实现 互斥 与 连接池
继续并发方面的知识.今天介绍Semaphore,同样在java.util.concurrent包下. 本来准备通过例子,从自己实现到最后使用并发工具实现,但是貌似效果并不是很好,有点太啰嗦的感觉,所有 ...
- 举例说,在命令模式(Command Pattern)
在前面加上 谈到命令,大部分的人脑海中会想到以下这幅画面 这在现实生活中是一副讽刺漫画,做决定的人不清楚运行决定的人有何特点,瞎指挥.外行领导内行说的就是这样的.只是在软件设计领域,我们显然要为这 ...
- Android项目包装apk和apk反编译,xml反编译
一.项目和一般原则其不足之处包 (1)开发一个简单的项目.当发布了APK档.假设我们不使用签名的方式,直接地bin文件夹中找到*.apk档.非常方便,但是,当我们在使用的用户,可能有其他方案覆盖安装. ...
- bin home
bin=$(cd `dirname $0`;pwd)home=$(dirname $bin)
- Linux下PS命令详解 (转)
要对系统中进程进行监测控制,查看状态,内存,CPU的使用情况,使用命令:/bin/ps (1)ps :是显示瞬间进程的状态,并不动态连续: (2)top:如果想对进程运行时间监控,应该用 top 命令 ...
- 原生javascript学习
首先在这里要非常感谢无私分享作品的网友们,这些代码片段主要由网友们平时分享的作品代码里面和经常去逛网站然后查看源文件收集到的.把平时网站上常用的一些实用功能代码片段通通收集起来,方便网友们学习使用,利 ...
- Java新手入门的30个基本概念
Java已经成为一个庞大而复杂的技术平台,对于开发者而言,特别是刚開始学习的人,要想更好的掌握Java技术,深入理解基本概念不可缺少,能够帮助你提高对Java的进一步了解.以下为你介绍了Java语言的 ...