java版的二维码生成器
来源:互联网 发布:java批量上传文件 编辑:程序博客网 时间:2024/05/09 21:35
本例子使用google Zxing 实现。
Zxing库的主要部分支持以下几个功能:核心代码的使用、适用于J2SE客户端的版本、适用于Android客户端的版本(即BarcodeScanner)、Android的集成(通过Intent支持和BarcodeScanner的集成)等。
需要的jar
pom.xml增加如下引用:
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.0</version>
</dependency>
========================================================================
java代码部分:
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
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.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
/**
* 二维码生成器
*
* @blog http://sjsky.iteye.com
* @author Michael
*/
public class ZxingHandler {
/**
* 生成二维码
*
* @param contents
* @param width
* @param height
* @param imgPath
* @throws Exception
*/
public static void encode(String contents, int width, int height, String imgPath) throws Exception {
Map<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
// 指定纠错等级
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
// 指定编码格式
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,
BarcodeFormat.QR_CODE, width, height, hints);
MatrixToImageWriter.writeToStream(bitMatrix, "png",
new FileOutputStream(imgPath));
}
/**
* 解析二维码
*
* @param imgPath
* @return
* @throws Exception
*/
public static String decode(String imgPath) throws Exception {
BufferedImage image = null;
Result result = null;
image = ImageIO.read(new File(imgPath));
if (image == null) {
System.out.println("the decode image may be not exit.");
}
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Map<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
result = new MultiFormatReader().decode(bitmap, hints);
return result.getText();
}
public static void main(String[] args) throws Exception {
encode("http://blog.csdn.net/rishengcsdn",300,400,"D:/temp/test2.png");
System.out.println(decode("D:/temp/test2.png"));
}
}
==========================================
图片如下,用手机扫描就可以访问我的BLOG了,
- java版的二维码生成器
- google的二维码生成器
- 二维码的生成器。
- java——基于zxing的二维码生成器
- 带LOG的二维码生成器
- 基于Qt的二维码生成器
- 基于Qt的二维码生成器
- 二维码生成器
- 二维码生成器
- 二维码生成器
- JAVA使用ZXING二维码生成器实现二维码输出
- 永久免费的专业二维码生成器和二维码扫描器
- C#版二维码生成器附皮肤下载
- 中秋快乐版二维码生成器实现
- 二维码的使用(java版)
- android二维码生成器 艺术二维码
- 二维码生成器和二维码扫描器
- java的代码生成器
- java中常用的工具类(三)连接数据库的综合类,DES加密和解密,Base64,ExcelUtil工具类,
- python中如何调用shell等外部的命令
- 2.8_线段
- log4j的常用配置总结
- Matlab样本增加
- java版的二维码生成器
- Hadop_HDF分布式布置
- [LeetCode]107. Binary Tree Level Order Traversal II
- 调优之内存调优-NUMA
- 【AndroidStudio】小米 DELETE_FAILED_INTERNAL_ERROR
- H5页面仿原生列表会话左滑显示删除键--整体滑动
- spark-streaming-[4]-Window Operations
- 2.8.2_网格的绘制
- 纵论“新零售”