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. php接二进制文件

    PHP默认只识别application/x-www.form-urlencoded标准的数据类型. 因此,对型如text/xml 或者 soap 或者 application/octet-stream ...

  2. js setTimeout

    setTimeout用法 //每个0.5秒钟改变字体和背景颜色,字体一闪一闪的效果 var flag = 0; function start(){ var text = document.getEle ...

  3. Centos7安装Zabbix3.0

    1.安装服务器端包 #rpm -ivh http://repo.zabbix.com/zabbix/3.0/rhel/7/x86_64/zabbix-release-3.0-1.el7.noarch. ...

  4. Understanding and Managing SMTP Virtual Servers

    Simple Mail Transfer Protocol (SMTP) Service Overview The Simple Mail Transfer Protocol (SMTP) servi ...

  5. IE浏览器的兼容模式代码细节解读

    兼容性对于网页设计师来说非常重要.虽然最好是建立一个完全不需依赖任何网页浏览器特性或功能的网站,但是有时候这是不可能实现的.而文件兼容模式能将网页限制在某个特定版本的IE中.可以使用 X-UA-Com ...

  6. [Freescale]Freescale L3.14.52_1.1.0 yocto build

    可参照:http://blog.csdn.net/wince_lover/article/details/51456745 1. Refer to <基于i.mx6处理器的Yocto项目及Lin ...

  7. [zsh]zsh常用小技巧

    文章来源http://yijiebuyi.com/blog/3154040ae0aa3d352c61a10f2664591e.html shell基础: 查看当前使用shell类型: ->ech ...

  8. Hadoop学习2--Linux准备及环境准备

    1.环境安装: 虚拟机:VMware Player 系统:Ubuntu12 注意事项:注意位数,包括系统,java,Hadoop 2.切换账号 当前登录账号是自己的账号,如果想切换到root,且是第一 ...

  9. python(13)线程池:threading

    先上代码: pool = threadpool.ThreadPool(10) #建立线程池,控制线程数量为10 reqs = threadpool.makeRequests(get_title, da ...

  10. Spring中IoC的入门实例

    Spring中IoC的入门实例 Spring的模块化是很强的,各个功能模块都是独立的,我们可以选择的使用.这一章先从Spring的IoC开始.所谓IoC就是一个用XML来定义生成对象的模式,我们看看如 ...