zxing生成二维码
关于QR code 的在Google诞生,历史及优缺点就不再介绍了,自行百度脑补吧;
1,准备材料 zxing-core-3.2.1.jar
2,创建java项目;
3,添加jar包,给项目添加jar包,右击项目build path - config bulid path - 选择Libraries - 选择 Add JARs.. - 找到jiar包添加 - select all选中 - 应用到项目中
4,创建实现类,定义一些基本的参数,如,宽,高,二维码的容错等级,编码格式,二维码格式QR code 版本
5,借助MatrixToImageWriter 这个类来做输出;
// 生成二维码
public class GreatQrCode {
public static void main(String[] args) {
// 定义一些基本参数
int width = 300;
int height = 300;
String format = "png";
String content = "http://www.cnblogs.com/wshuo/";
// 定义二维码参数 使用map 和MatrixToImageWriter
HashMap hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
// 设置二维码的容错等级
hints.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.M);
hints.put(EncodeHintType.MARGIN, 2);
// 定义二维码的格式 BarcodeFormat 来定义二维码 ; 需要捕捉异常
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
File file = new File("D:/img.png");
MatrixToImageWriter.writeToFile(bitMatrix, format, file);
System.out.println("dangkl");
} catch (Exception e) {
e.printStackTrace();
}
}
}
6,使用扫描下载功能,时要注意,微信(屏蔽了第三方的下载软件) 和 浏览器下载区别
浙公网安备 33010602011771号