一丶生成二维码图片
- // 生成二维码,直接写到本地
generateQRcodePic(CONTENT, QRCODE_SIZE, QRCODE_SIZE, "jpg");
public static void generateQRcodePic(String content, int width, int height, String picFormat) {
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.MARGIN, 1);
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
String fileDir = "D:\\DEV_ENV" + File.separator + "image" + File.separator + sf.format(new Date());
if (!new File(fileDir).exists()) {
new File(fileDir).mkdirs();
}
Path file = new File(fileDir + File.separator + "qrcode." + picFormat).toPath();
MatrixToImageWriter.writeToPath(bitMatrix, picFormat, file);
} catch (WriterException | IOException e) {
e.printStackTrace();
}
}