1.新建一个java项目,将zxing.jar包导进去,然后新建两个java文件,整体的框架如图所示:
2.CreateQRCode.java里面的代码如下所示:
3.ReadQRCode.java里面的代码如下所示:
package zxing;
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) throws NotFoundException {
try {
MultiFormatReader formatReader = new MultiFormatReader();
File file = new File("D:/code/img.png");
BufferedImage image = ImageIO.read(file);
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
new BufferedImageLuminanceSource(image)));
HashMap hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
Result result = formatReader.decode(binaryBitmap, hints);
System.out.println("解析结果:" + result.toString());
} catch (NotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
4. 要确保D盘下面有code这个文件夹,然后运行CreateQRCode代码,可以得到一张想要生成的二维码,扫扫二维码可以进入设置好的链接:
5.要确保D盘下面有code这个文件夹并且有img.png这张二维码,然后运行ReadQRCode代码,可以得到解析出来的二维码链接,要用到的jar包可以到http://download.csdn.net/download/qq_29656961/10006793去下载: