mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
给QrCodeUtil添加生成Ascii Art字符画格式二维码的功能
This commit is contained in:
parent
c9f33e5949
commit
7a233005e8
@ -108,6 +108,38 @@ public class QrCodeUtil {
|
|||||||
return toSVG(bitMatrix, qrConfig);
|
return toSVG(bitMatrix, qrConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param content 内容
|
||||||
|
* @return ASCII Art字符画形式的二维码
|
||||||
|
* @since 5.8.6
|
||||||
|
*/
|
||||||
|
public static String generateAsAsciiArt(String content) {
|
||||||
|
return generateAsAsciiArt(content, 0,0,1);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @param content 内容
|
||||||
|
* @param qrConfig 二维码配置,仅长、宽、边距配置有效
|
||||||
|
* @return ASCII Art字符画形式的二维码
|
||||||
|
* @since 5.8.6
|
||||||
|
*/
|
||||||
|
public static String generateAsAsciiArt(String content, QrConfig qrConfig) {
|
||||||
|
BitMatrix bitMatrix = encode(content, qrConfig);
|
||||||
|
return toAsciiArt(bitMatrix);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param content 内容
|
||||||
|
* @param width 宽
|
||||||
|
* @param height 长
|
||||||
|
* @return ASCII Art字符画形式的二维码
|
||||||
|
* @since 5.8.6
|
||||||
|
*/
|
||||||
|
public static String generateAsAsciiArt(String content,int width, int height,int margin) {
|
||||||
|
QrConfig qrConfig = new QrConfig(width, height).setMargin(margin);
|
||||||
|
return generateAsAsciiArt( content, qrConfig);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成PNG格式的二维码图片,以byte[]形式表示
|
* 生成PNG格式的二维码图片,以byte[]形式表示
|
||||||
*
|
*
|
||||||
@ -495,6 +527,35 @@ public class QrCodeUtil {
|
|||||||
"</svg>";
|
"</svg>";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param bitMatrix
|
||||||
|
* @return ASCII Art字符画形式的二维码
|
||||||
|
* @since 5.8.6
|
||||||
|
*/
|
||||||
|
public static String toAsciiArt(BitMatrix bitMatrix) {
|
||||||
|
int width = bitMatrix.getWidth();
|
||||||
|
int height = bitMatrix.getHeight();
|
||||||
|
StringBuilder result = new StringBuilder(height * (width + 1));
|
||||||
|
for (int i = 0; i <= height; i += 2) {
|
||||||
|
for (int j = 0; j < width; j++) {
|
||||||
|
boolean tp = bitMatrix.get(i, j);
|
||||||
|
boolean bt = i + 1 >= height || bitMatrix.get(i + 1, j);
|
||||||
|
if (tp && bt) {
|
||||||
|
result.append(' ');//'\u0020'
|
||||||
|
} else if (tp) {
|
||||||
|
result.append('▄');//'\u2584'
|
||||||
|
} else if (bt) {
|
||||||
|
result.append('▀');//'\u2580'
|
||||||
|
} else {
|
||||||
|
result.append('█');//'\u2588'
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
result.append('\n');
|
||||||
|
}
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建解码选项
|
* 创建解码选项
|
||||||
*
|
*
|
||||||
|
@ -118,4 +118,12 @@ public class QrCodeUtilTest {
|
|||||||
FileUtil.writeString(svg, "d:/test/qr.svg", StandardCharsets.UTF_8);
|
FileUtil.writeString(svg, "d:/test/qr.svg", StandardCharsets.UTF_8);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void generateAsciiArtTest() {
|
||||||
|
QrConfig qrConfig = QrConfig.create();
|
||||||
|
String asciiArt = QrCodeUtil.generateAsAsciiArt("https://hutool.cn/呱呱呱呱呱呱呱呱呱呱呱呱呱呱呱古古怪怪");
|
||||||
|
Assert.assertNotNull(asciiArt);
|
||||||
|
System.out.println(asciiArt);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user