利用zxing解析生成的二维码的内容,源码如下:
package com.xz.erweima;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import javax.imageio.ImageIO;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
public class ReadQRCode {
public static void main(String[] args) {
MultiFormatReader reader = new MultiFormatReader();
try {
//二维码的路径
File file = new File("D:/img4.png");
BufferedImage image = ImageIO.read(file);
BinaryBitmap bitMap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
//定义二维码所需要的参数
HashMap hints = new HashMap();
//定义编码
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
Result result = reader.decode(bitMap,hints);
System.out.println("解析结果:"+result.toString());
System.out.println("二维码格式:"+result.getBarcodeFormat());
System.out.println("文本内容为:"+result.getText());
} catch (NotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
快乐学习,快乐编程!