fix qr test

This commit is contained in:
Looly
2021-10-26 22:19:59 +08:00
parent 93a901d864
commit a77dce46cd
6 changed files with 36 additions and 9 deletions

View File

@@ -313,9 +313,10 @@ public class QrCodeUtil {
// 默认配置
config = new QrConfig();
}
BitMatrix bitMatrix;
try {
bitMatrix = multiFormatWriter.encode(content, format, config.width, config.height, config.toHints());
bitMatrix = multiFormatWriter.encode(content, format, config.width, config.height, config.toHints(format));
} catch (WriterException e) {
throw new QrCodeException(e);
}

View File

@@ -3,6 +3,7 @@ package cn.hutool.extra.qrcode;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
@@ -331,13 +332,31 @@ public class QrConfig {
* @return 配置
*/
public HashMap<EncodeHintType, Object> toHints() {
return toHints(BarcodeFormat.QR_CODE);
}
/**
* 转换为Zxing的二维码配置
*
* @param format 格式,根据格式不同,{@link #errorCorrection}的值类型有所不同
* @return 配置
*/
public HashMap<EncodeHintType, Object> toHints(BarcodeFormat format) {
// 配置
final HashMap<EncodeHintType, Object> hints = new HashMap<>();
if (null != this.charset) {
hints.put(EncodeHintType.CHARACTER_SET, charset.toString().toLowerCase());
}
if (null != this.errorCorrection) {
hints.put(EncodeHintType.ERROR_CORRECTION, this.errorCorrection);
Object value;
if(BarcodeFormat.AZTEC == format || BarcodeFormat.PDF_417 == format){
// issue#I4FE3U@Gitee
value = this.errorCorrection.getBits();
} else {
value = this.errorCorrection;
}
hints.put(EncodeHintType.ERROR_CORRECTION, value);
}
if (null != this.margin) {
hints.put(EncodeHintType.MARGIN, this.margin);