Advertisement
Guest User

ASCIIArt.java

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