Java实现二维码生成的方法
1、支持QRcode、ZXing 二维码生成、解析;
package com.thinkgem.jeesite.test; 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; import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map; /**
* 二维码生成工具类
* @author Cloud
* @data 2016-12-15
* QRCode
*/ public class QRCodeUtil{ //二维码颜色
private static final int BLACK = 0xFF000000;
//二维码颜色
private static final int WHITE = 0xFFFFFFFF; /**
* <span style="font-size:18px;font-weight:blod;">ZXing 方式生成二维码</span>
* @param text <a href="javascript:void();">二维码内容</a>
* @param width 二维码宽
* @param height 二维码高
* @param outPutPath 二维码生成保存路径
* @param imageType 二维码生成格式
*/
public static void zxingCodeCreate(String text, int width, int height, String outPutPath, String imageType){
Map<EncodeHintType, String> his = new HashMap<EncodeHintType, String>();
//设置编码字符集
his.put(EncodeHintType.CHARACTER_SET, "utf-8");
try {
//1、生成二维码
BitMatrix encode = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, his); //2、获取二维码宽高
int codeWidth = encode.getWidth();
int codeHeight = encode.getHeight(); //3、将二维码放入缓冲流
BufferedImage image = new BufferedImage(codeWidth, codeHeight, BufferedImage.TYPE_INT_RGB);
for (int i = 0; i < codeWidth; i++) {
for (int j = 0; j < codeHeight; j++) {
//4、循环将二维码内容定入图片
image.setRGB(i, j, encode.get(i, j) ? BLACK : WHITE);
}
}
File outPutImage = new File(outPutPath);
//如果图片不存在创建图片
if(!outPutImage.exists())
outPutImage.createNewFile();
//5、将二维码写入图片
ImageIO.write(image, imageType, outPutImage);
} catch (WriterException e) {
e.printStackTrace();
System.out.println("二维码生成失败");
} catch (IOException e) {
e.printStackTrace();
System.out.println("生成二维码图片失败");
}
} public static void main(String[] args) throws Exception { // QRcode 二维码生成测试
QRCodeUtil.zxingCodeCreate("祝小仙女节日快乐",300, 300, "E:/borths.jpg", "jpg");
QRCodeUtil.zxingCodeCreate("https://www.cnblogs.com/zhukaixin/",300, 300, "E:/borths.jpg", "jpg");
System.out.println("success");
}
}
二维码图片:



Java实现二维码生成的方法的更多相关文章
- java实现二维码生成的几个方法
1: 使用SwetakeQRCode在Java项目中生成二维码 http://swetake.com/qr/ 下载地址 或着http://sourceforge.jp/projects/qrcode/ ...
- java web 二维码生成
pom支持: <!-- 二维码支持包 start--> <dependency> <groupId>com.google.zxing</groupId> ...
- PHP二维码生成的方法(google APi,PHP类库,libqrencode等)
原文地址: http://blog.csdn.net/liuxinmingcode/article/details/7910975 ================================== ...
- JAVA实现二维码生成加背景图
pom.xml依赖 <!-- 二维码生成 --> <!-- https://mvnrepository.com/artifact/com.google.zxing/c ...
- Java条形码/二维码生成和解析
注意-本类依赖jar包文件:core.jar和zxing-javase.jar 下载jar文件,到本博客文件栏目下载. import com.google.zxing.BarcodeFormat; i ...
- Android 二维码 生成和识别(附Demo源码)
今天讲一下目前移动领域很常用的技术——二维码.现在大街小巷.各大网站都有二维码的踪迹,不管是IOS. Android.WP都有相关支持的软件.之前我就想了解二维码是如何工作,最近因为工作需要使用相关技 ...
- Android 二维码 生成和识别(转)
原博客地址 :http://www.cnblogs.com/weixing/archive/2013/08/28/3287120.html 还有几个写的也可以参考一下:http://www.itnos ...
- android 二维码生成+扫描
android 二维码生成+扫描 1.在Android应用当中,很多时候都要用到二维码扫描,来避免让用户手动输入的麻烦. Google官方自己推出了一个二维码开源项目:ZXing库. 2.这里简单介绍 ...
- 【转】Android 二维码 生成和识别(附Demo源码)--不错
原文网址:http://www.cnblogs.com/mythou/p/3280023.html 今天讲一下目前移动领域很常用的技术——二维码.现在大街小巷.各大网站都有二维码的踪迹,不管是IOS. ...
随机推荐
- springBean之BeanFactory与ApplicationContext
一.主要区别: 两者都是通过xml配置文件加载bean,ApplicationContext和BeanFacotry相比,提供了更多的扩展功能,但其主要区别在于后者是延迟加载,如果Bean的某一个属性 ...
- JS 实现 unicode 中文互转
// 转为unicode 编码 function encodeUnicode(str) { var res = []; for ( var i=0; i<str.length; i++ ) { ...
- Linux yum失败解决
Linux yum失败解决 问题: 在CentOS 5.5中需要使用yum安装程序,出现错误: There was a problem importing one of the Python modu ...
- Python倒序循环列表(序列)
如果要倒序遍历访问序列中的元素,可以对该序列使用reversed() 函数,reversed函数会生成一份倒序列表的拷贝,但是不会改变原列表.这个函数理解起来很自然,例如 for i in rever ...
- Django框架 之 Ajax
Django框架 之 Ajax 浏览目录 AJAX准备知识 AJAX与XML的比较 AJAX简介 jQuery实现的ajax AJAX参数 AJAX请求如何设置csrf_token 序列化 一.AJA ...
- SpringMVC——映射请求参数
Spring MVC 通过分析处理方法的签名,将 HTTP 请求信息绑定到处理方法的相应人参中. @PathVariable @RequestParam @RequestHeader 等) Sprin ...
- LoadRunner 学习(基础一)
最近开始正式系统地学习LoadRunner11.本想在自己觉得确实学到了比较有成就感的时候再mark一下,写个博客分享.阶段性地或者在自己有所小收获的时候,做做笔记分享下也好.这次作为开篇,我想记录下 ...
- xen创建pvm和hvm的过程
these are the basic steps of installing domU with xen-tools in ubuntu13.04 64bit in xen4.3 you can a ...
- sql从简单到高级
Ø 基本常用查询 --select select * from student; --all 查询所有 select all sex from student; --distinct 过滤重复 sel ...
- 如何恢复VS2013代码实时校验功能
VS2013在某一天突然无法进行实时代码校验了,只有在编译的时候,错误列表才显示语法错误 怎么来解决这个问题呢?试试环境重置吧. 首先:打开工具菜单,选择“导入和导出设置”. 其次:可以先导出选定 ...