使用谷歌的开源包ZXing

maven引入如下两个包即可

<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.0.0</version>
</dependency>

  

1、工具类

package com.unicom.zxing;

import com.google.zxing.*;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Hashtable; /**
* 二维码生成和读的工具类
*
*/
public class ZXingUtil { /**
* 生成包含字符串信息的二维码图片
* @param outputStream 文件输出流路径
* @param content 二维码携带信息
* @param qrCodeSize 二维码图片大小
* @param imageFormat 二维码的格式
* @throws WriterException
* @throws IOException
*/
public static boolean createQrCode(OutputStream outputStream, String content, int qrCodeSize, String imageFormat) throws WriterException, IOException{
//设置二维码纠错级别MAP
Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); // 矫错级别
QRCodeWriter qrCodeWriter = new QRCodeWriter();
//创建比特矩阵(位矩阵)的QR码编码的字符串
BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hintMap);
// 使BufferedImage勾画QRCode (matrixWidth 是行二维码像素点)
int matrixWidth = byteMatrix.getWidth();
BufferedImage image = new BufferedImage(matrixWidth-200, matrixWidth-200, BufferedImage.TYPE_INT_RGB);
image.createGraphics();
Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, matrixWidth, matrixWidth);
// 使用比特矩阵画并保存图像
graphics.setColor(Color.BLACK);
for (int i = 0; i < matrixWidth; i++){
for (int j = 0; j < matrixWidth; j++){
if (byteMatrix.get(i, j)){
graphics.fillRect(i-100, j-100, 1, 1);
}
}
}
return ImageIO.write(image, imageFormat, outputStream);
} /**
* 读二维码并输出携带的信息
*/
public static void readQrCode(InputStream inputStream) throws IOException{
//从输入流中获取字符串信息
BufferedImage image = ImageIO.read(inputStream);
//将图像转换为二进制位图源
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
QRCodeReader reader = new QRCodeReader();
Result result = null ;
try {
result = reader.decode(bitmap);
} catch (ReaderException e) {
e.printStackTrace();
}
System.out.println(result.getText());
}
/**
* 测试代码
* @throws WriterException
*/
public static void main(String[] args) throws IOException, WriterException {
//生成二维码到E盘
createQrCode(new FileOutputStream(new File("E:\\test.jpg")),"https://www.baidu.com/",900,"JPEG");
//下面是读取二维码内容
readQrCode(new FileInputStream(new File("E:\\test.jpg")));
} }

直接运行Main方法,即可在E盘生成二维码

微信扫一扫,即可跳转到百度网页

ZXing生成二维码、读取二维码的更多相关文章

  1. 随手记一次利用开源zxing生成带嵌入logo的二维码图片

    之前就在项目里面用过zxing生成二维码,最近另一个项目同样需要用到二维码,故重新在学了学利用zxing生成二维码 接下来先做准备工作了,因为我是用vs2013上开发的,故选择了.net4.5版本的z ...

  2. ZXing 生成、读取二维码(带logo)

    前言 ZXing,一个支持在图像中解码和生成条形码(如二维码.PDF 417.EAN.UPC.Aztec.Data Matrix.Codabar)的库.ZXing(“zebra crossing”)是 ...

  3. zxing生成二维码和读取二维码

    当然,首先要导入zxing的jar包. 生成二维码代码: package com.imooc.zxing; import java.io.File; import java.nio.file.Path ...

  4. 【java】google的zxing架包生成二维码和读取二维码【可带文字和logo】

    承接RC4生成不重复字符串的需求之后,因为优惠码要方便用户使用的缘故,所以思来想去,觉得还是直接生成二维码给用户直接扫比较实用,也不用用户专门记录冗长的优惠码编号. ================= ...

  5. ZXing 生成、解析二维码图片的小示例

    概述 ZXing 是一个开源 Java 类库用于解析多种格式的 1D/2D 条形码.目标是能够对QR编码.Data Matrix.UPC的1D条形码进行解码. 其提供了多种平台下的客户端包括:J2ME ...

  6. iOS系统原生 二维码的生成、扫描和读取(高清、彩色)

    由于近期工作中遇到了个需求:需要将一些固定的字段 在多个移动端进行相互传输,所以就想到了 二维码 这个神奇的东东! 现在的大街上.连个摊煎饼的大妈 都有自己的二维码来让大家进行扫码支付.可见现在的二维 ...

  7. (转)ZXing生成二维码和带logo的二维码,模仿微信生成二维码效果

    场景:移动支付需要对二维码的生成与部署有所了解,掌握目前主流的二维码生成技术. 1 ZXing 生成二维码 首先说下,QRCode是日本人开发的,ZXing是google开发,barcode4j也是老 ...

  8. ZXing生成条形码、二维码、带logo二维码

    采用的是开源的ZXing,Maven配置如下,jar包下载地址,自己选择版本下载,顺便推荐下Maven Repository <!-- https://mvnrepository.com/art ...

  9. Java使用zxing生成解读QRcode二维码

    1.maven的pom配置jar包,如果不实用maven请手动下载jar包 <dependency> <groupId>com.google.zxing</groupId ...

随机推荐

  1. 纯数据结构Java实现(10/11)(2-3树&红黑树)

    欢迎访问我的自建博客: CH-YK Blog.

  2. Visual Studio 调试 ---- 系列文章

    调试是软件开发过程中非常重要的一个部分,它具挑战性,但是也有一定的方法和技巧. Visual Studio 调试程序有助于你观察程序的运行时行为并发现问题. 该调试器可用于所有 Visual Stud ...

  3. C#多线程编程中的锁系统

    C#多线程编程中的锁系统(二) 上章主要讲排他锁的直接使用方式.但实际当中全部都用锁又太浪费了,或者排他锁粒度太大了. 这一次我们说说升级锁和原子操作. 目录 1:volatile 2:  Inter ...

  4. cmds系统归并缓慢的处理过程 2017-2-16

      检查反应较慢的时间段,数据库整体运行情况,从awr报告总看看是否有异常   以上是两个节点的等待事件排行,其中发现cursor: pin S wait on X等待事件类型是并行,切较高,开始分析 ...

  5. ios app ipv6 审核总结

    AAAA记录(AAAA record)是用来将域名解析到IPv6地址的DNS记录.用户可以将一个域名解析到IPv6地址上,也可以将子域名解析到IPv6地址上. 1)$ dig +nocmd +nost ...

  6. Error handling in Swift does not involve stack unwinding. What does it mean?

    Stack unwinding is just the process of navigating up the stack looking for the handler. Wikipedia su ...

  7. iis webapi不间隔第一次访问超慢

    第一种尝试(正在验证是否有效): 设置网站对应的应用程序池的"闲置超时"为0.如下图所示

  8. MongoDB 查看集合与索引状态命令

    1.查看集合状态命令 MongoDB Enterprise > db.trs_action_dzwl_zm.stats() 2.查看索引状态命令 MongoDB Enterprise > ...

  9. JS的ES6的Symbol

    一.Symbol 1.什么是Symbol: Symbol是ES6新添加的原始类型(ES5已有原始数据类型:String,Number,boolean,function,undefined,object ...

  10. OpenFlow和SDN的历史和原理介绍

    OpenFlow相关的历史.新闻:http://blog.csdn.net/jincm13/article/details/7825754起源与发展[https://36kr.com/p/503598 ...