Source
Dependency
implementation group: 'com.google.zxing', name: 'javase', version: '3.5.0'
implementation group: 'com.google.zxing', name: 'core', version: '3.5.0'
util
public static String makeIdentifyCode(String uuid) throws NoSuchAlgorithmException {
String hashType = "SHA-256";java.security.MessageDigest md;
md = java.security.MessageDigest.getInstance(hashType);
byte[] hashData = md.digest((uuid+new Timestamp(System.currentTimeMillis())).getBytes(StandardCharsets.UTF_8));
StringBuilder result = new StringBuilder();
for ( byte b : hashData )
result.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
return result.toString();
}
public static byte[] makeQrImage(String hash, String name) throws WriterException {
int width = 200;
int height = 200;
Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix encode = new MultiFormatWriter().encode(hash, BarcodeFormat.QR_CODE, width, height, hints);
try {
BufferedImage qrImage = MatrixToImageWriter.toBufferedImage(encode);
int combinedHeight = height + 12;
BufferedImage combined = new BufferedImage(width, combinedHeight, BufferedImage.TYPE_INT_RGB);
Graphics g = combined.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, combinedHeight);
g.drawImage(qrImage, 0, 0, null);
g.setColor(Color.BLACK);
g.setFont(new Font("Arial", Font.PLAIN, 18));
FontMetrics fontMetrics = g.getFontMetrics();
int textWidth = fontMetrics.stringWidth(name);
int textX = (width - textWidth) / 2;
int textY = height + (g.getFont().getSize() - 15);
g.drawString(name, textX, textY);
g.dispose();
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(combined, "PNG", out);
return out.toByteArray();
}catch (Exception e){e.fillInStackTrace();}
return null;
}
private static BitMatrix emptyQR(BitMatrix bitMatrix, int regionHeight, int regionWidth) {
int width=bitMatrix.getWidth();
int height=bitMatrix.getHeight();
int left=(width-regionWidth)/2;
int top=(height-regionHeight)/2;
bitMatrix.setRegion(left,top,regionWidth,regionHeight);
for (int y = top; y <= top+regionHeight; y++) {
for (int x = left; x <= left+regionWidth; x++) {
if(bitMatrix.get(x, y)){
bitMatrix.unset(x,y);
};
}
}
return bitMatrix;
}
controller
@GetMapping("/downloadUserQr/{uid}")
public ResponseEntity<byte[]> downloadUserQr(@PathVariable String uid, HttpServletResponse res ) throws Exception {
byte[] qrImage = null;
Map<String, Object> param = new HashMap<>();
param.put("uuid", uid);
if(param.get("uuid") == null) {
return ResponseEntity.badRequest().build();
}
try {
String uuid = param.get("uuid").toString();
Map<String, Object> data = fileDao.selectQrHashFromId(uuid);
if(data == null) {
return ResponseEntity.badRequest().build();
} else if(data.get("hash") == null) {
String genQrHash = StringUtils.makeIdentifyCode(uuid);
param.put("hash", genQrHash);
fileDao.updateUserQrHash(param);
data.put("hash", genQrHash);
}
qrImage = StringUtils.makeQrImage(data.get("hash").toString(), data.get("name").toString());
} catch (Exception e) {
throw new RuntimeException(e);
}
return ResponseEntity.ok().contentType(MediaType.IMAGE_PNG).body(qrImage);
}