From 5da8e1c9a780c54466b64c87dd8fbea0f613810c Mon Sep 17 00:00:00 2001 From: liujiahua123123 Date: Mon, 2 Sep 2019 10:55:11 +0800 Subject: [PATCH 1/3] robot data system --- mirai-core/src/main/java/net/mamoe/mirai/Robot.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mirai-core/src/main/java/net/mamoe/mirai/Robot.java b/mirai-core/src/main/java/net/mamoe/mirai/Robot.java index 16469e5d2..79b9fe6d5 100644 --- a/mirai-core/src/main/java/net/mamoe/mirai/Robot.java +++ b/mirai-core/src/main/java/net/mamoe/mirai/Robot.java @@ -12,6 +12,8 @@ public class Robot { private final int qq; private final String password; + + @Getter private final RobotNetworkHandler handler; @@ -43,6 +45,5 @@ public class Robot { this.handler = new RobotNetworkHandler(this, this.qq, this.password); } - } From 5b07f09d3d58d805abca1147be3122978d8df2ea Mon Sep 17 00:00:00 2001 From: liujiahua123123 Date: Sat, 7 Sep 2019 11:48:52 +0800 Subject: [PATCH 2/3] changes --- .../src/main/java/net/mamoe/mirai/Robot.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/mirai-core/src/main/java/net/mamoe/mirai/Robot.java b/mirai-core/src/main/java/net/mamoe/mirai/Robot.java index fa6d90e37..a1d273d32 100644 --- a/mirai-core/src/main/java/net/mamoe/mirai/Robot.java +++ b/mirai-core/src/main/java/net/mamoe/mirai/Robot.java @@ -1,10 +1,12 @@ package net.mamoe.mirai; +import kotlin.jvm.internal.MagicApiIntrinsics; import lombok.Getter; import net.mamoe.mirai.contact.Group; import net.mamoe.mirai.contact.QQ; import net.mamoe.mirai.network.RobotNetworkHandler; import net.mamoe.mirai.utils.ContactList; +import net.mamoe.mirai.utils.config.MiraiConfig; import net.mamoe.mirai.utils.config.MiraiConfigSection; import java.util.ArrayList; @@ -65,5 +67,25 @@ public class Robot { public Group getGroupByGroupId(int groupId) { return getGroup(Group.Companion.groupIdToNumber(groupId)); } + + /* Attribute + * Attribute will be SAVED and LOAD automatically as long as the QQ account is same + * {Attributes} is in the format of Map, keeping thread-safe + * {Attributes} is a KEY-VALUE typed Data. + * * + **/ + } +/* +class RobotAttribute extends MiraiConfigSection{ + + static RobotAttribute load(Robot robot){ + + } + + private MiraiConfigSection data;//late init + +} + +*/ From 3dd79367008b721e28507fc0dc77e365791fac7b Mon Sep 17 00:00:00 2001 From: liujiahua123123 Date: Sat, 7 Sep 2019 15:14:51 +0800 Subject: [PATCH 3/3] char image support --- .../mamoe/mirai/utils/CharImageConverter.java | 92 +++++++++++++++++++ .../net/mamoe/mirai/utils/CharImageUtil.java | 43 +-------- mirai-core/src/test/java/ImageOutputTest.java | 14 +++ 3 files changed, 107 insertions(+), 42 deletions(-) create mode 100644 mirai-core/src/main/java/net/mamoe/mirai/utils/CharImageConverter.java create mode 100644 mirai-core/src/test/java/ImageOutputTest.java diff --git a/mirai-core/src/main/java/net/mamoe/mirai/utils/CharImageConverter.java b/mirai-core/src/main/java/net/mamoe/mirai/utils/CharImageConverter.java new file mode 100644 index 000000000..23ca34656 --- /dev/null +++ b/mirai-core/src/main/java/net/mamoe/mirai/utils/CharImageConverter.java @@ -0,0 +1,92 @@ +package net.mamoe.mirai.utils; + +import java.awt.*; +import java.awt.image.BufferedImage; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.Callable; + +/** + * Convert IMAGE into Chars that could shows in terminal + * @author NaturalHG + */ +public class CharImageConverter implements Callable { + + /** + * width should depends on the width of the terminal + */ + private BufferedImage image; + private int width; + private double ignoreRate; + + public CharImageConverter(BufferedImage image, int width){ + this(image,width,0.95); + } + + public CharImageConverter(BufferedImage image, int width, double ignoreRate){ + this.image = image; + this.width = width; + this.ignoreRate = ignoreRate; + } + + @Override + public String call(){ + /* + * resize Image + * */ + int newHeight = (int)(this.image.getHeight() * (((double)width) /this.image.getWidth())); + Image tmp = image.getScaledInstance(width, newHeight, Image.SCALE_SMOOTH); + BufferedImage dimg = new BufferedImage(width, newHeight, BufferedImage.TYPE_INT_ARGB); + Graphics2D g2d = dimg.createGraphics(); + g2d.drawImage(tmp, 0, 0, null); + this.image = dimg; + + int background = this.gray(image.getRGB(0,0)); + + StringBuilder builder = new StringBuilder(); + + List lines = new ArrayList<>(this.image.getHeight()); + + int minXPos = this.width; + int maxXPos = 0; + + for (int y = 0; y < image.getHeight(); y++) { + StringBuilder builderLine = new StringBuilder(); + for (int x = 0; x < image.getWidth(); x++) { + int gray = this.gray(image.getRGB(x, y)); + if (grayCompare(gray, background)) { + builderLine.append(" "); + } else { + builderLine.append("#"); + if (x < minXPos) { + minXPos = x; + } + if (x > maxXPos) { + maxXPos = x; + } + } + } + if (builderLine.toString().isBlank()) { + continue; + } + lines.add(builderLine); + } + for (int i=0;i> 16; + int G = (rgb & 0x00ff00) >> 8; + int B = rgb & 0x0000ff; + int gray = (R * 30 + G * 59 + B * 11 + 50) / 100; + return (R * 30 + G * 59 + B * 11 + 50) / 100; + } + + public boolean grayCompare(int g1, int g2){ + return ((double)Math.min(g1,g2)/Math.max(g1,g2)) >= ignoreRate; + } + +} diff --git a/mirai-core/src/main/java/net/mamoe/mirai/utils/CharImageUtil.java b/mirai-core/src/main/java/net/mamoe/mirai/utils/CharImageUtil.java index b39249b3f..4c77e83a5 100644 --- a/mirai-core/src/main/java/net/mamoe/mirai/utils/CharImageUtil.java +++ b/mirai-core/src/main/java/net/mamoe/mirai/utils/CharImageUtil.java @@ -1,13 +1,6 @@ package net.mamoe.mirai.utils; - -import java.awt.*; import java.awt.image.BufferedImage; -/** - * 图片转字符图片, 来自 CSDN 开源 - * - * @author zhoujie https://blog.csdn.net/qq_37902949/article/details/81228566 - */ public final class CharImageUtil { public static String createCharImg(BufferedImage image) { @@ -15,41 +8,7 @@ public final class CharImageUtil { } public static String createCharImg(BufferedImage image, int sizeWeight, int sizeHeight) { - //生成字符图片 - image = resize(image, sizeWeight, sizeHeight); - int width = image.getWidth(); - int height = image.getHeight(); - - StringBuilder output = new StringBuilder(); - for (int i = 0; i < height; i++) { - StringBuilder line = new StringBuilder(); - for (int j = 0; j < width; j++) { - int rgb = image.getRGB(j, i); - int R = (rgb & 0xff0000) >> 16; - int G = (rgb & 0x00ff00) >> 8; - int B = rgb & 0x0000ff; - int gray = (R * 30 + G * 59 + B * 11 + 50) / 100; - int index = 31 * gray / 255; - line.append(asc[index]); //添加每个字符 - } - output.append(line).append("\n"); - } - return output.toString(); + return new CharImageConverter(image,sizeWeight).call(); } - public static BufferedImage resize(BufferedImage img, int newW, int newH) { - Image tmp = img.getScaledInstance(newW, newH, Image.SCALE_SMOOTH); - BufferedImage dimg = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB); - - Graphics2D g2d = dimg.createGraphics(); - g2d.drawImage(tmp, 0, 0, null); - g2d.dispose(); - - return dimg; - } - - private final static char[] asc = {' ', '`', '.', '^', ',', ':', '~', '"', - '<', '!', 'c', 't', '+', '{', 'i', '7', '?', 'u', '3', '0', 'p', 'w', - '4', 'A', '8', 'D', 'X', '%', '#', 'H', 'W', 'M'}; - } \ No newline at end of file diff --git a/mirai-core/src/test/java/ImageOutputTest.java b/mirai-core/src/test/java/ImageOutputTest.java new file mode 100644 index 000000000..bebd617cf --- /dev/null +++ b/mirai-core/src/test/java/ImageOutputTest.java @@ -0,0 +1,14 @@ +import net.mamoe.mirai.utils.CharImageConverter; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; + +public class ImageOutputTest { + public static void main(String[] args) throws IOException { + BufferedImage image = ImageIO.read(new File((System.getProperty("user.dir") + "/VerificationCode.png").replace("//","/"))); + CharImageConverter charImageConvertor = new CharImageConverter(image,100); + System.out.println(charImageConvertor.call()); + } +}