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);

View File

@@ -4,6 +4,7 @@ import cn.hutool.core.codec.Base64;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Console;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.junit.Assert;
import org.junit.Ignore;
@@ -87,4 +88,10 @@ public class QrCodeUtilTest {
final String decode = QrCodeUtil.decode(ImgUtil.read("d:/test/qr_a.png"), false, true);
Console.log(decode);
}
@Test
public void pdf417Test(){
final BufferedImage image = QrCodeUtil.generate("content111", BarcodeFormat.PDF_417, QrConfig.create());
Assert.assertNotNull(image);
}
}