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生成二维码的更多相关文章

  1. 在java中生成二维码,并直接输出到jsp页面

    在java中生成的二维码不存到磁盘里要直接输出到页面上,这就需要把生成的二维码直接以流的形式输出到页面上,我用的是myeclipse 和 tomcat 它的原理是:在加载页面时,根据img的src(c ...

  2. java zxing生成二维码

    package zxing.test; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; i ...

  3. java实现生成二维码

    package com.cn.test; import java.awt.Graphics2D; import java.awt.geom.AffineTransform; import java.a ...

  4. java Springboot 生成 二维码 +logo

    上码,如有问题或者优化,劳请广友下方留言 1.工具类 import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHint ...

  5. Java——CaptchaUtil生成二维码乱码

    前言 这个问题就是因为Linux上没有字体,你可以有两种方法,一个在生成的时候设置字体,一个就是安装字体. 默认的字体为Courier 乱码情况 步骤 安装字体工具 yum install -y fo ...

  6. Java zxing生成二维码所需的jar包

    免费的,不需要积分. 共有2个jar包, 链接: https://pan.baidu.com/s/1QJcEkRQOp1NdrNAgGC6LvQ 密码: 4524

  7. java url生成二维码保存到本地

    http://blog.sina.com.cn/s/blog_5a6efa330102v1lb.html http://blog.csdn.net/about58238/article/details ...

  8. 根据短链生成二维码并上传七牛云(Java)

    通过短链生成二维码并上传七牛云(Java) 前言 网上这种帖子其实也是很多,大部分搜出来的是CSDN的,然后点进去一看都几乎一样:所以这次给个自己实践的例子记录. 这次也是通过搜索得到的一部分能实现这 ...

  9. java生成二维码(需导入第三方ZXing.jar包)

    //这个类是用来解析,通过图片解析该图片的网页链接是什么 package util; import java.awt.Graphics2D;import java.awt.geom.AffineTra ...

随机推荐

  1. Centos7 + Windows7 双系统

    以前装双系统只要先装Windows7,然后再装Centos7的话,grub会自动添加原有的Windows7系统.不过在新的Centos7中需要手动修改. 步骤如下 $ sudo vi /etc/gru ...

  2. Windows2012 cannot access netapp CIFS share

    NAS1> options cifs.smb2.signing.requiredcifs.smb2.signing.required off NAS1> options cifs.smb2 ...

  3. Linux开机自动挂载存储

    今天有个系统的开发人员跟我说,他们测试系统出现问题重启了服务器后就发现找不到存储了. 唉,不用说了.肯定没有自动加载存储呗.一个堂堂的技术顾问,一天4-5K工资的人连这个操作都不会啊?忍了... 登录 ...

  4. FLEX 特效

    一.简介: flex特效是ria应用程序的rich的重要组成部分. EffectManager类管理所有的特效实例以避免不必要的定时器和方法调用造成的内内存使用过大.一个效果由两部分组成:一是效果的E ...

  5. Redis作者谈Redis应用场景(转)

    毫无疑问,Redis开创了一种新的数据存储思路,使用Redis,我们不用在面对功能单调的数据库时,把精力放在如何把大象放进冰箱这样的问题上,而是利用Redis灵活多变的数据结构和数据操作,为不同的大象 ...

  6. C/C++代码静态分析插件 SourceInsight_Scan

    sourceinsight-scan 是一款集成在 SourceInsight 中的c/c++代码静态分析插件,集成了cppcheck,coverity,pclint等业界优秀的静态分析工具的优点. ...

  7. tomcat服务器配置多个项目

    修改tomcat的server.xml文件中的Engine标签下的Host标签如下: <Host name="www.a.com" appBase="webapps ...

  8. BloomFilter–大规模数据处理利器(转)

    BloomFilter–大规模数据处理利器 Bloom Filter是由Bloom在1970年提出的一种多哈希函数映射的快速查找算法.通常应用在一些需要快速判断某个元素是否属于集合,但是并不严格要求1 ...

  9. 关于 MySQL UTF8 编码下生僻字符插入失败/假死问题的分析

    原文:http://my.oschina.net/leejun2005/blog/343353 目录[-] 1.问题:mysql 遇到某些中文插入异常 2.原因:此 utf8 非彼 utf8 3.解决 ...

  10. loadrunner破解

    如果报的violation的错误,就下载hp.lr即deletelicense.exe软件运行即可以清楚注册表,然后再用管理员运行loadrunner再注册就可以了.