//生成二维码的方法
public String showUserName(HttpServletRequest request, Model model,String code){
// 设置二维码大小
int width=150;
int height=150;
//设置二维码格式
String format="png";
// 二维码的内容
String contents=code;
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, 0);
try {
BitMatrix bm = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height);
Path file=new File("D:/"+code+".png").toPath();//二维码生成后的存储地址
MatrixToImageWriter.writeToPath(bm, format, file);
} catch (WriterException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
/**
* 删除白边
* */
private static BitMatrix deleteWhite(BitMatrix matrix) {
int[] rec = matrix.getEnclosingRectangle();
int resWidth = rec[2] + 1;
int resHeight = rec[3] + 1;
BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
resMatrix.clear();
for (int i = 0; i < resWidth; i++) {
for (int j = 0; j < resHeight; j++) {
if (matrix.get(i + rec[0], j + rec[1]))
resMatrix.set(i, j);
}
}
return resMatrix;
}