diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/ansi/AnsiColorWrapper.java b/hutool-core/src/main/java/cn/hutool/core/lang/ansi/AnsiColorWrapper.java new file mode 100644 index 000000000..b78487865 --- /dev/null +++ b/hutool-core/src/main/java/cn/hutool/core/lang/ansi/AnsiColorWrapper.java @@ -0,0 +1,79 @@ +package cn.hutool.core.lang.ansi; + +import cn.hutool.core.lang.Assert; +import cn.hutool.core.util.StrUtil; + +import java.util.Objects; + +/** + * ANSI 颜色包装类 + * + * @author TomXin + * @since 5.8.6 + */ +public class AnsiColorWrapper { + + private final int code; + + private final AnsiColors.BitDepth bitDepth; + + /** + * 创建指定位深度的 {@link AnsiColorWrapper} 实例 + * @param code 颜色编码,位深度为4bit时,code取值范围[30~37],[90~97]。位深度为8bit时,code取值范围[0~255] + * @param bitDepth 位深度 + */ + public AnsiColorWrapper(int code, AnsiColors.BitDepth bitDepth) { + if (bitDepth==AnsiColors.BitDepth.FOUR){ + Assert.isTrue((30<=code&&code<=37)||(90<=code&&code<=97),"The value of 4 bit color only supported [30~37],[90~97]."); + } + Assert.isTrue((0<=code&&code<=255),"The value of 8 bit color only supported [0~255]."); + this.code = code; + this.bitDepth = bitDepth; + } + + /** + * 转换为 {@link AnsiElement} 实例 + * + * @param foreOrBack 区分前景还是背景 + * @return {@link AnsiElement} 实例 + */ + public AnsiElement toAnsiElement(ForeOrBack foreOrBack){ + if (bitDepth== AnsiColors.BitDepth.FOUR){ + if (foreOrBack == ForeOrBack.FORE){ + for (AnsiColor item : AnsiColor.values()) { + if (StrUtil.equals(item.toString(), StrUtil.toString(this.code))) { + return item; + } + } + throw new IllegalArgumentException(StrUtil.format("No matched AnsiColor instance,code={}",this.code)); + } + for (AnsiBackground item : AnsiBackground.values()) { + if (StrUtil.equals(item.toString(), StrUtil.toString(this.code+10))) { + return item; + } + } + throw new IllegalArgumentException(StrUtil.format("No matched AnsiBackground instance,code={}",this.code)); + } + if (foreOrBack == ForeOrBack.FORE){ + return Ansi8BitColor.foreground(this.code); + } + return Ansi8BitColor.background(this.code); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + AnsiColorWrapper that = (AnsiColorWrapper) o; + return this.code == that.code && this.bitDepth == that.bitDepth; + } + + @Override + public int hashCode() { + return Objects.hash(this.code, this.bitDepth); + } +} diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/ansi/AnsiColors.java b/hutool-core/src/main/java/cn/hutool/core/lang/ansi/AnsiColors.java new file mode 100644 index 000000000..f0aa749b5 --- /dev/null +++ b/hutool-core/src/main/java/cn/hutool/core/lang/ansi/AnsiColors.java @@ -0,0 +1,282 @@ +package cn.hutool.core.lang.ansi; + +import cn.hutool.core.lang.Assert; + +import java.awt.Color; +import java.awt.color.ColorSpace; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; + +/** + * + * 在 {@link Color AWT Colors} 的上下文中使用 {@link AnsiColor} 的实用程序 + *

来自Spring Boot

+ * + * @author Craig Burke,Ruben Dijkstra,Phillip Webb,Michael Simons,Tom Xin + * @since 5.8.6 + */ +public final class AnsiColors { + + private static final Map ANSI_COLOR_MAP; + + /** + * @see AnsiColor#BLACK + */ + private static final int CODE_OF_4_BIT_ANSI_COLOR_BLACK = 30; + + /** + * @see AnsiColor#RED + */ + private static final int CODE_OF_4_BIT_ANSI_COLOR_RED = 31; + + /** + * @see AnsiColor#GREEN + */ + private static final int CODE_OF_4_BIT_ANSI_COLOR_GREEN = 32; + + /** + * @see AnsiColor#YELLOW + */ + private static final int CODE_OF_4_BIT_ANSI_COLOR_YELLOW = 33; + + /** + * @see AnsiColor#BLUE + */ + private static final int CODE_OF_4_BIT_ANSI_COLOR_BLUE = 34; + + /** + * @see AnsiColor#MAGENTA + */ + private static final int CODE_OF_4_BIT_ANSI_COLOR_MAGENTA = 35; + + /** + * @see AnsiColor#CYAN + */ + private static final int CODE_OF_4_BIT_ANSI_COLOR_CYAN = 36; + + /** + * @see AnsiColor#WHITE + */ + private static final int CODE_OF_4_BIT_ANSI_COLOR_WHITE = 37; + + /** + * @see AnsiColor#BRIGHT_BLACK + */ + private static final int CODE_OF_4_BIT_ANSI_COLOR_BRIGHT_BLACK = 90; + + /** + * @see AnsiColor#BRIGHT_RED + */ + private static final int CODE_OF_4_BIT_ANSI_COLOR_BRIGHT_RED = 91; + + /** + * @see AnsiColor#BRIGHT_GREEN + */ + private static final int CODE_OF_4_BIT_ANSI_COLOR_BRIGHT_GREEN = 92; + + /** + * @see AnsiColor#BRIGHT_YELLOW + */ + private static final int CODE_OF_4_BIT_ANSI_COLOR_BRIGHT_YELLOW = 93; + + /** + * @see AnsiColor#BRIGHT_BLUE + */ + private static final int CODE_OF_4_BIT_ANSI_COLOR_BRIGHT_BLUE = 94; + + /** + * @see AnsiColor#BRIGHT_MAGENTA + */ + private static final int CODE_OF_4_BIT_ANSI_COLOR_BRIGHT_MAGENTA = 95; + + /** + * @see AnsiColor#BRIGHT_CYAN + */ + private static final int CODE_OF_4_BIT_ANSI_COLOR_BRIGHT_CYAN = 96; + + /** + * @see AnsiColor#BRIGHT_WHITE + */ + private static final int CODE_OF_4_BIT_ANSI_COLOR_BRIGHT_WHITE = 97; + + static { + Map colorMap = new LinkedHashMap<>(16); + colorMap.put(new AnsiColorWrapper(CODE_OF_4_BIT_ANSI_COLOR_BLACK, BitDepth.FOUR), new LabColor(0x000000)); + colorMap.put(new AnsiColorWrapper(CODE_OF_4_BIT_ANSI_COLOR_RED, BitDepth.FOUR), new LabColor(0xAA0000)); + colorMap.put(new AnsiColorWrapper(CODE_OF_4_BIT_ANSI_COLOR_GREEN, BitDepth.FOUR), new LabColor(0x00AA00)); + colorMap.put(new AnsiColorWrapper(CODE_OF_4_BIT_ANSI_COLOR_YELLOW, BitDepth.FOUR), new LabColor(0xAA5500)); + colorMap.put(new AnsiColorWrapper(CODE_OF_4_BIT_ANSI_COLOR_BLUE, BitDepth.FOUR), new LabColor(0x0000AA)); + colorMap.put(new AnsiColorWrapper(CODE_OF_4_BIT_ANSI_COLOR_MAGENTA, BitDepth.FOUR), new LabColor(0xAA00AA)); + colorMap.put(new AnsiColorWrapper(CODE_OF_4_BIT_ANSI_COLOR_CYAN, BitDepth.FOUR), new LabColor(0x00AAAA)); + colorMap.put(new AnsiColorWrapper(CODE_OF_4_BIT_ANSI_COLOR_WHITE, BitDepth.FOUR), new LabColor(0xAAAAAA)); + colorMap.put(new AnsiColorWrapper(CODE_OF_4_BIT_ANSI_COLOR_BRIGHT_BLACK, BitDepth.FOUR), new LabColor(0x555555)); + colorMap.put(new AnsiColorWrapper(CODE_OF_4_BIT_ANSI_COLOR_BRIGHT_RED, BitDepth.FOUR), new LabColor(0xFF5555)); + colorMap.put(new AnsiColorWrapper(CODE_OF_4_BIT_ANSI_COLOR_BRIGHT_GREEN, BitDepth.FOUR), new LabColor(0x55FF00)); + colorMap.put(new AnsiColorWrapper(CODE_OF_4_BIT_ANSI_COLOR_BRIGHT_YELLOW, BitDepth.FOUR), new LabColor(0xFFFF55)); + colorMap.put(new AnsiColorWrapper(CODE_OF_4_BIT_ANSI_COLOR_BRIGHT_BLUE, BitDepth.FOUR), new LabColor(0x5555FF)); + colorMap.put(new AnsiColorWrapper(CODE_OF_4_BIT_ANSI_COLOR_BRIGHT_MAGENTA, BitDepth.FOUR), new LabColor(0xFF55FF)); + colorMap.put(new AnsiColorWrapper(CODE_OF_4_BIT_ANSI_COLOR_BRIGHT_CYAN, BitDepth.FOUR), new LabColor(0x55FFFF)); + colorMap.put(new AnsiColorWrapper(CODE_OF_4_BIT_ANSI_COLOR_BRIGHT_WHITE, BitDepth.FOUR), new LabColor(0xFFFFFF)); + ANSI_COLOR_MAP = Collections.unmodifiableMap(colorMap); + } + + private static final int[] ANSI_8BIT_COLOR_CODE_LOOKUP = new int[] { 0x000000, 0x800000, 0x008000, 0x808000, + 0x000080, 0x800080, 0x008080, 0xc0c0c0, 0x808080, 0xff0000, 0x00ff00, 0xffff00, 0x0000ff, 0xff00ff, + 0x00ffff, 0xffffff, 0x000000, 0x00005f, 0x000087, 0x0000af, 0x0000d7, 0x0000ff, 0x005f00, 0x005f5f, + 0x005f87, 0x005faf, 0x005fd7, 0x005fff, 0x008700, 0x00875f, 0x008787, 0x0087af, 0x0087d7, 0x0087ff, + 0x00af00, 0x00af5f, 0x00af87, 0x00afaf, 0x00afd7, 0x00afff, 0x00d700, 0x00d75f, 0x00d787, 0x00d7af, + 0x00d7d7, 0x00d7ff, 0x00ff00, 0x00ff5f, 0x00ff87, 0x00ffaf, 0x00ffd7, 0x00ffff, 0x5f0000, 0x5f005f, + 0x5f0087, 0x5f00af, 0x5f00d7, 0x5f00ff, 0x5f5f00, 0x5f5f5f, 0x5f5f87, 0x5f5faf, 0x5f5fd7, 0x5f5fff, + 0x5f8700, 0x5f875f, 0x5f8787, 0x5f87af, 0x5f87d7, 0x5f87ff, 0x5faf00, 0x5faf5f, 0x5faf87, 0x5fafaf, + 0x5fafd7, 0x5fafff, 0x5fd700, 0x5fd75f, 0x5fd787, 0x5fd7af, 0x5fd7d7, 0x5fd7ff, 0x5fff00, 0x5fff5f, + 0x5fff87, 0x5fffaf, 0x5fffd7, 0x5fffff, 0x870000, 0x87005f, 0x870087, 0x8700af, 0x8700d7, 0x8700ff, + 0x875f00, 0x875f5f, 0x875f87, 0x875faf, 0x875fd7, 0x875fff, 0x878700, 0x87875f, 0x878787, 0x8787af, + 0x8787d7, 0x8787ff, 0x87af00, 0x87af5f, 0x87af87, 0x87afaf, 0x87afd7, 0x87afff, 0x87d700, 0x87d75f, + 0x87d787, 0x87d7af, 0x87d7d7, 0x87d7ff, 0x87ff00, 0x87ff5f, 0x87ff87, 0x87ffaf, 0x87ffd7, 0x87ffff, + 0xaf0000, 0xaf005f, 0xaf0087, 0xaf00af, 0xaf00d7, 0xaf00ff, 0xaf5f00, 0xaf5f5f, 0xaf5f87, 0xaf5faf, + 0xaf5fd7, 0xaf5fff, 0xaf8700, 0xaf875f, 0xaf8787, 0xaf87af, 0xaf87d7, 0xaf87ff, 0xafaf00, 0xafaf5f, + 0xafaf87, 0xafafaf, 0xafafd7, 0xafafff, 0xafd700, 0xafd75f, 0xafd787, 0xafd7af, 0xafd7d7, 0xafd7ff, + 0xafff00, 0xafff5f, 0xafff87, 0xafffaf, 0xafffd7, 0xafffff, 0xd70000, 0xd7005f, 0xd70087, 0xd700af, + 0xd700d7, 0xd700ff, 0xd75f00, 0xd75f5f, 0xd75f87, 0xd75faf, 0xd75fd7, 0xd75fff, 0xd78700, 0xd7875f, + 0xd78787, 0xd787af, 0xd787d7, 0xd787ff, 0xd7af00, 0xd7af5f, 0xd7af87, 0xd7afaf, 0xd7afd7, 0xd7afff, + 0xd7d700, 0xd7d75f, 0xd7d787, 0xd7d7af, 0xd7d7d7, 0xd7d7ff, 0xd7ff00, 0xd7ff5f, 0xd7ff87, 0xd7ffaf, + 0xd7ffd7, 0xd7ffff, 0xff0000, 0xff005f, 0xff0087, 0xff00af, 0xff00d7, 0xff00ff, 0xff5f00, 0xff5f5f, + 0xff5f87, 0xff5faf, 0xff5fd7, 0xff5fff, 0xff8700, 0xff875f, 0xff8787, 0xff87af, 0xff87d7, 0xff87ff, + 0xffaf00, 0xffaf5f, 0xffaf87, 0xffafaf, 0xffafd7, 0xffafff, 0xffd700, 0xffd75f, 0xffd787, 0xffd7af, + 0xffd7d7, 0xffd7ff, 0xffff00, 0xffff5f, 0xffff87, 0xffffaf, 0xffffd7, 0xffffff, 0x080808, 0x121212, + 0x1c1c1c, 0x262626, 0x303030, 0x3a3a3a, 0x444444, 0x4e4e4e, 0x585858, 0x626262, 0x6c6c6c, 0x767676, + 0x808080, 0x8a8a8a, 0x949494, 0x9e9e9e, 0xa8a8a8, 0xb2b2b2, 0xbcbcbc, 0xc6c6c6, 0xd0d0d0, 0xdadada, + 0xe4e4e4, 0xeeeeee }; + + private final Map lookup; + + /** + * 创建具有指定位深度的新 {@link AnsiColors} 实例。 + * @param bitDepth 所需的位深度 + */ + public AnsiColors(BitDepth bitDepth) { + this.lookup = getLookup(bitDepth); + } + + private Map getLookup(BitDepth bitDepth) { + if (bitDepth == BitDepth.EIGHT) { + Map lookup = new LinkedHashMap<>(256); + for (int i = 0; i < ANSI_8BIT_COLOR_CODE_LOOKUP.length; i++) { + lookup.put(new AnsiColorWrapper(i,BitDepth.EIGHT), new LabColor(ANSI_8BIT_COLOR_CODE_LOOKUP[i])); + } + return Collections.unmodifiableMap(lookup); + } + return ANSI_COLOR_MAP; + } + + /** + * 找到最接近给定 AWT {@link Color} 的 {@link AnsiColorWrapper ANSI 颜色包装} 实例。 + * @param color AWT 颜色 + * @return 最接近指定 ANSI 颜色的 {@link AnsiColorWrapper ANSI 颜色包装} 实例 + */ + public AnsiColorWrapper findClosest(Color color) { + return findClosest(new LabColor(color)); + } + + private AnsiColorWrapper findClosest(LabColor color) { + AnsiColorWrapper closest = null; + double closestDistance = Float.MAX_VALUE; + for (Map.Entry entry : this.lookup.entrySet()) { + double candidateDistance = color.getDistance(entry.getValue()); + if (closest == null || candidateDistance < closestDistance) { + closestDistance = candidateDistance; + closest = entry.getKey(); + } + } + return closest; + } + + /** + * 表示以 LAB 形式存储的颜色。 + */ + private static final class LabColor { + + private static final ColorSpace XYZ_COLOR_SPACE = ColorSpace.getInstance(ColorSpace.CS_CIEXYZ); + + private final double l; + + private final double a; + + private final double b; + + LabColor(Integer rgb) { + this((rgb != null) ? new Color(rgb) : null); + } + + LabColor(Color color) { + Assert.notNull(color, "Color must not be null"); + float[] lab = fromXyz(color.getColorComponents(XYZ_COLOR_SPACE, null)); + this.l = lab[0]; + this.a = lab[1]; + this.b = lab[2]; + } + + private float[] fromXyz(float[] xyz) { + return fromXyz(xyz[0], xyz[1], xyz[2]); + } + + private float[] fromXyz(float x, float y, float z) { + double l = (f(y) - 16.0) * 116.0; + double a = (f(x) - f(y)) * 500.0; + double b = (f(y) - f(z)) * 200.0; + return new float[] { (float) l, (float) a, (float) b }; + } + + private double f(double t) { + return (t > (216.0 / 24389.0)) ? Math.cbrt(t) : (1.0 / 3.0) * Math.pow(29.0 / 6.0, 2) * t + (4.0 / 29.0); + } + + // See https://en.wikipedia.org/wiki/Color_difference#CIE94 + double getDistance(LabColor other) { + double c1 = Math.sqrt(this.a * this.a + this.b * this.b); + double deltaC = c1 - Math.sqrt(other.a * other.a + other.b * other.b); + double deltaA = this.a - other.a; + double deltaB = this.b - other.b; + double deltaH = Math.sqrt(Math.max(0.0, deltaA * deltaA + deltaB * deltaB - deltaC * deltaC)); + return Math.sqrt(Math.max(0.0, Math.pow((this.l - other.l) / (1.0), 2) + + Math.pow(deltaC / (1 + 0.045 * c1), 2) + Math.pow(deltaH / (1 + 0.015 * c1), 2.0))); + } + + } + + /** + * 此类支持的位深度。 + */ + public enum BitDepth { + + /** + * 4位 (16色). + * @see AnsiColor + * @see AnsiBackground + */ + FOUR(4), + + /** + * 8位 (256色). + * @see Ansi8BitColor + */ + EIGHT(8); + + private final int bits; + + BitDepth(int bits) { + this.bits = bits; + } + + public static BitDepth of(int bits) { + for (BitDepth candidate : values()) { + if (candidate.bits == bits) { + return candidate; + } + } + throw new IllegalArgumentException("Unsupported ANSI bit depth '" + bits + "'"); + } + + } +} diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/ansi/ForeOrBack.java b/hutool-core/src/main/java/cn/hutool/core/lang/ansi/ForeOrBack.java new file mode 100644 index 000000000..518782bbf --- /dev/null +++ b/hutool-core/src/main/java/cn/hutool/core/lang/ansi/ForeOrBack.java @@ -0,0 +1,16 @@ +package cn.hutool.core.lang.ansi; + +/** + * 区分前景还是背景 + */ +public enum ForeOrBack{ + + /** + * 前景 + */ + FORE, + /** + * 背景 + */ + BACK, +} diff --git a/hutool-core/src/test/java/cn/hutool/core/lang/ansi/AnsiEncoderTest.java b/hutool-core/src/test/java/cn/hutool/core/lang/ansi/AnsiEncoderTest.java index 710c5c0f0..b2eaa50cf 100755 --- a/hutool-core/src/test/java/cn/hutool/core/lang/ansi/AnsiEncoderTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/lang/ansi/AnsiEncoderTest.java @@ -1,8 +1,13 @@ package cn.hutool.core.lang.ansi; +import cn.hutool.core.lang.Console; +import cn.hutool.core.util.StrUtil; import org.junit.Assert; +import org.junit.Ignore; import org.junit.Test; +import java.awt.*; + public class AnsiEncoderTest { @Test @@ -10,4 +15,55 @@ public class AnsiEncoderTest { final String encode = AnsiEncoder.encode(AnsiColor.GREEN, "Hutool test"); Assert.assertEquals("\u001B[32mHutool test\u001B[0;39m", encode); } + + @Test + @Ignore + public void colorfulEncodeTest(){ + String text = "Hutool▀████▀"; + final AnsiColors ansiColors = new AnsiColors(AnsiColors.BitDepth.EIGHT); + Color[] colorArray = new Color[]{ + Color.BLACK, Color.BLUE, + Color.CYAN, Color.DARK_GRAY, + Color.GRAY, Color.GREEN, + Color.LIGHT_GRAY, Color.MAGENTA, + Color.ORANGE, Color.PINK, + Color.RED, Color.WHITE, + Color.YELLOW + }; + for (int i = 0; i < colorArray.length; i++) { + Color foreColor = colorArray[i]; + AnsiElement foreElement = ansiColors.findClosest(foreColor).toAnsiElement(ForeOrBack.FORE); + Color backColor = new Color(255 - foreColor.getRed(), 255 - foreColor.getGreen(), 255 - foreColor.getBlue()); + AnsiElement backElement = ansiColors.findClosest(backColor).toAnsiElement(ForeOrBack.BACK); + String encode = AnsiEncoder.encode(foreElement, backElement, text); + //Console.print( i%2==1?encode+"\n":encode); + } + } + + @Test + public void colorMappingTest(){ + String text4 = "RGB:({},{},{})--4bit "; + String text8 = "RGB:({},{},{})--8bit "; + final AnsiColors ansiColors4Bit = new AnsiColors(AnsiColors.BitDepth.FOUR); + final AnsiColors ansiColors8Bit = new AnsiColors(AnsiColors.BitDepth.EIGHT); + int count = 0; + int from = 100000; + int until = 120000; + for (int r = 0; r < 256; r++) { + if (count>until)break; + for (int g = 0; g < 256; g++) { + if (count>until)break; + for (int b = 0; b < 256; b++) { + count++; + if (countuntil)break; + AnsiElement backElement4bit = ansiColors4Bit.findClosest(new Color(r,g,b)).toAnsiElement(ForeOrBack.BACK); + AnsiElement backElement8bit = ansiColors8Bit.findClosest(new Color(r,g,b)).toAnsiElement(ForeOrBack.BACK); + String encode4 = AnsiEncoder.encode( backElement4bit,text4); + String encode8 = AnsiEncoder.encode( backElement8bit,text8); + //Console.log(StrUtil.format(encode4,r,g,b)+StrUtil.format(encode8,r,g,b)); + } + } + } + } } diff --git a/hutool-extra/src/main/java/cn/hutool/extra/qrcode/QrCodeUtil.java b/hutool-extra/src/main/java/cn/hutool/extra/qrcode/QrCodeUtil.java index 48820596a..2a4d3171a 100755 --- a/hutool-extra/src/main/java/cn/hutool/extra/qrcode/QrCodeUtil.java +++ b/hutool-extra/src/main/java/cn/hutool/extra/qrcode/QrCodeUtil.java @@ -5,11 +5,8 @@ import cn.hutool.core.img.Img; import cn.hutool.core.img.ImgUtil; import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.IoUtil; -import cn.hutool.core.lang.ansi.Ansi8BitColor; -import cn.hutool.core.lang.ansi.AnsiElement; -import cn.hutool.core.lang.ansi.AnsiEncoder; +import cn.hutool.core.lang.ansi.*; import cn.hutool.core.util.CharsetUtil; -import cn.hutool.core.util.NumberUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.URLUtil; import com.google.zxing.*; @@ -17,7 +14,9 @@ import com.google.zxing.common.BitMatrix; import com.google.zxing.common.GlobalHistogramBinarizer; import com.google.zxing.common.HybridBinarizer; -import java.awt.*; +import java.awt.Color; +import java.awt.Image; +import java.awt.Rectangle; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; @@ -41,12 +40,13 @@ public class QrCodeUtil { public static final String QR_TYPE_SVG = "svg";// SVG矢量图格式 public static final String QR_TYPE_TXT = "txt";// Ascii Art字符画文本 + private static final AnsiColors ansiColors= new AnsiColors(AnsiColors.BitDepth.EIGHT); /** * 生成代 logo 图片的 Base64 编码格式的二维码,以 String 形式表示 * * @param content 内容 - * @param qrConfig 二维码配置,包括长、宽、边距、颜色等 + * @param qrConfig 二维码配置,包括宽度、高度、边距、颜色等 * @param targetType 类型(图片扩展名),见{@link #QR_TYPE_SVG}、 {@link #QR_TYPE_TXT}、{@link ImgUtil} * @param logoBase64 logo 图片的 base64 编码 * @return 图片 Base64 编码字符串 @@ -59,7 +59,7 @@ public class QrCodeUtil { * 生成代 logo 图片的 Base64 编码格式的二维码,以 String 形式表示 * * @param content 内容 - * @param qrConfig 二维码配置,包括长、宽、边距、颜色等 + * @param qrConfig 二维码配置,包括宽度、高度、边距、颜色等 * @param targetType 类型(图片扩展名),见{@link #QR_TYPE_SVG}、 {@link #QR_TYPE_TXT}、{@link ImgUtil} * @param logo logo 图片的byte[] * @return 图片 Base64 编码字符串 @@ -72,7 +72,7 @@ public class QrCodeUtil { * 生成代 logo 图片的 Base64 编码格式的二维码,以 String 形式表示 * * @param content 内容 - * @param qrConfig 二维码配置,包括长、宽、边距、颜色等 + * @param qrConfig 二维码配置,包括宽度、高度、边距、颜色等 * @param targetType 类型(图片扩展名),见{@link #QR_TYPE_SVG}、 {@link #QR_TYPE_TXT}、{@link ImgUtil} * @param logo logo 图片的byte[] * @return 图片 Base64 编码字符串 @@ -90,7 +90,7 @@ public class QrCodeUtil { *

* * @param content 内容 - * @param qrConfig 二维码配置,包括长、宽、边距、颜色等 + * @param qrConfig 二维码配置,包括宽度、高度、边距、颜色等 * @param targetType 类型(图片扩展名),见{@link #QR_TYPE_SVG}、 {@link #QR_TYPE_TXT}、{@link ImgUtil} * @return 图片 Base64 编码字符串 */ @@ -125,7 +125,7 @@ public class QrCodeUtil { /** * @param content 内容 - * @param qrConfig 二维码配置,包括长、宽、边距、颜色等 + * @param qrConfig 二维码配置,包括宽度、高度、边距、颜色等 * @return SVG矢量图(字符串) * @since 5.8.6 */ @@ -149,7 +149,7 @@ public class QrCodeUtil { * 生成ASCII Art字符画形式的二维码 * * @param content 内容 - * @param qrConfig 二维码配置,仅长、宽、边距配置有效 + * @param qrConfig 二维码配置,仅宽度、高度、边距配置有效 * @return ASCII Art字符画形式的二维码 * @since 5.8.6 */ @@ -160,8 +160,8 @@ public class QrCodeUtil { /** * @param content 内容 - * @param width 宽 - * @param height 长 + * @param width 宽度(单位:字符▄的大小) + * @param height 高度(单位:字符▄的大小) * @return ASCII Art字符画形式的二维码 * @since 5.8.6 */ @@ -175,8 +175,8 @@ public class QrCodeUtil { * 生成PNG格式的二维码图片,以byte[]形式表示 * * @param content 内容 - * @param width 宽度 - * @param height 高度 + * @param width 宽度(单位:像素) + * @param height 高度(单位:像素) * @return 图片的byte[] * @since 4.0.10 */ @@ -190,7 +190,7 @@ public class QrCodeUtil { * 生成PNG格式的二维码图片,以byte[]形式表示 * * @param content 内容 - * @param config 二维码配置,包括长、宽、边距、颜色等 + * @param config 二维码配置,包括宽度、高度、边距、颜色等 * @return 图片的byte[] * @since 4.1.2 */ @@ -204,8 +204,8 @@ public class QrCodeUtil { * 生成二维码到文件,二维码图片格式取决于文件的扩展名 * * @param content 文本内容 - * @param width 宽度 - * @param height 高度 + * @param width 宽度(单位:类型为一般图片或SVG时,单位是像素,类型为 Ascii Art 字符画时,单位是字符▄或▀的大小) + * @param height 高度(单位:类型为一般图片或SVG时,单位是像素,类型为 Ascii Art 字符画时,单位是字符▄或▀的大小) * @param targetFile 目标文件,扩展名决定输出格式 * @return 目标文件 */ @@ -233,7 +233,7 @@ public class QrCodeUtil { * 生成二维码到文件,二维码图片格式取决于文件的扩展名 * * @param content 文本内容 - * @param config 二维码配置,包括长、宽、边距、颜色等 + * @param config 二维码配置,包括宽度、高度、边距、颜色等 * @param targetFile 目标文件,扩展名决定输出格式 * @return 目标文件 * @since 4.1.2 @@ -261,8 +261,8 @@ public class QrCodeUtil { * 生成二维码到输出流 * * @param content 文本内容 - * @param width 宽度 - * @param height 高度 + * @param width 宽度(单位:类型为一般图片或SVG时,单位是像素,类型为 Ascii Art 字符画时,单位是字符▄或▀的大小) + * @param height 高度(单位:类型为一般图片或SVG时,单位是像素,类型为 Ascii Art 字符画时,单位是字符▄或▀的大小) * @param targetType 类型(图片扩展名),见{@link #QR_TYPE_SVG}、 {@link #QR_TYPE_TXT}、{@link ImgUtil} * @param out 目标流 */ @@ -287,7 +287,7 @@ public class QrCodeUtil { * 生成二维码到输出流 * * @param content 文本内容 - * @param config 二维码配置,包括长、宽、边距、颜色等 + * @param config 二维码配置,包括宽度、高度、边距、颜色等 * @param targetType 类型(图片扩展名),见{@link #QR_TYPE_SVG}、 {@link #QR_TYPE_TXT}、{@link ImgUtil} * @param out 目标流 * @since 4.1.2 @@ -313,8 +313,8 @@ public class QrCodeUtil { * 生成二维码图片 * * @param content 文本内容 - * @param width 宽度 - * @param height 高度 + * @param width 宽度(单位:类型为一般图片或SVG时,单位是像素,类型为 Ascii Art 字符画时,单位是字符▄或▀的大小) + * @param height 高度(单位:类型为一般图片或SVG时,单位是像素,类型为 Ascii Art 字符画时,单位是字符▄或▀的大小) * @return 二维码图片(黑白) */ public static BufferedImage generate(String content, int width, int height) { @@ -326,8 +326,8 @@ public class QrCodeUtil { * * @param content 文本内容 * @param format 格式,可选二维码或者条形码 - * @param width 宽度 - * @param height 高度 + * @param width 宽度(单位:像素) + * @param height 高度(单位:像素) * @return 二维码图片(黑白) */ public static BufferedImage generate(String content, BarcodeFormat format, int width, int height) { @@ -338,7 +338,7 @@ public class QrCodeUtil { * 生成二维码图片 * * @param content 文本内容 - * @param config 二维码配置,包括长、宽、边距、颜色等 + * @param config 二维码配置,包括宽度、高度、边距、颜色等 * @return 二维码图片(黑白) * @since 4.1.2 */ @@ -352,7 +352,7 @@ public class QrCodeUtil { * * @param content 文本内容 * @param format 格式,可选二维码、条形码等 - * @param config 二维码配置,包括长、宽、边距、颜色等 + * @param config 二维码配置,包括宽度、高度、边距、颜色等 * @return 二维码图片(黑白) * @since 4.1.14 */ @@ -390,8 +390,8 @@ public class QrCodeUtil { * 将文本内容编码为二维码 * * @param content 文本内容 - * @param width 宽度 - * @param height 高度 + * @param width 宽度(单位:类型为一般图片或SVG时,单位是像素,类型为 Ascii Art 字符画时,单位是字符▄或▀的大小) + * @param height 高度(单位:类型为一般图片或SVG时,单位是像素,类型为 Ascii Art 字符画时,单位是字符▄或▀的大小) * @return {@link BitMatrix} */ public static BitMatrix encode(String content, int width, int height) { @@ -402,7 +402,7 @@ public class QrCodeUtil { * 将文本内容编码为二维码 * * @param content 文本内容 - * @param config 二维码配置,包括长、宽、边距、颜色等 + * @param config 二维码配置,包括宽度、高度、边距、颜色等 * @return {@link BitMatrix} * @since 4.1.2 */ @@ -415,8 +415,8 @@ public class QrCodeUtil { * * @param content 文本内容 * @param format 格式枚举 - * @param width 宽度 - * @param height 高度 + * @param width 宽度(单位:类型为一般图片或SVG时,单位是像素,类型为 Ascii Art 字符画时,单位是字符▄或▀的大小) + * @param height 高度(单位:类型为一般图片或SVG时,单位是像素,类型为 Ascii Art 字符画时,单位是字符▄或▀的大小) * @return {@link BitMatrix} */ public static BitMatrix encode(String content, BarcodeFormat format, int width, int height) { @@ -428,7 +428,7 @@ public class QrCodeUtil { * * @param content 文本内容 * @param format 格式枚举 - * @param config 二维码配置,包括长、宽、边距、颜色等 + * @param config 二维码配置,包括宽度、高度、边距、颜色等 * @return {@link BitMatrix} * @since 4.1.2 */ @@ -454,11 +454,11 @@ public class QrCodeUtil { /** * 解码二维码或条形码图片为文本 * - * @param qrCodeInputstream 二维码输入流 + * @param qrCodeInputStream 二维码输入流 * @return 解码文本 */ - public static String decode(InputStream qrCodeInputstream) { - return decode(ImgUtil.read(qrCodeInputstream)); + public static String decode(InputStream qrCodeInputStream) { + return decode(ImgUtil.read(qrCodeInputStream)); } /** @@ -549,7 +549,7 @@ public class QrCodeUtil { * BitMatrix转SVG(字符串) * * @param matrix BitMatrix - * @param qrConfig 二维码配置,包括长、宽、边距、颜色等 + * @param qrConfig 二维码配置,包括宽度、高度、边距、颜色等 * @return SVG矢量图(字符串) * @since 5.8.6 */ @@ -567,7 +567,7 @@ public class QrCodeUtil { * @return SVG矢量图(字符串) * @since 5.8.6 */ - public static String toSVG(BitMatrix matrix, int foreColor, Integer backColor, Image logoImg, int ratio) { + public static String toSVG(BitMatrix matrix, Integer foreColor, Integer backColor, Image logoImg, int ratio) { StringBuilder sb = new StringBuilder(); int qrWidth = matrix.getWidth(); int qrHeight = matrix.getHeight(); @@ -575,7 +575,7 @@ public class QrCodeUtil { for (int y = 0; y < qrHeight; y++) { for (int x = 0; x < qrWidth; x++) { if (matrix.get(x, y)) { - sb.append(" M" + x + "," + y + "h1v" + moduleHeight + "h-1z"); + sb.append(" M").append(x).append(",").append(y).append("h1v").append(moduleHeight).append("h-1z"); } } } @@ -600,20 +600,23 @@ public class QrCodeUtil { } - Color fore = new Color(foreColor, true); - StringBuilder result = StrUtil.builder(); - result.append("\n"); - result.append(" \n"); + result.append(" \n"); if (StrUtil.isNotBlank(logoBase64)) { - result.append("\n"); + result.append("\n"); } result.append(""); return result.toString(); @@ -622,7 +625,7 @@ public class QrCodeUtil { /** * BitMatrix转ASCII Art字符画形式的二维码 * - * @param bitMatrix + * @param bitMatrix BitMatrix * @return ASCII Art字符画形式的二维码 * @since 5.8.6 */ @@ -631,8 +634,8 @@ public class QrCodeUtil { final int height = bitMatrix.getHeight(); - final AnsiElement foreground = qrConfig.foreColor == null ? null : Ansi8BitColor.foreground(rgbToAnsi8BitValue(qrConfig.foreColor)); - final AnsiElement background = qrConfig.backColor == null ? null : Ansi8BitColor.background(rgbToAnsi8BitValue(qrConfig.backColor)); + final AnsiElement foreground = qrConfig.foreColor == null ? null : rgbToAnsi8BitElement(qrConfig.foreColor, ForeOrBack.FORE); + final AnsiElement background = qrConfig.backColor == null ? null : rgbToAnsi8BitElement(qrConfig.backColor, ForeOrBack.BACK); StringBuilder builder = new StringBuilder(); for (int i = 0; i <= height; i += 2) { @@ -655,26 +658,16 @@ public class QrCodeUtil { return builder.toString(); } - /** - * rgb转Ansi8Bit值 +/* *//** + * rgb转AnsiElement * * @param rgb rgb颜色值 - * @return Ansi8bit颜色值 + * @param foreOrBack 前景or背景 + * @return AnsiElement * @since 5.8.6 */ - private static int rgbToAnsi8BitValue(int rgb) { - final int r = (rgb >> 16) & 0xff; - final int g = (rgb >> 8) & 0xff; - final int b = (rgb) & 0xff; - - final int l; - if (r == g && g == b) { - final int i = (int) (NumberUtil.div(NumberUtil.mul(r - 10.625, 23), (255 - 10.625), 0)); - l = i >= 0 ? 232 + i : 0; - } else { - l = 16 + (int) (36 * NumberUtil.div(NumberUtil.mul(r, 5), 255, 0)) + (int) (6.0 * (g / 256.0 * 6.0)) + (int) (b / 256.0 * 6.0); - } - return l; + private static AnsiElement rgbToAnsi8BitElement(int rgb,ForeOrBack foreOrBack) { + return ansiColors.findClosest(new Color(rgb)).toAnsiElement(foreOrBack); } diff --git a/hutool-extra/src/main/java/cn/hutool/extra/qrcode/QrConfig.java b/hutool-extra/src/main/java/cn/hutool/extra/qrcode/QrConfig.java index f4ea8f12a..b461eccf4 100755 --- a/hutool-extra/src/main/java/cn/hutool/extra/qrcode/QrConfig.java +++ b/hutool-extra/src/main/java/cn/hutool/extra/qrcode/QrConfig.java @@ -25,9 +25,19 @@ public class QrConfig { private static final int BLACK = 0xFF000000; private static final int WHITE = 0xFFFFFFFF; - /** 宽 */ + + /** + * 宽度(单位:像素或▄) + *
当二维码类型为一般图片或者SVG时,单位是像素
+ *
当二维码类型Ascii Art字符画时,单位是字符▄或▀的大小
+ */ protected int width; - /** 长 */ + + /** + * 高度(单位:像素或▄) + *
当二维码类型为一般图片或者SVG时,单位是像素
+ *
当二维码类型Ascii Art字符画时,单位是字符▄或▀的大小
+ */ protected int height; /** 前景色(二维码颜色) */ protected Integer foreColor = BLACK; diff --git a/hutool-extra/src/test/java/cn/hutool/extra/qrcode/QrCodeUtilTest.java b/hutool-extra/src/test/java/cn/hutool/extra/qrcode/QrCodeUtilTest.java index 8404ec39e..52fbe6ba9 100755 --- a/hutool-extra/src/test/java/cn/hutool/extra/qrcode/QrCodeUtilTest.java +++ b/hutool-extra/src/test/java/cn/hutool/extra/qrcode/QrCodeUtilTest.java @@ -3,11 +3,11 @@ package cn.hutool.extra.qrcode; import cn.hutool.core.codec.Base64; import cn.hutool.core.img.ImgUtil; import cn.hutool.core.io.FileUtil; +import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Console; 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; import org.junit.Test; @@ -29,7 +29,7 @@ public class QrCodeUtilTest { @Test public void generateTest() { final BufferedImage image = QrCodeUtil.generate("https://hutool.cn/", 300, 300); - Assert.assertNotNull(image); + Assert.notNull(image); } @Test @@ -72,7 +72,7 @@ public class QrCodeUtilTest { @Ignore public void decodeTest() { final String decode = QrCodeUtil.decode(FileUtil.file("d:/test/pic/qr.png")); - Console.log(decode); + //Console.log(decode); } @Test @@ -80,13 +80,13 @@ public class QrCodeUtilTest { public void decodeTest2() { // 条形码 final String decode = QrCodeUtil.decode(FileUtil.file("d:/test/90.png")); - Console.log(decode); + //Console.log(decode); } @Test public void generateAsBase64Test() { final String base64 = QrCodeUtil.generateAsBase64("https://hutool.cn/", new QrConfig(400, 400), "png"); - Assert.assertNotNull(base64); + Assert.notNull(base64); } @Test @@ -96,27 +96,27 @@ public class QrCodeUtilTest { 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); + Assert.notNull(base641); } @Test public void generateAsBase64Test3() { final String base64 = QrCodeUtil.generateAsBase64("https://hutool.cn/", new QrConfig(400, 400), "svg"); - Assert.assertNotNull(base64); - System.out.println(base64); + Assert.notNull(base64); + //Console.log(base64); } @Test @Ignore public void decodeTest3() { final String decode = QrCodeUtil.decode(ImgUtil.read("d:/test/qr_a.png"), false, true); - Console.log(decode); + //Console.log(decode); } @Test public void pdf417Test() { final BufferedImage image = QrCodeUtil.generate("content111", BarcodeFormat.PDF_417, QrConfig.create()); - Assert.assertNotNull(image); + Assert.notNull(image); } @Test @@ -124,11 +124,11 @@ public class QrCodeUtilTest { final QrConfig qrConfig = QrConfig.create(); qrConfig.setShapeHint(SymbolShapeHint.FORCE_RECTANGLE); final BufferedImage image = QrCodeUtil.generate("content111", BarcodeFormat.DATA_MATRIX, qrConfig); - Assert.assertNotNull(image); + Assert.notNull(image); final QrConfig config = QrConfig.create(); config.setShapeHint(SymbolShapeHint.FORCE_SQUARE); final BufferedImage imageSquare = QrCodeUtil.generate("content111", BarcodeFormat.DATA_MATRIX, qrConfig); - Assert.assertNotNull(imageSquare); + Assert.notNull(imageSquare); } @Test @@ -142,7 +142,7 @@ public class QrCodeUtilTest { .setErrorCorrection(ErrorCorrectionLevel.M) .setMargin(1); final String svg = QrCodeUtil.generateAsSvg("https://hutool.cn/", qrConfig); - Assert.assertNotNull(svg); + Assert.notNull(svg); FileUtil.writeString(svg, FileUtil.touch("d:/test/hutool_qr.svg"),StandardCharsets.UTF_8); } @@ -154,8 +154,8 @@ public class QrCodeUtilTest { .setWidth(0) .setHeight(0).setMargin(1); final String asciiArt = QrCodeUtil.generateAsAsciiArt("https://hutool.cn/",qrConfig); - Assert.assertNotNull(asciiArt); - //Console.log(asciiArt); + Assert.notNull(asciiArt); + Console.log(asciiArt); } @Test @@ -166,7 +166,7 @@ public class QrCodeUtilTest { .setWidth(0) .setHeight(0).setMargin(1); final String asciiArt = QrCodeUtil.generateAsAsciiArt("https://hutool.cn/",qrConfig); - Assert.assertNotNull(asciiArt); + Assert.notNull(asciiArt); //Console.log(asciiArt); } @@ -180,8 +180,8 @@ public class QrCodeUtilTest { .setWidth(0) .setHeight(0).setMargin(1); final File qrFile = QrCodeUtil.generate("https://hutool.cn/", qrConfig, FileUtil.touch("d:/test/ascii_art_qr_code.txt")); - final BufferedReader reader = FileUtil.getReader(qrFile, StandardCharsets.UTF_8); - reader.lines().forEach(System.out::println); + //final BufferedReader reader = FileUtil.getReader(qrFile, StandardCharsets.UTF_8); + //reader.lines().forEach(Console::log); } @Test @@ -198,21 +198,22 @@ public class QrCodeUtilTest { }catch (final IOException e){ e.printStackTrace(); } - final BufferedReader reader = FileUtil.getReader(filepath, StandardCharsets.UTF_8); - reader.lines().forEach(System.out::println); + //final BufferedReader reader = FileUtil.getReader(filepath, StandardCharsets.UTF_8); + //reader.lines().forEach(Console::log); } @Test @Ignore - public void comparePngAndAsciiArtTest() { + public void comparePngAndSvgAndAsciiArtTest() { final QrConfig qrConfig = QrConfig.create() .setForeColor(null) - .setBackColor(null) - .setWidth(0) - .setHeight(0).setMargin(1); - QrCodeUtil.generate("https://hutool.cn", qrConfig, FileUtil.touch("d:/test/compare/default.jpg")); - QrCodeUtil.generate("https://hutool.cn", qrConfig, FileUtil.touch("d:/test/compare/default.txt")); - QrCodeUtil.generate("https://hutool.cn", qrConfig, FileUtil.touch("d:/test/compare/default.png")); + .setBackColor(Color.WHITE) + .setWidth(200) + .setHeight(200).setMargin(1); + QrCodeUtil.generate("https://hutool.cn", qrConfig, FileUtil.touch("d:/test/compare/config_null_color.jpg")); + QrCodeUtil.generate("https://hutool.cn", qrConfig, FileUtil.touch("d:/test/compare/config_null_color.txt")); + QrCodeUtil.generate("https://hutool.cn", qrConfig, FileUtil.touch("d:/test/compare/config_null_color.png")); + QrCodeUtil.generate("https://hutool.cn", qrConfig, FileUtil.touch("d:/test/compare/config_null_color.svg")); } }