1、到github上下载zxing的项目,将项目中的core/scr/main/java/的com文件夹和javase/scr/main/java的com文件夹复制到新的Java项目中,将其到处为jar包备用。
————————————————————————————
zxing github链接
zxing.jar下载
2、将该jar包导入用于生成二维码的项目
3、编写代码
public static void main(String[] args) throws Exception {
// write your code here
int width = 300;
int height = 300;
String content = "http://www.baidu.com";
String format = "png";
System.setProperty("java.specification.version", "1.9");
//定义二维码参数
HashMap hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
hints.put(EncodeHintType.MARGIN, 2);
Path file = new File("D:/img.png").toPath();
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
BufferedImage image = toBufferedImage(bitMatrix);
MatrixToImageWriter.writeToPath(bitMatrix,format,file);
}
运行程序后,D盘会生成一张二维码图片