Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
M
Mirai
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
Mirai
Commits
3dd79367
Commit
3dd79367
authored
Sep 07, 2019
by
liujiahua123123
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
char image support
parent
7682f210
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
107 additions
and
42 deletions
+107
-42
mirai-core/src/main/java/net/mamoe/mirai/utils/CharImageConverter.java
...c/main/java/net/mamoe/mirai/utils/CharImageConverter.java
+92
-0
mirai-core/src/main/java/net/mamoe/mirai/utils/CharImageUtil.java
...re/src/main/java/net/mamoe/mirai/utils/CharImageUtil.java
+1
-42
mirai-core/src/test/java/ImageOutputTest.java
mirai-core/src/test/java/ImageOutputTest.java
+14
-0
No files found.
mirai-core/src/main/java/net/mamoe/mirai/utils/CharImageConverter.java
0 → 100644
View file @
3dd79367
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
;
}
}
mirai-core/src/main/java/net/mamoe/mirai/utils/CharImageUtil.java
View file @
3dd79367
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
mirai-core/src/test/java/ImageOutputTest.java
0 → 100644
View file @
3dd79367
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
());
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment