我在 Java 中看到了幾個 QR 碼生成器的例子,其中大部分都包含一個名為hintMap. 這是示例之一:
public static BufferedImage getQRCode(String targetUrl, int width,
int height) {
try {
Hashtable<EncodeHintType, Object> hintMap = new Hashtable<>();
hintMap.put(EncodeHintType.ERROR_CORRECTION,
ErrorCorrectionLevel.L);
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix byteMatrix = qrCodeWriter.encode(targetUrl,
BarcodeFormat.QR_CODE, width, height, hintMap);
int CrunchifyWidth = byteMatrix.getWidth();
BufferedImage image = new BufferedImage(CrunchifyWidth,
CrunchifyWidth, BufferedImage.TYPE_INT_RGB);
image.createGraphics();
Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, CrunchifyWidth, CrunchifyWidth);
graphics.setColor(Color.BLACK);
for (int i = 0; i < CrunchifyWidth; i ) {
for (int j = 0; j < CrunchifyWidth; j ) {
if (byteMatrix.get(i, j)) {
graphics.fillRect(i, j, 1, 1);
}
}
}
return image;
} catch (WriterException e) {
e.printStackTrace();
throw new RuntimeException("Error getting QR Code");
}
}
那么,它的用途是hintMap什么,我們為什么要使用它?
uj5u.com熱心網友回復:
該JavaDoc中QRCodeWriter#encode()說:
hints- 提供給編碼器的附加引數
該hints引數被宣告為Map<EncodeHintType,?> hints,那么你還可以看看EncodeHintType的JavaDoc為不同的提示,你可以傳遞給方法。
一些引數是
CHARACTER_SET: 指定在適用的情況下使用什么字符編碼(字串型別)ERROR_CORRECTION:指定要使用的糾錯程度,例如在 QR 碼中。
所以基本上這個hints引數允許你指定如何生成二維碼,而無需創建幾十種不同的encode()方法。
我們為什么用它?當我們想為QRCodeWriter#encode()方法指定特殊引數時。如果我們不需要指定這些引數,我們就不需要它,也不使用它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/381717.html
上一篇:物體回應DTO中的統計資訊
