mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add qrconfig
This commit is contained in:
parent
e7ed50a789
commit
fa5763e03a
@ -18,6 +18,7 @@ public class BCD {
|
||||
* @return BCD
|
||||
*/
|
||||
public static byte[] strToBcd(String asc) {
|
||||
Assert.notNull(asc, "ASCII must not be null!");
|
||||
int len = asc.length();
|
||||
final int mod = len % 2;
|
||||
if (mod != 0) {
|
||||
|
@ -1,5 +1,7 @@
|
||||
package cn.hutool.core.codec;
|
||||
|
||||
import cn.hutool.core.lang.Assert;
|
||||
|
||||
/**
|
||||
* 凯撒密码实现<br>
|
||||
* 算法来自:<a href="https://github.com/zhaorenjie110/SymmetricEncryptionAndDecryption">https://github.com/zhaorenjie110/SymmetricEncryptionAndDecryption</a>
|
||||
@ -19,6 +21,7 @@ public class Caesar {
|
||||
* @return 加密后的内容
|
||||
*/
|
||||
public static String encode(final String message, final int offset) {
|
||||
Assert.notNull(message, "message must be not null!");
|
||||
final int len = message.length();
|
||||
final char[] plain = message.toCharArray();
|
||||
char c;
|
||||
@ -40,6 +43,7 @@ public class Caesar {
|
||||
* @return 解密后的内容
|
||||
*/
|
||||
public static String decode(final String cipherText, final int offset) {
|
||||
Assert.notNull(cipherText, "cipherText must be not null!");
|
||||
final int len = cipherText.length();
|
||||
final char[] plain = cipherText.toCharArray();
|
||||
char c;
|
||||
|
@ -1,5 +1,6 @@
|
||||
package cn.hutool.core.codec;
|
||||
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.text.CharPool;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
@ -183,6 +184,7 @@ public class PercentCodec implements Encoder<byte[], byte[]>, Serializable {
|
||||
* @return PercentCodec
|
||||
*/
|
||||
public static Builder of(final CharSequence chars) {
|
||||
Assert.notNull(chars, "chars must not be null");
|
||||
final Builder builder = of(new PercentCodec());
|
||||
final int length = chars.length();
|
||||
for (int i = 0; i < length; i++) {
|
||||
|
@ -44,6 +44,7 @@ public class PunyCode {
|
||||
* @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 delta = 0;
|
||||
int bias = INITIAL_BIAS;
|
||||
@ -126,6 +127,7 @@ public class PunyCode {
|
||||
* @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);
|
||||
|
||||
int n = INITIAL_N;
|
||||
|
@ -1,10 +1,12 @@
|
||||
package cn.hutool.core.codec;
|
||||
|
||||
import cn.hutool.core.lang.Assert;
|
||||
|
||||
/**
|
||||
* RotN(rotate 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
|
||||
*/
|
||||
public class Rot {
|
||||
@ -30,11 +32,11 @@ public class Rot {
|
||||
* Rot-13编码
|
||||
*
|
||||
* @param message 被编码的消息
|
||||
* @param isEnocdeNumber 是否编码数字
|
||||
* @param isEncodeNumber 是否编码数字
|
||||
* @return 编码后的字符串
|
||||
*/
|
||||
public static String encode13(final String message, final boolean isEnocdeNumber) {
|
||||
return encode(message, 13, isEnocdeNumber);
|
||||
public static String encode13(final String message, final boolean isEncodeNumber) {
|
||||
return encode(message, 13, isEncodeNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -42,15 +44,15 @@ public class Rot {
|
||||
*
|
||||
* @param message 被编码的消息
|
||||
* @param offset 位移,常用位移13
|
||||
* @param isEnocdeNumber 是否编码数字
|
||||
* @param isEncodeNumber 是否编码数字
|
||||
* @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 char[] chars = new char[len];
|
||||
|
||||
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);
|
||||
}
|
||||
@ -85,6 +87,7 @@ public class Rot {
|
||||
* @return 解码后的字符串
|
||||
*/
|
||||
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 char[] chars = new char[len];
|
||||
|
||||
|
@ -1,10 +1,11 @@
|
||||
package cn.hutool.extra.qrcode;
|
||||
|
||||
import cn.hutool.swing.img.ImgUtil;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.swing.img.ImgUtil;
|
||||
import com.google.zxing.BarcodeFormat;
|
||||
import com.google.zxing.EncodeHintType;
|
||||
import com.google.zxing.datamatrix.encoder.SymbolShapeHint;
|
||||
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
|
||||
|
||||
import java.awt.Color;
|
||||
@ -44,6 +45,10 @@ public class QrConfig {
|
||||
protected Image img;
|
||||
/** 二维码中的Logo缩放的比例系数,如5表示长宽最小值的1/5 */
|
||||
protected int ratio = 6;
|
||||
/**
|
||||
* DATA_MATRIX的符号形状
|
||||
*/
|
||||
protected SymbolShapeHint shapeHint = SymbolShapeHint.FORCE_NONE;
|
||||
|
||||
/**
|
||||
* 创建QrConfig
|
||||
@ -300,6 +305,17 @@ public class QrConfig {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置DATA_MATRIX的符号形状
|
||||
*
|
||||
* @param shapeHint DATA_MATRIX的符号形状
|
||||
* @return this
|
||||
*/
|
||||
public QrConfig setShapeHint(SymbolShapeHint shapeHint) {
|
||||
this.shapeHint = shapeHint;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为Zxing的二维码配置
|
||||
*
|
||||
@ -331,6 +347,7 @@ public class QrConfig {
|
||||
}
|
||||
|
||||
hints.put(EncodeHintType.ERROR_CORRECTION, value);
|
||||
hints.put(EncodeHintType.DATA_MATRIX_SHAPE, shapeHint);
|
||||
}
|
||||
if (null != this.margin) {
|
||||
hints.put(EncodeHintType.MARGIN, this.margin);
|
||||
|
@ -1,10 +1,11 @@
|
||||
package cn.hutool.extra.qrcode;
|
||||
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import cn.hutool.swing.img.ImgUtil;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.swing.img.ImgUtil;
|
||||
import com.google.zxing.BarcodeFormat;
|
||||
import com.google.zxing.datamatrix.encoder.SymbolShapeHint;
|
||||
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
@ -75,8 +76,7 @@ public class QrCodeUtilTest {
|
||||
@Test
|
||||
@Ignore
|
||||
public void generateAsBase64Test2() {
|
||||
final byte[] bytes = FileUtil.readBytes(
|
||||
new File("d:/test/qr.png"));
|
||||
final byte[] bytes = FileUtil.readBytes(new File("d:/test/qr.png"));
|
||||
final String encode = Base64.encode(bytes);
|
||||
final String base641 = QrCodeUtil.generateAsBase64("https://hutool.cn/", new QrConfig(400, 400), "png", encode);
|
||||
Assert.assertNotNull(base641);
|
||||
@ -84,16 +84,26 @@ public class QrCodeUtilTest {
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void decodeTest3(){
|
||||
public void decodeTest3() {
|
||||
final String decode = QrCodeUtil.decode(ImgUtil.read("d:/test/qr_a.png"), false, true);
|
||||
Console.log(decode);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pdf417Test(){
|
||||
public void pdf417Test() {
|
||||
final BufferedImage image = QrCodeUtil.generate("content111", BarcodeFormat.PDF_417, QrConfig.of());
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user