Image to ASCII art application was not that much hard to implement. Now we will talk about the implementation. Since I'm going to cover the basics, here I will describe even the very basic things. In a nutshell, the application will open an image file and do some processing and then that text file will be saved to somewhere. If the user needs to preview it before saving the text file, he/she can also do it. Here I will describe the application functionality as questions (in a new way ;)) )
1. How to open an Image in java?
"javax.ImageIO" class provides all the facilities to read an image to an application. We can use it to read an image as a BufferedImage to our application. In java2D API images are mainly represented in two ways. Either as an Image ("java.awt.Image") or as an Buffered Image (java.awt.image.BufferedImage"), which is a descended class of image and which provides more options for image manipulations.
BufferedImage img = ImageIO.read(new File(fileChooser.getSelectedFile().getAbsolutePath()));Here the purpose of "new File(fileChooser.getSelectedFile().getAbsolutePath())" is to create a new File (java.io.File) which is pointing to image that we want to open. Here I also have used a File Cooser to browse for files. I will explain how to use a JFileCooser now.
2. How to use a JFileCooser?
"javax.swing.JFileChooser is ideal for opening and saving files. The most important point here we have to understand is "JFileChooser cannot open or save a files" :)) . It can only point a file. The saving or opening part is up to us to handle. When a JFileCooser (either to open a file or to save a one) is shown a user can either select a file or press cancel. If user doesn't cancel we have to handle that situation. We can identify that by a return value after calling the JFileChooser.showOpenDialog() method or JFileChooser.showSaveDialog(). That is shown in the code below.
int userChoice = fileChooser.showOpenDialog(FileChooserDemo.this); if (userChoice == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); //Open the file code goes here } else { //Do nothing user has press cancel }
Here is the code I'm using in my application to open and show a image. Note that some error handling are also included. Here I'm also using a file filter with the file chooser which I'm going to explain next.
//open the file choose and show the selected file fileChooser = new JFileChooser(); //add filters to the file chooser - to only show the image files fileChooser.addChoosableFileFilter(new ImageFilter()); fileChooser.setAcceptAllFileFilterUsed(false); int userChoice = fileChooser.showOpenDialog(ImageLoader.this); if( userChoice == JFileChooser.APPROVE_OPTION ){ try{ txtImagePath.setText(fileChooser.getSelectedFile().getAbsolutePath()); BufferedImage img = ImageIO.read(new File(fileChooser.getSelectedFile().getAbsolutePath())); currentImg = img; //display the image using an image icon ImageIcon icon = new ImageIcon(img); lblImage.setIcon(icon); }catch(Exception e){ JOptionPane.showMessageDialog(ImageLoader.this, "Invalid file!"); } }else{ JOptionPane.showMessageDialog(ImageLoader.this, "Please select a file!"); }
3. Howto use filters with a JFileChooser?
Did you notice the below code lines in the above code? Those are for some filtering mechanism.
fileChooser.addChoosableFileFilter(new ImageFilter()); fileChooser.setAcceptAllFileFilterUsed(false);The purpose of filtering is to filter out some of file extensions and show what we are interested, as it name implies "Filters" ;). Here I'm only going to show image files and folders. Using a filter is not a must. But it is a good practice to do so. Above first line initiate the filter and the second line disable the default settings of the JFileChooser. (note - I will host the the complete project in Google code and provide the link to you later in a upcoming post). Please read the up to now section to get an overall idea what we had done up to now.
import java.io.File; import javax.swing.filechooser.FileFilter; public class ImageFilter extends FileFilter { //Accept all directories and all gif, jpg, or png files. public boolean accept(File f) { if (f.isDirectory()) { return true; } String extension = getExtension(f); if (extension != null) { if (extension.equals("gif") || extension.equals("jpg") || extension.equals("png")) { return true; } else { return false; } } return false; } //The description of this filter public String getDescription() { return "Images"; } //find the extension of the file name public String getExtension(File f) { String ext = null; String s = f.getName(); int i = s.lastIndexOf('.'); if (i > 0 && i < s.length() - 1) { ext = s.substring(i+1).toLowerCase(); } return ext; } }Up to Now!
Up to now we talk about the things we need to open a image file into our application. How to read the file and how to browse for it etc. In my next post I will explain you how to manipulate the image we opened and etc. Before that I will Host the complete source code, then it will be very easy for you... Bye..!! :))
No comments:
Post a Comment