Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.imageio.ImageIO;
- import java.awt.*;
- import java.awt.geom.Arc2D;
- import java.awt.image.BufferedImage;
- import java.awt.image.DataBufferByte;
- import java.awt.image.DataBufferInt;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.nio.file.NoSuchFileException;
- import java.util.Arrays;
- import java.util.Scanner;
- import static java.lang.System.out;
- import static javafx.scene.input.KeyCode.R;
- /**
- * Created by User on 7/25/2016.
- */
- public class ASCIIArt {
- static BufferedImage imgRead = null;
- static BufferedWriter writer = null;
- public static void main(String[] args) {
- Scanner inp = new Scanner(System.in);
- int tempInp = 0;
- int artWidth = 0;
- int artHeight = 0;
- try {
- writer = new BufferedWriter(new FileWriter("output.txt"));
- } catch (Exception e) {
- out.println("Unable to create text file for output.");
- System.exit(0);
- }
- //get a valid BufferedImage
- try {
- imgRead = ImageIO.read(new File("ASCII.jpg"));
- } catch (NoSuchFileException e) {
- out.println("Please put a .jpg image into the folder.");
- System.exit(0);
- } catch (Exception e) {
- out.println("Only .jpg format supported at this time.");
- System.exit(0);
- }
- //get the desired width (in characters), making sure it is less than the image width and not 0 or less.
- boolean invalidInp = false;
- out.println("How many pixels width-wise do you want to be represented per ASCII character?" +
- "\nExample: If the input is 10, then a 400x200 image would be represented by a text file of 40x10.");
- do {
- try {
- tempInp = Integer.parseInt(inp.nextLine());
- invalidInp = false;
- } catch (Exception e) {
- out.println("Please input a number.");
- invalidInp = true;
- }
- if (tempInp > imgRead.getWidth() && !invalidInp) {
- out.println("Please input a number that is less than or equal to the original dimensions of the picture.");
- invalidInp = true;
- } else if (tempInp < 1 && !invalidInp) {
- out.println("Please input a positive number.");
- invalidInp = true;
- }
- } while (invalidInp);
- artWidth = tempInp;
- //Text is twice as tall as it is wide, so we have to half the number of characters per pixel height-wise
- artHeight = artWidth * 2;
- //shaves off pixels that make the image dimensions indivisible with the desired width
- int nHeight = imgRead.getHeight();
- int nWidth = imgRead.getWidth();
- if (imgRead.getHeight() % artHeight != 0) {
- nHeight = nHeight - (imgRead.getHeight() % artHeight);
- }
- if (imgRead.getWidth() % artWidth != 0) {
- nWidth = nWidth - (imgRead.getWidth() % artWidth);
- }
- int count = 0;
- for (int r = 0; r < nHeight; r += artHeight) {
- for (int c = 0; c < nWidth; c += artWidth) {
- int totalR = 0;
- int totalG = 0;
- int totalB = 0;
- Double avgBright = 0.0;
- for (int x = c; x < c + artWidth; x++) {
- for (int y = r; y < r + artHeight; y++) {
- Color getColor = new Color(imgRead.getRGB(x, y));
- totalR += getColor.getRed();
- totalG += getColor.getGreen();
- totalB += getColor.getBlue();
- count++;
- }
- }
- avgBright = (Math.sqrt(0.299 * totalR * totalR + 0.587 * totalG * totalG + 0.114 * totalB * totalB) / (artWidth * artHeight));
- //out.println(count + " " + avgBright);
- try {
- printChar(avgBright);
- } catch (IOException e) {
- e.printStackTrace();
- System.exit(1);
- }
- }
- try {
- writer.newLine();
- } catch (Exception e) {
- e.printStackTrace();
- System.exit(1);
- }
- }
- try {
- writer.flush();
- } catch (IOException e) {
- e.printStackTrace();
- }
- out.close();
- }
- private static void printChar(Double avgBright) throws IOException {
- //this organization is fucking AIDS but it makes a mostly decipherable picture
- if (avgBright < 15) {
- writer.write('█');
- return;
- }
- if (avgBright < 30) {
- writer.write('▓');
- return;
- }
- if (avgBright < 50) {
- writer.write('▒');
- return;
- }
- if (avgBright < 65) {
- writer.write('░');
- return;
- }
- if (avgBright < 80) {
- writer.write('X');
- return;
- }
- if (avgBright < 100) {
- writer.write('E');
- return;
- }
- if (avgBright < 115) {
- writer.write('S');
- return;
- }
- if (avgBright < 130) {
- writer.write('7');
- return;
- }
- if (avgBright < 145) {
- writer.write('1');
- return;
- }
- if (avgBright < 150) {
- writer.write(':');
- return;
- }
- if (avgBright < 170) {
- writer.write('<');
- return;
- }
- if (avgBright < 190) {
- writer.write('-');
- return;
- }
- if (avgBright < 210) {
- writer.write('`');
- return;
- }
- if (avgBright < 230) {
- writer.write('.');
- } else {
- writer.write(' ');
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement