Advertisement
Guest User

ASCIIArt.java

a guest
Jul 28th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.86 KB | None | 0 0
  1. import javax.imageio.ImageIO;
  2. import java.awt.*;
  3. import java.awt.image.BufferedImage;
  4. import java.io.BufferedWriter;
  5. import java.io.File;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.nio.file.NoSuchFileException;
  9. import java.util.Scanner;
  10.  
  11. import static java.lang.System.out;
  12.  
  13. /**
  14. * Created by User on 7/25/2016.
  15. */
  16.  
  17.  
  18. public class ASCIIArt {
  19.  
  20. static BufferedImage imgRead = null;
  21. static BufferedWriter writer = null;
  22.  
  23. public static void main(String[] args) {
  24. Scanner inp = new Scanner(System.in);
  25. int tempInp = 0;
  26. int artWidth = 0;
  27. int artHeight = 0;
  28.  
  29. try {
  30. writer = new BufferedWriter(new FileWriter("output.txt"));
  31. } catch (Exception e) {
  32. out.println("Unable to create text file for output.");
  33. System.exit(0);
  34. }
  35.  
  36. //get a valid BufferedImage
  37. try {
  38. imgRead = ImageIO.read(new File("ASCII.jpg"));
  39. } catch (NoSuchFileException e) {
  40. out.println("Please put a .jpg image into the folder.");
  41. System.exit(0);
  42. } catch (Exception e) {
  43. out.println("Only .jpg format supported at this time.");
  44. System.exit(0);
  45. }
  46.  
  47. //get the desired width (in characters), making sure it is less than the image width and not 0 or less.
  48. boolean invalidInp = false;
  49. out.println("How many pixels width-wise do you want to be represented per ASCII character?" +
  50. "\nExample: If the input is 10, then a 400x200 image would be represented by a text file of 40x10.");
  51. do {
  52. try {
  53. tempInp = Integer.parseInt(inp.nextLine());
  54. invalidInp = false;
  55. } catch (Exception e) {
  56. out.println("Please input a number.");
  57. invalidInp = true;
  58. }
  59. if (tempInp > imgRead.getWidth() && !invalidInp) {
  60. out.println("Please input a number that is less than or equal to the original dimensions of the picture.");
  61. invalidInp = true;
  62. } else if (tempInp < 1 && !invalidInp) {
  63. out.println("Please input a positive number.");
  64. invalidInp = true;
  65. }
  66. } while (invalidInp);
  67. artWidth = tempInp;
  68. //Text is twice as tall as it is wide, so we have to half the number of characters per pixel height-wise
  69. artHeight = artWidth * 2;
  70.  
  71. //shaves off pixels that make the image dimensions indivisible with the desired width
  72. int nHeight = imgRead.getHeight();
  73. int nWidth = imgRead.getWidth();
  74. if (imgRead.getHeight() % artHeight != 0) {
  75. nHeight = nHeight - (imgRead.getHeight() % artHeight);
  76. }
  77. if (imgRead.getWidth() % artWidth != 0) {
  78. nWidth = nWidth - (imgRead.getWidth() % artWidth);
  79. }
  80.  
  81. int count = 0;
  82. for (int r = 0; r < nHeight; r += artHeight) {
  83. for (int c = 0; c < nWidth; c += artWidth) {
  84. int totalR = 0;
  85. int totalG = 0;
  86. int totalB = 0;
  87. Double avgBright = 0.0;
  88. for (int x = c; x < c + artWidth; x++) {
  89. for (int y = r; y < r + artHeight; y++) {
  90. Color getColor = new Color(imgRead.getRGB(x, y));
  91. totalR += getColor.getRed();
  92. totalG += getColor.getGreen();
  93. totalB += getColor.getBlue();
  94. count++;
  95. }
  96. }
  97. avgBright = (Math.sqrt(0.299 * totalR * totalR + 0.587 * totalG * totalG + 0.114 * totalB * totalB) / (artWidth * artHeight));
  98. //out.println(count + " " + avgBright);
  99. try {
  100. printChar(avgBright);
  101. } catch (IOException e) {
  102. e.printStackTrace();
  103. System.exit(1);
  104. }
  105. }
  106. try {
  107. writer.newLine();
  108. } catch (Exception e) {
  109. e.printStackTrace();
  110. System.exit(1);
  111. }
  112. }
  113. try {
  114. writer.flush();
  115. } catch (IOException e) {
  116. e.printStackTrace();
  117. }
  118. out.close();
  119.  
  120. }
  121.  
  122. private static void printChar(Double avgBright) throws IOException {
  123.  
  124. //this organization is fucking AIDS but it makes a mostly decipherable picture
  125. if (avgBright < 15) {
  126. writer.write('█');
  127. return;
  128. }
  129. if (avgBright < 30) {
  130. writer.write('▓');
  131. return;
  132. }
  133. if (avgBright < 50) {
  134. writer.write('▒');
  135. return;
  136. }
  137. if (avgBright < 65) {
  138. writer.write('░');
  139. return;
  140. }
  141. if (avgBright < 80) {
  142. writer.write('X');
  143. return;
  144. }
  145. if (avgBright < 100) {
  146. writer.write('E');
  147. return;
  148. }
  149. if (avgBright < 115) {
  150. writer.write('S');
  151. return;
  152. }
  153. if (avgBright < 130) {
  154. writer.write('7');
  155. return;
  156. }
  157. if (avgBright < 145) {
  158. writer.write('1');
  159. return;
  160. }
  161. if (avgBright < 150) {
  162. writer.write(':');
  163. return;
  164. }
  165. if (avgBright < 170) {
  166. writer.write('<');
  167. return;
  168. }
  169. if (avgBright < 190) {
  170. writer.write('-');
  171. return;
  172. }
  173. if (avgBright < 210) {
  174. writer.write('`');
  175. return;
  176. }
  177. if (avgBright < 230) {
  178. writer.write('.');
  179. } else {
  180. writer.write(' ');
  181. }
  182. }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement