java springMVC生成二维码
Zxing是Google提供的工具,提供了二维码的生成与解析的方法,现在使用Java利用Zxing生成二维码
1),二维码的生成
将Zxing-core.jar 包加入到classpath下。
我的下载地址:http://i.cnblogs.com/Files.aspx 下zxing.zip包
1.RqCodeController 类
private static final Log logger = LogFactory.getLog(RqCodeController.class);
@RequestMapping("/gen.json")
public void gen(String url, HttpServletResponse response, Integer width, Integer height ) {
try {
int iWidth = (width == null?200: width);
int iHeight = (height==null?200: height);
MatrixToImageWriter.createRqCode(url, iWidth, iHeight
, response.getOutputStream());
} catch (Exception e) {
logger.error(String.format("生成二维码失败: url: %s", url), e);
}
}
2,MatrixToImageWriter类的方法
package com.web.util; import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Hashtable; import javax.imageio.ImageIO; import org.springframework.core.io.ClassPathResource; import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix; /**
* 二维码生成工具
*/
public class MatrixToImageWriter { private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
private static final int MARGIN = 1; //边框 private static final String FORMAT = "png"; private MatrixToImageWriter() {
} public static void createRqCode(String textOrUrl, int width, int height, OutputStream toStream)
throws WriterException, IOException { Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 内容所使用字符集编码
hints.put(EncodeHintType.MARGIN, new Integer(MARGIN)); BitMatrix bitMatrix = new MultiFormatWriter().encode(textOrUrl, BarcodeFormat.QR_CODE, width, height, hints); BufferedImage image = toBufferedImage(bitMatrix);
applyLogo(image);//应用LOGO writeToStream(image, FORMAT, toStream); } private static void applyLogo(BufferedImage image) throws IOException { Graphics2D gs = image.createGraphics(); ClassPathResource resource = new ClassPathResource("logo.png");//logo图片 // 载入logo
Image img = ImageIO.read(resource.getFile()); int left = image.getWidth() / 2 - img.getWidth(null) / 2;
int top = image.getHeight() / 2 - img.getHeight(null) / 2; gs.drawImage(img, left, top, null);
gs.dispose();
img.flush(); } private static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
}
}
return image;
} public static void writeToFile(BufferedImage image, String format, File file) throws IOException { if (!ImageIO.write(image, format, file)) {
throw new IOException("Could not write an image of format " + format + " to " + file);
}
} public static void writeToStream(BufferedImage image, String format, OutputStream stream) throws IOException {
if (!ImageIO.write(image, format, stream)) {
throw new IOException("Could not write an image of format " + format);
}
} }
上述编写的代码,就可传出一个二进制数,然后前端使用图片的格式将二进制数展现出来,就是一个二维码。
下面是页面生成,可以是链接,可以是文本
/*获取页面二维码*/
function share2dImg(link){
$("#shareImg").attr("src","/rqcode/gen.json?url="+link+"&width=200&height=200")
} $(function(){ /*微信分享的执行*/
var invitationCode = ajaxGetInfo();//不管登录与否,都传空,获取邀请码
var shareTitle = "送有8888元!";//分享的标题
var shareDesc = "送有8888元!";//分享的描述
var shareLink = "https://www.baidu.com/index.php?tn=monline_3_dg";//分享的链接
weixinShare(shareTitle,shareDesc,shareLink); //点击立即邀请,弹出界面框
$("#toInvite").click(function(){
$(".share-dialog").show();
}); //点击弹出界面框,回到基本页面
$(".share-dialog").click(function(){
$(this).hide();
}); share2dImg(encodeURIComponent(shareLink))//获取分享出去的二维码 });
对于二维码的解析,需要zxing一个辅助类( BufferedImageLuminanceSource),可以直接用。
偶遇晨光原创
2016-02-25
java springMVC生成二维码的更多相关文章
- 在java中生成二维码,并直接输出到jsp页面
在java中生成的二维码不存到磁盘里要直接输出到页面上,这就需要把生成的二维码直接以流的形式输出到页面上,我用的是myeclipse 和 tomcat 它的原理是:在加载页面时,根据img的src(c ...
- java zxing生成二维码
package zxing.test; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; i ...
- java实现生成二维码
package com.cn.test; import java.awt.Graphics2D; import java.awt.geom.AffineTransform; import java.a ...
- java Springboot 生成 二维码 +logo
上码,如有问题或者优化,劳请广友下方留言 1.工具类 import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHint ...
- Java——CaptchaUtil生成二维码乱码
前言 这个问题就是因为Linux上没有字体,你可以有两种方法,一个在生成的时候设置字体,一个就是安装字体. 默认的字体为Courier 乱码情况 步骤 安装字体工具 yum install -y fo ...
- Java zxing生成二维码所需的jar包
免费的,不需要积分. 共有2个jar包, 链接: https://pan.baidu.com/s/1QJcEkRQOp1NdrNAgGC6LvQ 密码: 4524
- java url生成二维码保存到本地
http://blog.sina.com.cn/s/blog_5a6efa330102v1lb.html http://blog.csdn.net/about58238/article/details ...
- 根据短链生成二维码并上传七牛云(Java)
通过短链生成二维码并上传七牛云(Java) 前言 网上这种帖子其实也是很多,大部分搜出来的是CSDN的,然后点进去一看都几乎一样:所以这次给个自己实践的例子记录. 这次也是通过搜索得到的一部分能实现这 ...
- java生成二维码(需导入第三方ZXing.jar包)
//这个类是用来解析,通过图片解析该图片的网页链接是什么 package util; import java.awt.Graphics2D;import java.awt.geom.AffineTra ...
随机推荐
- rails里routes配置文件里的resources和resource的区别
抄自 http://stackoverflow.com/questions/11356146/difference-between-resource-and-resources-in-rails-ro ...
- linux下mongodb定时备份指定的集合
目标:把一台linux机上mongodb的数据定时备份到另一台机上: 过程: 一开始打算使用mongoexport和mongoimport,但是总是会报“\x00”字符串不能识别的问题,后来就改成了m ...
- 返回顶部(解决IE6固定定位)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- C++ 类的动态组件化技术
序言: N年前,我们曾在软件开发上出现了这样的困惑,用VC开发COM组件过于复杂,用VB开发COM组件发现效率低,而且不能实现面向对象的很多特性,例如,继承,多态等.更况且如何快速封装利用历史遗留的大 ...
- linux下安装mysql数据库与相关操作
如下命令都是用root身份安装,或者在命令前加上sudo 采用yum安装方式安装 yum install mysql #安装mysql客户端 yum install mysql-server #安装m ...
- PLSQL_PLSQL中DML/DDL/DCL的概念和区分(概念)
2014-06-20 Created By BaoXinjian
- java浮点型比较大小
======1 java浮点型比较大小 Float.parseFloat(String)和Float.valueOf(String).floatValue()的区别 Float.parseFloa ...
- 使用UltraEdit+BCC5.5搭建C语言学习环境(转)
今天闲来无聊,想起以前学的C都差不多忘光了,想练练,先搭环境吧,vc bc之类都太大了,我以前在borland下过一个命令行编译工具不错,好像以前看到有人用ultraedit配合命令行工具做过一个开发 ...
- Android之SQLite
在模拟器运行的情况下,进入cmd运行adb shell 可进入模拟器的linux系统输入 lite3 mars_test_db 可进入sqlite模式.schema或者.sch 查看有哪些表 SQLi ...
- Python深入04 闭包
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 闭包(closure)是函数式编程的重要的语法结构.函数式编程是一种编程范式 (而 ...