-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImg2Ascii.java
More file actions
89 lines (80 loc) · 2.33 KB
/
Copy pathImg2Ascii.java
File metadata and controls
89 lines (80 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* TAG: Team Adventure Game
* Code altered: Don Combs
* 11-24-2019 To Current Date
*
*/
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.imageio.ImageIO;
public class Img2Ascii {
private BufferedImage img;
private double pixval;
private PrintWriter prntwrt;
private FileWriter filewrt;
public Img2Ascii() {
try {
prntwrt = new PrintWriter(filewrt = new FileWriter("asciiart.txt",
true));
} catch (IOException ex) {
}
}
public void convertToAscii(String imgname) {
try {
img = ImageIO.read(new File(imgname));
} catch (IOException e) {
}
for (int i = 0; i < img.getHeight(); i++) {
for (int j = 0; j < img.getWidth(); j++) {
Color pixcol = new Color(img.getRGB(j, i));
pixval = (((pixcol.getRed() * 0.30) + (pixcol.getBlue() * 0.59) + (pixcol
.getGreen() * 0.11)));
print(strChar(pixval));
}
try {
prntwrt.println("");
prntwrt.flush();
filewrt.flush();
} catch (Exception ex) {
}
}
}
public String strChar(double g) {
String str = " ";
if (g >= 240) {
str = " ";
} else if (g >= 210) {
str = ".";
} else if (g >= 190) {
str = "*";
} else if (g >= 170) {
str = "+";
} else if (g >= 120) {
str = "^";
} else if (g >= 110) {
str = "&";
} else if (g >= 80) {
str = "8";
} else if (g >= 60) {
str = "#";
} else {
str = "@";
}
return str;
}
public void print(String str) {
try {
prntwrt.print(str);
prntwrt.flush();
filewrt.flush();
} catch (Exception ex) {
}
}
public static void main(String[] args) {
Img2Ascii obj = new Img2Ascii();
obj.convertToAscii("C:\\Users\\Mike\\Desktop\\Eclipse-Workspace\\ADV_GAME_1\\small_imp-2-2.png");
}
}