环境:
1.eclipse
2.jdk1.8
3.tomcat8
4.zxing3.2.1.jar
具体实现:
public static void main(String[] args) throws Exception {
int width = 300;
int height = 300;
String format = "png";
String content = "此处为二维码生成的内容";
@SuppressWarnings("rawtypes")
HashMap<EncodeHintType, Comparable> map = new HashMap<EncodeHintType, Comparable>();
map.put(EncodeHintType.CHARACTER_SET,"UTF-8");
map.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.M);
map.put(EncodeHintType.MARGIN,2);
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE, width, height,map);
Path file = new File("F:/code/img.png").toPath();
MatrixToImageWriter.writeToPath(bitMatrix, format, file);
} catch (WriterException e) {
e.printStackTrace();
}
}