Showing posts with label gui. Show all posts
Showing posts with label gui. Show all posts

Friday, February 25, 2011

What more can you ASCII for?

Greetings

I am not the artistic type, but that does not mean that I do not appreciate art. While I do like art created by others, I sometimes wish that I too was gifted with artistic abilities. Once again, using the awesome powers of Java, I was able to realize this dream. Using simple Java code, I created an image to ASCII converter program which takes an image and outputs a text file with ASCII characters, which looks like the input image when viewed all at once. In this post I will explain the basic principle behind computer generated ASCII art, and how to implement it using Java. I will release my own code for the program that I built, the program itself and a short video of the program in action. From now on, I will release all my code under the Open Source Initiative (OSI) MIT License (MIT), because the license is clean, short and easy to understand. Before I explain the principle behind ASCII art, here are some of the ASCII art created by the program: 
That's me!!

The Simpsons are ASCIIfied!
p-51 Mustang- A classic

The principle behind ASCII art is easy to understand. An image can be thought of as an 2 dimension array with rows and columns that correspond to the width and height of the image (In reality, it is more complex than that but this is a good enough understanding for the time being). Each cell in this array holds a pixel. When all these pixels are viewed together at once, you get an image. So a 400x400 image has 400 rows and 400 columns, and has 160000 pixels. Now to convert an image to text, the program has to iterate through all the pixels, get its RGB value, convert it to its gray scale value, and append a suitable ASCII character. Java has a class called the BufferedImage class, which has the methods necessary to obtain the RGB values of a pixel. Now once the RGB values(ranging from 0 to 255) of the pixel is obtained, there is a simple formula to convert it to a gray scale value. Simply take ~30% of the Red Value,  ~59% of the Green value and ~11% of the blue value and add them up together to a value ranging from 0 to 255, 0 being the darkest and 255 being the brightest. Don't worry, the BufferedImage class has the necessary methods to obtain these values,which you can see in my annotated code posted below.This value is your gray scale value. Generally gray scale value range from 0 to 1 with decimals in between, but there is no reason for them to be between 0 and 255. You could divide it by 255 to range it from 0 to 1, but why the extra hassle? After obtaining the gray scale values, the creative part begins. As you know, some ASCII characters appear darker than the others. For example, # appears darker than 0.So what the program does is obtain the gray scale value of each pixel, and according to the value, append an ASCII character to the text file. For example, if the gray scale value is in between 180 and 200(very bright), it appends a ".", and if it is 50 and 70(very dark), it appends  a "#". Currently there are nine different ASCII characters that the program chooses from to append. However, I urge you to edit the code and experiment by adding more characters, changing the existing characters, etc. For a detailed explanation, read the comments in my code which explain the function and workings of each method. The results are stored in a file called result.txt, and when viewing the results in a text editor, choose a small bold mono-spaced font, with word wrapping DISABLED. I recommend choosing images not bigger than 600x600 as the task will become more and more resource intensive with bigger images. While the current program does have an area where the results are appended, for spectacular results, open up the result.txt file and choose the best font and size to view the art. I find that Prestige Std Elite and OCR A work really well, almost giving the illusion of colors. This program is only a test, and soon I will be releasing code to a program with better GUI, more options and ultimately more robust. But until then, here is a small video below which runs you through the code of the program and explains how the program works. 


And finally here are the links to the code of the program and the actual program and the Netbeans Project. Please go through the code, experiment with it and hope you learn something out of this.


This is not the end

Aravind Rao
                  

Tuesday, February 15, 2011

Java to the rescue!

Greetings

Some time back, I decided to run for the position of secretary in the student council of my school. Being a secretary appealed to me; I did not want a position that was constantly in the limelight and I enjoyed tasks such as data handling and processing (student data and volunteer info). I was subsequently elected and while reviewing the duties of  a secretary, I realized that there was no real system in place to store, view and edit member data. I found out that previous secretaries had left this responsibility to the members themselves; that is the reporting of the hours volunteered and so on. It did not seem right to me to allow volunteers to self report theirs hours. However it was seemingly the only option considering there was no concrete system in place. One of my fellow student council members had the "bright" idea to use an excel document; At 300+ members and over 100 activities per year, the excel cells to be edited would exceed 30000. There had to be another method. Finally, it was Java that came to my rescue! I decided to create a Java GUI Swing App combined with a self written text file parser. Every time a new member is added, the app creates a new text file with name, grade, etc as well as a volunteer hours summary, edited to look like a table. Adding hours to members files involves using the text file parser. which searches for special indexes and edits the file accordingly. Since the data is stored as text files, it can be printed out and handed back to members to increase transparency. I set out to code the app and in about a week finished it. It was a mad rush of coding ~ 440 lines of Java. All the codes are in a single file, though looking back in retrospect, I should have used interfaces and abstract classes for more readable code. However, it was the best I could do in a week! In any case, it helped me tremendously in processing members and their hours. I also built upon this code to build a simple payroll program for a local dentist's office. Although I cannot release that code until they allow me, here is a screen shot below:
My more complex payroll app screenshot.

As you can see, it is a pretty complex app with over 1250 lines of code. I shall be however posting links for the source code and entire project of my Student Council Secretary App as well as the .jar file. I have annotated key parts of code, but if you have any questions, please post a comment question on this post. When using the app, he member files are created in the same directory as that of the app. Like always, you can use, modify and redistribute it without any license. If you do redistribute it, It would be nice if you could add my name somewhere in the file, but if you cannot, it is not really a problem. Here are the links:
Link to download zipped Netbeans project (source code in the Hours-src-hours folder.)
Link to download .jar executable file. (Not tested on Mac, but should work. Works in Ubuntu and Windows)

This is not the end...

Aravind Rao