最近在项目中使用到了由二维码图片的生成和解析。
下面我就简单是写一个小小的示例。
首先是使用的maven创建项目,所以我们先引入zxing的相关依赖。
<!-- 二维码相关的依赖 -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.2.0</version>
</dependency>
只需要引入这两个依赖就可以了。
接下来就可以测试验证了。
package com.study;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import net.sf.json.JSONException;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.Binarizer;
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.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
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;
/*
* @author jianweigang
* (1)使用zxing工具,根据一串字符生成一个包含该串字符的二维码图片。
* (2)使用zxing工具,加载一个二维码图片,解析里面包含的内容。
*/
public class MiQRCreate {
/**
* 生成图像
*
* @throws com.google.zxing.WriterException
* @throws java.io.IOException
*/
public void testEncode() throws WriterException, IOException, JSONException {
String filePath = "/home/jianweigang/qr/";
String fileName = "zxing.png";
String content = "http://staging.m.pay.xiaomi.com/test/qrpay/544165145454";
int width = 400; //图像宽度
int height = 400; //图像高度
String format = "png"; //图像类型
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.MARGIN, 0); //此处去白边没有其任何作用(得分析源代码了)
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);// 生成矩阵
//1.1去白边
int[] rec = bitMatrix.getEnclosingRectangle();
int resWidth = rec[2] + 1;
int resHeight = rec[3] + 1;
BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
resMatrix.clear();
for (int i = 0; i < resWidth; i++) {
for (int j = 0; j < resHeight; j++) {
if (bitMatrix.get(i + rec[0], j + rec[1])) {
resMatrix.set(i, j);
}
}
}
Path path = FileSystems.getDefault().getPath(filePath, fileName);
MatrixToImageWriter.writeToPath(resMatrix, format, path);// 输出图像
}
/**
* 解析图像
*/
public void testDecode() throws WriterException, IOException, JSONException{
String filePath = "/home/jianweigang/qr/zxing.png";
BufferedImage image;
try {
image = ImageIO.read(new File(filePath));
LuminanceSource source = new BufferedImageLuminanceSource(image);
Binarizer binarizer = new HybridBinarizer(source);
BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
Result result = new MultiFormatReader().decode(binaryBitmap, hints); //对图像进行解码
System.out.println("content: " + result.getText());
System.out.println("encode: " + result.getBarcodeFormat());
} catch (IOException e) {
e.printStackTrace();
} catch (NotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws JSONException, IOException, WriterException {
MiQRCreate test = new MiQRCreate();
test.testEncode();
test.testDecode();
}
}
以上代码验证正确