java二维码生成工具
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Map; import javax.imageio.ImageIO; import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.ReaderException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer; /**
* 利用zxing开源工具生成二维码QRCode
*
* @date 2012-10-26
* @author xhw
*
*/
public class QRCode {
private static final int BLACK = 0xff000000;
private static final int WHITE = 0xFFFFFFFF; /**
* @param args
*/
public static void main(String[] args) { /**
* 在com.google.zxing.MultiFormatWriter类中,定义了一些我们不知道的码,二维码只是其中的一种<br>
* public BitMatrix encode(String contents,
BarcodeFormat format,
int width, int height,
Map<EncodeHintType,?> hints) throws WriterException {
Writer writer;
switch (format) {
case EAN_8:
writer = new EAN8Writer();
break;
case EAN_13:
writer = new EAN13Writer();
break;
case UPC_A:
writer = new UPCAWriter();
break;
case QR_CODE:
writer = new QRCodeWriter();
break;
case CODE_39:
writer = new Code39Writer();
break;
case CODE_128:
writer = new Code128Writer();
break;
case ITF:
writer = new ITFWriter();
break;
case PDF_417:
writer = new PDF417Writer();
break;
case CODABAR:
writer = new CodaBarWriter();
break;
default:
throw new IllegalArgumentException("No encoder available for format " + format);
}
return writer.encode(contents, format, width, height, hints);
} */
String filePostfix="png";
File file = new File("C://test_QR_CODE."+filePostfix);
QRCode.encode("http://www.cnblogs.com/sprinng", file,filePostfix, BarcodeFormat.QR_CODE, 500, 500, null);
QRCode.decode(file);
} /**
* 生成QRCode二维码<br>
* 在编码时需要将com.google.zxing.qrcode.encoder.Encoder.java中的<br>
* static final String DEFAULT_BYTE_MODE_ENCODING = "ISO8859-1";<br>
* 修改为UTF-8,否则中文编译后解析不了<br>
* @param contents 二维码的内容
* @param file 二维码保存的路径,如:C://test_QR_CODE.png
* @param filePostfix 生成二维码图片的格式:png,jpeg,gif等格式
* @param format qrcode码的生成格式
* @param width 图片宽度
* @param height 图片高度
* @param hints
*/
public static void encode(String contents, File file,String filePostfix, BarcodeFormat format, int width, int height, Map<EncodeHintType, ?> hints) {
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, format, width, height);
writeToFile(bitMatrix, filePostfix, file);
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 生成二维码图片<br>
*
* @param matrix
* @param format
* 图片格式
* @param file
* 生成二维码图片位置
* @throws IOException
*/
public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {
BufferedImage image = toBufferedImage(matrix);
ImageIO.write(image, format, file);
} /**
* 生成二维码内容<br>
*
* @param matrix
* @return
*/
public static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, matrix.get(x, y) == true ? BLACK : WHITE);
}
}
return image;
} /**
* 解析QRCode二维码
*/
@SuppressWarnings("unchecked")
public static void decode(File file) {
try {
BufferedImage image;
try {
image = ImageIO.read(file);
if (image == null) {
System.out.println("Could not decode image");
}
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result;
@SuppressWarnings("rawtypes")
Hashtable hints = new Hashtable();
//解码设置编码方式为:utf-8
hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
result = new MultiFormatReader().decode(bitmap, hints);
String resultStr = result.getText();
System.out.println("解析后内容:" + resultStr);
} catch (IOException ioe) {
System.out.println(ioe.toString());
} catch (ReaderException re) {
System.out.println(re.toString());
}
} catch (Exception ex) {
System.out.println(ex.toString());
}
}
}
java二维码生成工具的更多相关文章
- Java 二维码生成工具类
/** * 二维码 工具 * * @author Rubekid * */ public class QRcodeUtils { /** * 默认version */ public static fi ...
- Java二维码生成与解码工具Zxing使用
Zxing是Google研发的一款非常好用的开放源代码的二维码生成工具,目前源码托管在github上,源码地址: https://github.com/zxing/zxing 可以看到Zxing库有很 ...
- java二维码生成-谷歌(Google.zxing)开源二维码生成学习及实例
java二维码生成-谷歌(Google.zxing)开源二维码生成的实例及介绍 我们使用比特矩阵(位矩阵)的QR码编码在缓冲图片上画出二维码 实例有以下一个传入参数 OutputStream ou ...
- Java二维码生成与解码
基于google zxing 的Java二维码生成与解码 一.添加Maven依赖(解码时需要上传二维码图片,所以需要依赖文件上传包) <!-- google二维码工具 --> &l ...
- 二维码生成工具类java版
注意:这里我不提供所需jar包的路径,我会把所有引用的jar包显示出来,大家自行Google package com.net.util; import java.awt.BasicStroke; im ...
- 谷歌zxing 二维码生成工具
一.加入maven依赖 <!-- 谷歌zxing 二维码 --> <dependency> <groupId>com.google.zxing</groupI ...
- java 二维码生成(可带图片)springboot版
本文(2019年6月29日 飞快的蜗牛博客) 有时候,男人和女人是两个完全不同的世界,男人的玩笑和女人的玩笑也完全是两码事,爱的人完全不了解你,你也不要指望一个女人了解你,所以男的不是要求别人怎么样, ...
- vue项目条形码和二维码生成工具试用
项目开发需要,优惠券分不同类型,简单的使用id生成条形码供店铺使用,麻烦点的需要多个字段的就需要使用二维码来展示了,对应的效果如下 条形码(一维码)使用工具code128 需引入code128.js ...
- java 二维码生成
直接上代码: 二维码生成核心类: package com.bbkj.wechat.tool; import java.awt.image.BufferedImage; import java.io.F ...
随机推荐
- Spring <context:annotation-config/> 解说(转)
在基于主机方式配置Spring的配置文件中,你可能会见到<context:annotation-config/>这样一条配置,他的作用是式地向 Spring 容器注册 AutowiredA ...
- IIS服务中五种身份验证
转载:http://os.51cto.com/art/201005/202380.htm 作为微软最经典的Web服务之一的IIS服务有大致上五种Web身份认证方法.身份认证时保障IIS服务安全的根本, ...
- Monotouch/WCF: How to consume the wcf service without svcutil
Becuase monotouch compile to native code, so it has some limitation such as dynamic invoke is not al ...
- C#引用类型转换,到底使用is,as还是显式强转?
在C#中,当引用类型需要转换的时候,经常会用到关键字is.as以及显式强转.本篇来体验这三者的用法. 先来梳理.NET引用类型转换的"约定俗成",或者叫"惯例" ...
- Tomcat集群扩展session集中管理,Memcached-session-manager使用
研究tomcat做负载均衡的时候如何实现ha,还有就是不采用session复制的方法做集群. 想到的是将session全部存储在后端的缓存服务器中. 正好网上有这么一个工具Memcached-sess ...
- Oracle WIHT AS 用法
1.with table as 相当于建个临时表(用于一个语句中某些中间结果放在临时表空间的SQL语句),Oracle 9i 新增WITH语法,可以将查询中的子查询命名,放到SELECT语句的最前面. ...
- Derby安装,创建数据库,在Java程序中使用Derby
1,下载并安装Derby: 下载地址:http://db.apache.org/derby /derby_downloads.html,下载最新版本. 我用的是10.5.3.0. 解压缩到任意文件夹, ...
- Error while loading shared libraries: libpq.so.5: cannot open shared object file: No such file or directory
32位系统:ln -s /opt/base/3.3/lib/libpq.so.5 /usr/lib/libpq.so.5 64位系统:ln -s /opt/base/3.3/lib/libpq.so. ...
- 使用HTML5画柱状图
柱状图在很多应用中都比较常见,例如投票结果的统计分析,企业销售数据的统计分析等等. 需求分析: 一个柱状图一般包含以下几部分: 1.标题 2.横坐标(含标题) 3.竖坐标 (含标题.刻度 ...
- [PHP] ubuntu16.04配置Lamp环境(搭建linux+apache+mysql+php7环境)
reference : http://blog.csdn.net/Abyss_sliver/article/details/77621404 好久没有在Linux环境下进行开发了,比较常用的还是win ...