Hi...!! In my previous post I discussed about opening an image using java. Now it is time to work with the image. Java 2D has two classes to represent images. java.awt.image and java.awt.image.BufferedImage. Buffered image is the most flexible one for some processing. Because it allow us to work on the image data.
Here what I'm going to do first is to open the image. Then I will convert the image in to a gray scale image with user defined dimensions. There we will have to do two things.
1. How to change a color image to a gray scale image using java 2D?
2. How to change the dimensions of an image?
Both the questions will answered in the below line of codes. There first I will create a new gray scale Buffered image (answer to the question one) with custom dimensions (answer to the question two). After that I will draw the current image on the newly created image.
BufferedImage imgEdted = new BufferedImage(width,height,BufferedImage.TYPE_BYTE_GRAY); Graphics graphicEdted = imgEdted.getGraphics(); graphicEdted.drawImage(imgIn, 0, 0, width, height, 0, 0, w, h, null);Here the image imgEdited is the new image and imgIn is the original image (The full source code is shown below). First we will get the graphic object of the newly created image (graphicEdted) and we will use it to draw the original image on the new image. Here width and height are the custom widths and heights and w and h are the width and height of the original image. The "drawImage()" method do the trick here. It convert the color and also re scale the image. Here are some of the very important usages of this method.
http://download.oracle.com/javase/tutorial/2d/images/drawimage.html
http://download.oracle.com/javase/1.4.2/docs/api/java/awt/Graphics.html
After the new gray-scale image is created we will use a multilevel threshold to create our ASCII image. For that purpose we have to go through each and every pixel of the image. For the simplicity I will use read the color value of the pixel and determine it group. That is how we are going to do the multilevel threshold. For each group of color value I will assign a letter (an ASCII) that will eventually produce a ASCII image. The quality of the output image will depend on two facts. The first one is the number of groups and the other is the letter we assign for a color group.
Because I need to create an ASCII image I will collect the ASCII letter for each pixel to a string buffer. After a row of pixel in the image I will append a line break to the string buffer.
Below code is showing the complete source code for the section I above discussed. It shows a method which take a image (buffered image) and a user defined width and height to create an ASCII image of user defined size.
package img2ascii.core; import java.awt.Graphics; import java.awt.image.BufferedImage; public class AsciiCreator { public static StringBuffer getASCII(BufferedImage imgIn, int width,int height){ //change the color mode - and reset the size as we need int w = imgIn.getWidth(null); int h = imgIn.getHeight(null); BufferedImage imgEdted = new BufferedImage(width,height,BufferedImage.TYPE_BYTE_GRAY); Graphics graphicEdted = imgEdted.getGraphics(); graphicEdted.drawImage(imgIn, 0, 0, width, height, 0, 0, w, h, null); //create ASCII image by multilevel threshloding and save the result to a String buffer StringBuffer asciiBuffer = new StringBuffer(); for(int i=0;i230){ System.out.print("`"); asciiBuffer.append("`"); }else{ if(color>200){ System.out.print("'"); asciiBuffer.append("'"); }else{ if(color>160){ System.out.print(";"); asciiBuffer.append(";"); }else{ if(color>120){ System.out.print("O"); asciiBuffer.append("O"); }else{ if(color>80){ System.out.print("8"); asciiBuffer.append("8"); }else{ if(color>40){ System.out.print("N"); asciiBuffer.append("N"); }else{ System.out.print("M"); asciiBuffer.append("M"); } } } } } } } System.out.println(""); asciiBuffer.append(System.getProperty("line.separator")); } return asciiBuffer; } }
No comments:
Post a Comment