Merge remote-tracking branch 'origin/master'

This commit is contained in:
Him188moe 2019-09-07 15:19:26 +08:00
commit 36f1980a84
3 changed files with 107 additions and 42 deletions

View File

@ -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<String> {
/**
* 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<StringBuilder> 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<lines.size();++i) {
builder.append(lines.get(i).substring(minXPos, maxXPos)).append("\n");
}
return builder.toString();
}
private int gray(int rgb) {
int R = (rgb & 0xff0000) >> 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;
}
}

View File

@ -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'};
}

View File

@ -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());
}
}