ASCII Art Java example

ascii-art-java

A funny Java example to create an ASCII art graphic. The concept is simple, get the image’s rgb color in “integer mode”, later, replace the color’s integer with ascii text.

P.S This example is credited for this post

ASCIIArtService.java

package com.mkyong.service;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;

public class ASCIIArtService {

    public static void main(String[] args) throws IOException {

        int width = 100;
        int height = 30;

        //BufferedImage image = ImageIO.read(new File("/Users/mkyong/Desktop/logo.jpg"));
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
        g.setFont(new Font("SansSerif", Font.BOLD, 24));

        Graphics2D graphics = (Graphics2D) g;
        graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        graphics.drawString("JAVA", 10, 20);

        //save this image
        //ImageIO.write(image, "png", new File("/users/mkyong/ascii-art.png"));

        for (int y = 0; y < height; y++) {
            StringBuilder sb = new StringBuilder();
            for (int x = 0; x < width; x++) {

                sb.append(image.getRGB(x, y) == -16777216 ? " " : "$");

            }

            if (sb.toString().trim().isEmpty()) {
                continue;
            }

            System.out.println(sb);
        }

    }

}

Output


                  $$$        $$$$$      $$$$         $$$$     $$$$$                                 
                  $$$       $$$$$$$     $$$$         $$$$    $$$$$$$                                
                  $$$       $$$$$$$     $$$$$       $$$$$    $$$$$$$                                
                  $$$       $$$$$$$      $$$$       $$$$     $$$$$$$                                
                  $$$      $$$$ $$$$     $$$$$     $$$$$    $$$$ $$$$                               
                  $$$      $$$$ $$$$      $$$$     $$$$     $$$$ $$$$                               
                  $$$     $$$$$ $$$$$     $$$$     $$$$    $$$$$ $$$$$                              
                  $$$     $$$$   $$$$     $$$$$   $$$$$    $$$$   $$$$                              
                  $$$     $$$$   $$$$      $$$$   $$$$     $$$$   $$$$                              
                  $$$    $$$$$   $$$$$     $$$$$ $$$$$    $$$$$   $$$$$                             
                  $$$    $$$$$$$$$$$$$      $$$$ $$$$     $$$$$$$$$$$$$                             
          $$$$   $$$$    $$$$$$$$$$$$$      $$$$ $$$$     $$$$$$$$$$$$$                             
          $$$$   $$$$   $$$$$$$$$$$$$$$      $$$$$$$     $$$$$$$$$$$$$$$                            
          $$$$$ $$$$$   $$$$       $$$$      $$$$$$$     $$$$       $$$$                            
          $$$$$$$$$$$  $$$$$       $$$$$     $$$$$$$    $$$$$       $$$$$                           
           $$$$$$$$$   $$$$         $$$$      $$$$$     $$$$         $$$$                           
            $$$$$$$    $$$$         $$$$      $$$$$     $$$$         $$$$                           
What is -1677216?
The color code, in this case all colors "-1677216" are replaced with empty " ". This is the idea to generate the ASCII art graphic. Try load an image, and print out the rgb color, you will noticed that different color has different code.

References

  1. Wiki : ASCII Art
  2. Stackoverflow - ascii art in Java
  3. Creating a Graphic form of the Provided Integer

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Milad
Guest
Milad

Thanks.

Elad Finish
Guest
Elad Finish

Do you have it for Android?

Hongten Jone
Guest
Hongten Jone
Telmo
Guest
Telmo

Very Nice

Java Online Trainer
Guest
Java Online Trainer

Very excellent!! shows that you think more about JAVA.

Tihamer
Guest
Tihamer
Nice work, Mkyong. Here is an improved version that checks the range of brightness, splits it up into 8 ranges, and prints the corresponding character: import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import java.net.MalformedURLException; import java.net.URL; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; public class AsciiArt { public AsciiArt() { // Auto-generated constructor stub } public static void transform(ImageIcon imageIcon) { int width = imageIcon.getIconWidth(); int height = imageIcon.getIconHeight(); System.out.println("wh="+width + ", " + height); System.out.println("wh="+width + ", " + height); Image image = imageIcon.getImage(); int dataBuffInt, sum; String thisChar = ""; BufferedImage buffered = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR); buffered.getGraphics().drawImage(image,… Read more »
a??k
Guest
a??k

I think you are Crazy Clever Big Programmer..:)God Bless You..

ninjacoder
Guest
ninjacoder

(facepalm) Could you please work on some interest projects?

sherbert
Guest
sherbert

dude, give the guy a break! he gives you GOLD all the time!!!

udaykiran
Guest
udaykiran

Good Post mkyong.

Felipe
Guest
Felipe

It’s funny ! It works!