Sunday, 1 December 2013

Generating ASCII Art From Input Text Or Image Using JAVA

Wikipedia describe as ASCII art as "A graphic design technique that uses computers for presentation and consists of pictures pieced together from the 95 printable (from a total of 128) characters defined by the ASCII Standard from 1963 and ASCII compliant character sets with proprietary extended characters (beyond the 128 characters of standard 7-bit ASCII)."

ASCII art has no use in real life. Still, it is fun to create pattern and images using the ASCII characters only. I still find people posting ascii picture of their name etc. on facebook.

It is very easy to create such art programatically. You can write text to a bitmap in any font you want and scan the bitmap to produce the ASCII image. A simple example which take a string as input and print out the name as the art is shown below:
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Scanner;

public class ASCIIArt {

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

		int width = 150;
		int height = 28;
		
		Scanner sc = new Scanner(System.in);
		System.out.print("Enter your name: ");
		String name = sc.nextLine();
		
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics g = image.getGraphics();
		g.setFont(new Font("Dialog", Font.BOLD, 20));

		Graphics2D graphics = (Graphics2D) g;
		graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
				RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
		graphics.drawString(name, 2, 20);

		for (int y = 0; y < height; y++) {
			StringBuilder strbuild = new StringBuilder();
			for (int x = 0; x < width; x++) 
				strbuild.append(image.getRGB(x, y) == -16777216 ? " " : "a");

			if (strbuild.toString().trim().isEmpty()) 
				continue;
			System.out.println(strbuild);
		}
		sc.close();
	}
}
A sample run is shown below:
Enter your name: Knavite
   aaa    aaaaaa                                   aaa   aa                                                                                           
   aaa   aaaaaa                                    aaa  aaa                                                                                           
   aaa   aaaaa                                          aaa                                                                                           
   aaa  aaaaa                                           aaa                                                                                           
   aaa aaaaa     aaaaaaaaa    aaaaaaa  aaaa   aaaa aaa aaaaaa   aaaaaaa                                                                               
   aaaaaaaa      aaaaaaaaaa  aaaaaaaaa aaaa   aaaa aaa aaaaaa  aaaaaaaaa                                                                              
   aaaaaaaaa     aaaaaaaaaa  aaaaaaaaa  aaaa aaaa  aaa  aaa    aaaa aaaa                                                                              
   aaaaaaaaa     aaaa  aaaa        aaa  aaaa aaaa  aaa  aaa    aaaa aaaa                                                                              
   aaaaa aaaa    aaaa   aaa    aaaaaaa   aaa aaa   aaa  aaa    aaaaaaaaa                                                                              
   aaaa  aaaa    aaa    aaa  aaaaaaaaa   aaaaaaa   aaa  aaa    aaaaaaaaa                                                                              
   aaaa   aaaa   aaa    aaa  aaaaaaaaa   aaaaaaa   aaa  aaa    aaaa                                                                                   
   aaa    aaaa   aaa    aaa  aaaa aaaa    aaaaa    aaa  aaa    aaaa                                                                                   
   aaa     aaaa  aaa    aaa  aaaaaaaaaa   aaaaa    aaa  aaaaa  aaaaaaaaa                                                                              
   aaa     aaaa  aaa    aaa  aaaaaaaaaa   aaaaa    aaa  aaaaa  aaaaaaaaa                                                                              
   aaa      aaaa aaa    aaa   aaaaaaaaa    aaa     aaa  aaaaa   aaaaaaa                                                                               

You can also use image to generate the art. Below example show the use of image for generating the art.
package com.example;

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

import javax.imageio.ImageIO;

public class ASCIIArt {

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

		int width = 50;
		int height = 50;
		
		BufferedImage image = ImageIO.read(new File("C:\\Users\\naved.alam\\Desktop\\img.bmp"));
		for (int y = 0; y < height; y++) {
			StringBuilder strbuild = new StringBuilder();
			for (int x = 0; x < width; x++) 
				strbuild.append(image.getRGB(x, y) < -1 ? "a" : " ");
			if (strbuild.toString().trim().isEmpty()) 
				continue;
			System.out.println(strbuild);
		}
	}
}
The output is shown below for the sample image:
                   aaaaaaaaaaa                    
              aaaaaaaaaaaaaaaaaaaaa               
            aaaaaaaaaaaaaaaaaaaaaaaaa             
          aaaaaaaaaaaaaaaaaaaaaaaaaaaaa           
        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa         
      aaaaaaaaaaaaa           aaaaaaaaaaaaa       
     aaaaaaaaaa                   aaaaaaaaaa      
    aaaaaaaaa                       aaaaaaaaa     
   aaaaaaaa           aaaaa           aaaaaaaa    
   aaaaaa           aaaaaaaaa           aaaaaa    
  aaaaaa           aaaaaaaaaaa           aaaaaa   
  aaaaa           aaaaaaaaaaaaa           aaaaa   
 aaaaaa          aaaaaaaaaaaaaaa          aaaaaa  
 aaaaa           aaaaaaaaaaaaaaa           aaaaa  
 aaaaa           aaaaaaaaaaaaaaa           aaaaa  
 aaaaa           aaaaaaaaaaaaaaa           aaaaa  
 aaaaa           aaaaaaaaaaaaaaa           aaaaa  
 aaaaaa           aaaaaaaaaaaaa           aaaaaa  
  aaaaa            aaaaaaaaaaa            aaaaa   
  aaaaaa            aaaaaaaaa            aaaaaa   
   aaaaaa             aaaaa             aaaaaa    
   aaaaaaaa                           aaaaaaaa    
    aaaaaaaa                        aaaaaaaaa     
     aaaaaaaaaa                   aaaaaaaaaa      
      aaaaaaaaaaaaa           aaaaaaaaaaaaa       
        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa         
         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa          
           aaaaaaaaaaaaaaaaaaaaaaaaaaa            
              aaaaaaaaaaaaaaaaaaaaa               
                   aaaaaaaaaaaa                   

Using this concept, more flexiblity can be added to generated ascii images like different character for different RGB values, anti aliasing effect, different font weight and style etc.

1 comment: