add qrconfig

This commit is contained in:
Looly 2022-07-22 17:30:54 +08:00
parent e7ed50a789
commit fa5763e03a
7 changed files with 54 additions and 15 deletions

View File

@ -18,6 +18,7 @@ public class BCD {
* @return BCD * @return BCD
*/ */
public static byte[] strToBcd(String asc) { public static byte[] strToBcd(String asc) {
Assert.notNull(asc, "ASCII must not be null!");
int len = asc.length(); int len = asc.length();
final int mod = len % 2; final int mod = len % 2;
if (mod != 0) { if (mod != 0) {

View File

@ -1,5 +1,7 @@
package cn.hutool.core.codec; package cn.hutool.core.codec;
import cn.hutool.core.lang.Assert;
/** /**
* 凯撒密码实现<br> * 凯撒密码实现<br>
* 算法来自<a href="https://github.com/zhaorenjie110/SymmetricEncryptionAndDecryption">https://github.com/zhaorenjie110/SymmetricEncryptionAndDecryption</a> * 算法来自<a href="https://github.com/zhaorenjie110/SymmetricEncryptionAndDecryption">https://github.com/zhaorenjie110/SymmetricEncryptionAndDecryption</a>
@ -19,6 +21,7 @@ public class Caesar {
* @return 加密后的内容 * @return 加密后的内容
*/ */
public static String encode(final String message, final int offset) { public static String encode(final String message, final int offset) {
Assert.notNull(message, "message must be not null!");
final int len = message.length(); final int len = message.length();
final char[] plain = message.toCharArray(); final char[] plain = message.toCharArray();
char c; char c;
@ -40,6 +43,7 @@ public class Caesar {
* @return 解密后的内容 * @return 解密后的内容
*/ */
public static String decode(final String cipherText, final int offset) { public static String decode(final String cipherText, final int offset) {
Assert.notNull(cipherText, "cipherText must be not null!");
final int len = cipherText.length(); final int len = cipherText.length();
final char[] plain = cipherText.toCharArray(); final char[] plain = cipherText.toCharArray();
char c; char c;

View File

@ -1,5 +1,6 @@
package cn.hutool.core.codec; package cn.hutool.core.codec;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.text.CharPool; import cn.hutool.core.text.CharPool;
import cn.hutool.core.text.StrUtil; import cn.hutool.core.text.StrUtil;
import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ArrayUtil;
@ -183,6 +184,7 @@ public class PercentCodec implements Encoder<byte[], byte[]>, Serializable {
* @return PercentCodec * @return PercentCodec
*/ */
public static Builder of(final CharSequence chars) { public static Builder of(final CharSequence chars) {
Assert.notNull(chars, "chars must not be null");
final Builder builder = of(new PercentCodec()); final Builder builder = of(new PercentCodec());
final int length = chars.length(); final int length = chars.length();
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {

View File

@ -44,6 +44,7 @@ public class PunyCode {
* @throws UtilException 计算异常 * @throws UtilException 计算异常
*/ */
public static String encode(final CharSequence input, final boolean withPrefix) throws UtilException { public static String encode(final CharSequence input, final boolean withPrefix) throws UtilException {
Assert.notNull(input, "input must not be null!");
int n = INITIAL_N; int n = INITIAL_N;
int delta = 0; int delta = 0;
int bias = INITIAL_BIAS; int bias = INITIAL_BIAS;
@ -126,6 +127,7 @@ public class PunyCode {
* @throws UtilException 计算异常 * @throws UtilException 计算异常
*/ */
public static String decode(String input) throws UtilException { public static String decode(String input) throws UtilException {
Assert.notNull(input, "input must not be null!");
input = StrUtil.removePrefixIgnoreCase(input, PUNY_CODE_PREFIX); input = StrUtil.removePrefixIgnoreCase(input, PUNY_CODE_PREFIX);
int n = INITIAL_N; int n = INITIAL_N;

View File

@ -1,10 +1,12 @@
package cn.hutool.core.codec; package cn.hutool.core.codec;
import cn.hutool.core.lang.Assert;
/** /**
* RotNrotate by N places回转N位密码是一种简易的替换式密码也是过去在古罗马开发的凯撒加密的一种变体<br> * RotNrotate by N places回转N位密码是一种简易的替换式密码也是过去在古罗马开发的凯撒加密的一种变体<br>
* 代码来自https://github.com/orclight/jencrypt * 代码来自<a href="https://github.com/orclight/jencrypt">https://github.com/orclight/jencrypt</a>
* *
* @author looly,shuzhilong * @author looly, shuzhilong
* @since 4.4.1 * @since 4.4.1
*/ */
public class Rot { public class Rot {
@ -30,11 +32,11 @@ public class Rot {
* Rot-13编码 * Rot-13编码
* *
* @param message 被编码的消息 * @param message 被编码的消息
* @param isEnocdeNumber 是否编码数字 * @param isEncodeNumber 是否编码数字
* @return 编码后的字符串 * @return 编码后的字符串
*/ */
public static String encode13(final String message, final boolean isEnocdeNumber) { public static String encode13(final String message, final boolean isEncodeNumber) {
return encode(message, 13, isEnocdeNumber); return encode(message, 13, isEncodeNumber);
} }
/** /**
@ -42,15 +44,15 @@ public class Rot {
* *
* @param message 被编码的消息 * @param message 被编码的消息
* @param offset 位移常用位移13 * @param offset 位移常用位移13
* @param isEnocdeNumber 是否编码数字 * @param isEncodeNumber 是否编码数字
* @return 编码后的字符串 * @return 编码后的字符串
*/ */
public static String encode(final String message, final int offset, final boolean isEnocdeNumber) { public static String encode(final String message, final int offset, final boolean isEncodeNumber) {
final int len = message.length(); final int len = message.length();
final char[] chars = new char[len]; final char[] chars = new char[len];
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
chars[i] = encodeChar(message.charAt(i), offset, isEnocdeNumber); chars[i] = encodeChar(message.charAt(i), offset, isEncodeNumber);
} }
return new String(chars); return new String(chars);
} }
@ -85,6 +87,7 @@ public class Rot {
* @return 解码后的字符串 * @return 解码后的字符串
*/ */
public static String decode(final String rot, final int offset, final boolean isDecodeNumber) { public static String decode(final String rot, final int offset, final boolean isDecodeNumber) {
Assert.notNull(rot, "rot must not be null");
final int len = rot.length(); final int len = rot.length();
final char[] chars = new char[len]; final char[] chars = new char[len];

View File

@ -1,10 +1,11 @@
package cn.hutool.extra.qrcode; package cn.hutool.extra.qrcode;
import cn.hutool.swing.img.ImgUtil;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil; import cn.hutool.core.util.CharsetUtil;
import cn.hutool.swing.img.ImgUtil;
import com.google.zxing.BarcodeFormat; import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType; import com.google.zxing.EncodeHintType;
import com.google.zxing.datamatrix.encoder.SymbolShapeHint;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import java.awt.Color; import java.awt.Color;
@ -44,6 +45,10 @@ public class QrConfig {
protected Image img; protected Image img;
/** 二维码中的Logo缩放的比例系数如5表示长宽最小值的1/5 */ /** 二维码中的Logo缩放的比例系数如5表示长宽最小值的1/5 */
protected int ratio = 6; protected int ratio = 6;
/**
* DATA_MATRIX的符号形状
*/
protected SymbolShapeHint shapeHint = SymbolShapeHint.FORCE_NONE;
/** /**
* 创建QrConfig * 创建QrConfig
@ -300,6 +305,17 @@ public class QrConfig {
return this; return this;
} }
/**
* 设置DATA_MATRIX的符号形状
*
* @param shapeHint DATA_MATRIX的符号形状
* @return this
*/
public QrConfig setShapeHint(SymbolShapeHint shapeHint) {
this.shapeHint = shapeHint;
return this;
}
/** /**
* 转换为Zxing的二维码配置 * 转换为Zxing的二维码配置
* *
@ -331,6 +347,7 @@ public class QrConfig {
} }
hints.put(EncodeHintType.ERROR_CORRECTION, value); hints.put(EncodeHintType.ERROR_CORRECTION, value);
hints.put(EncodeHintType.DATA_MATRIX_SHAPE, shapeHint);
} }
if (null != this.margin) { if (null != this.margin) {
hints.put(EncodeHintType.MARGIN, this.margin); hints.put(EncodeHintType.MARGIN, this.margin);

View File

@ -1,10 +1,11 @@
package cn.hutool.extra.qrcode; package cn.hutool.extra.qrcode;
import cn.hutool.core.codec.Base64; import cn.hutool.core.codec.Base64;
import cn.hutool.swing.img.ImgUtil;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Console; import cn.hutool.core.lang.Console;
import cn.hutool.swing.img.ImgUtil;
import com.google.zxing.BarcodeFormat; import com.google.zxing.BarcodeFormat;
import com.google.zxing.datamatrix.encoder.SymbolShapeHint;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Ignore; import org.junit.Ignore;
@ -75,8 +76,7 @@ public class QrCodeUtilTest {
@Test @Test
@Ignore @Ignore
public void generateAsBase64Test2() { public void generateAsBase64Test2() {
final byte[] bytes = FileUtil.readBytes( final byte[] bytes = FileUtil.readBytes(new File("d:/test/qr.png"));
new File("d:/test/qr.png"));
final String encode = Base64.encode(bytes); final String encode = Base64.encode(bytes);
final String base641 = QrCodeUtil.generateAsBase64("https://hutool.cn/", new QrConfig(400, 400), "png", encode); final String base641 = QrCodeUtil.generateAsBase64("https://hutool.cn/", new QrConfig(400, 400), "png", encode);
Assert.assertNotNull(base641); Assert.assertNotNull(base641);
@ -84,16 +84,26 @@ public class QrCodeUtilTest {
@Test @Test
@Ignore @Ignore
public void decodeTest3(){ public void decodeTest3() {
final String decode = QrCodeUtil.decode(ImgUtil.read("d:/test/qr_a.png"), false, true); final String decode = QrCodeUtil.decode(ImgUtil.read("d:/test/qr_a.png"), false, true);
Console.log(decode); Console.log(decode);
} }
@Test @Test
public void pdf417Test(){ public void pdf417Test() {
final BufferedImage image = QrCodeUtil.generate("content111", BarcodeFormat.PDF_417, QrConfig.of()); final BufferedImage image = QrCodeUtil.generate("content111", BarcodeFormat.PDF_417, QrConfig.of());
Assert.assertNotNull(image); Assert.assertNotNull(image);
} }
@Test
public void generateDataMatrixTest() {
final QrConfig qrConfig = QrConfig.of();
qrConfig.setShapeHint(SymbolShapeHint.FORCE_RECTANGLE);
final BufferedImage image = QrCodeUtil.generate("content111", BarcodeFormat.DATA_MATRIX, qrConfig);
Assert.assertNotNull(image);
final QrConfig config = QrConfig.of();
config.setShapeHint(SymbolShapeHint.FORCE_SQUARE);
final BufferedImage imageSquare = QrCodeUtil.generate("content111", BarcodeFormat.DATA_MATRIX, qrConfig);
Assert.assertNotNull(imageSquare);
}
} }