Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.imageio.ImageIO;
- import java.awt.*;
- import java.awt.image.*;
- import java.io.File;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.Objects;
- class Main {
- private final static String DEFAULT_ORIGINAL_IMAGE_PATH = "images/google.png";
- private final static String DEFAULT_ASCII_IMAGE_PATH = "images/copy.txt";
- public static void main(String[] args) {
- BufferedImage image = null;
- Color color;
- PrintWriter pw = null;
- try {
- image = ImageIO.read(new File(DEFAULT_ORIGINAL_IMAGE_PATH));
- pw = new PrintWriter(new File(DEFAULT_ASCII_IMAGE_PATH));
- } catch (IOException | NullPointerException e) {
- System.out.println(e.getMessage());
- }
- try {
- for (int i = 0; i < image.getHeight(); i++) {
- for (int j = 0; j < image.getWidth(); j++) {
- color = new Color(image.getRGB(j, i));
- double colorValue = ((double) color.getRed() + (double) color.getBlue() + (double) color.getGreen()) / 3;
- Objects.requireNonNull(pw).append(getCharRepresentation(colorValue));
- }
- }
- } catch (NullPointerException npe) {
- System.out.println(npe.getMessage());
- }
- }
- private static char getCharRepresentation(double value) {
- if (value >= 230.0) {
- return ' ';
- } else if (value >= 200.0) {
- return '.';
- } else if (value >= 180.0) {
- return '*';
- } else if (value >= 160.0) {
- return ':';
- } else if (value >= 130.0) {
- return 'o';
- } else if (value >= 100.0) {
- return '&';
- } else if (value >= 70.0) {
- return '8';
- } else if (value >= 50.0) {
- return '#';
- } else {
- return '@';
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement