Image processor (Java)

From LiteratePrograms

Jump to: navigation, search

Contents

Image Processor

This is a java program that performs graphical operations on an image. These operations are:

1)Reset the image

2)Invert the image colours

3)Gamma correction

4)Applying custom filters to the image

5)Convolution

6)Blue Fade

Using a GUI, users can select any image they wish to use.

For this program to work, users should save the image below to the directory that the source code is saved in.

Image:Raytrace.jpg

This is a good program to learn java with as it introduces how to produce graphical user interfaces. It also demonstrates how the extensive java API can be implemented to the programmers benefit.

Defining the class

It is essential to define a class at the beginning of a java program, listing all the library classes that the program will use and defining any initial variables or fields. The class definition is below:

<<Defining the class>>=
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;
public class ImageProcessor extends JFrame {
    JButton select_image_button, invert_button, gamma_button, 
convolve_button, reset_button,    slow_gamma_button, fast_gamma_button;
    JButton box_3x3_button, box_5x5_button, box_6x4_button, 
gaussian_3x3_button, high_3x3_button, select_file_button;
    JButton custom_filter_button, select_size_button, execute_button;
    JTextField gamma_value_height, width_field;
    JTextField[][] filter_array;
    JLabel info_label, height_label, width_label, gamma_label, image_icon;
    BufferedImage image, orig_image;
    JFileChooser file_chooser;
    JTable filter_table;
    Container container;
    JPanel control_container, menu_container, option_container, 
top_container, bottom_container;
    int height = 1, width = 1;
    private final String INITIAL_IMAGE_FILE = "raytrace.jpg";
    class body
}
<<class body>>=
Constructor
Event Handler
Return Pointer to Array
Reset
Invert
Slow Gamma
Fast Gamma
File Filters
Creating Custom Filter
Convolution
Blue fade
Main

Because this program creates a graphical user interface (GUI), it requires alot of import statements to import and use java library classes in its implementation. What each class does and how thier characteristics are described below:

java.awt.*- Contains all of the classes for creating user interfaces and for painting graphics and images.

java.awt.image- An abstract class, image is the superclass of all classes that represent graphical images.

java.io.*- Used for system input and output through data streams, serialization and the file system.

javax.imageio.*- Provides a pluggable architecture for working with images stored in files and accessed across the network.

javax.swing.*- Provides a set of lightweight GUI components that, to the maximum degree possible, work the same on all platforms.

java.awt.event.*- Provides interfaces and classes for dealing with different types of events created by AWT components.

java.util.ArrayList- Provides access to a library class which can store an arbitrary number of elements, with each element being another object.

javax.swing.table.DefaultTableModel- The TableModel interface specifies the methods the JTable will use to interrogate a tabular data model, this library class is an implementation of TableModel which uses a Vector of Vectors to store the cell value objects.

When naming a class it is important to name it something that identifies what it does or how it behaves. In this case the class is called ImageProcessor because it processes images in a certain way.

Notice the code extends JFrame. This indicates ImageProcessor is a subclass of the library class Jframe. Essentially, this means ImageProcessor is a Jframe. It inherits all the methods and behaviour of Jframe but adds some of its own functionality, which will be explained, along with the methods that provide this functionality.

The body of the class definition is taken up by defining the various components that are used to create the GUI. These components are described below, organised in groups of thier component type.

Component Type: JButton A button component that can cause an event to occur when the user clicks on it.

Component Names:The select_image_button allows users to select an image of thier choice from a certain location. The invert_button invers the values of the image. The gamma_button allows the user to perform gamma correction on the image. The convolve_button allows users to apply convolution to the image.The reset_button resets the image to its original state. The slow_gamma_button performs gamma correction at a slow rate. The fast_gamma_button performs gamma correction at a fast rate. The box_3x3_button allows the user to apply a 3x3 blur filter to the image. The box_5x5_button allows the user to apply a 5x5 blur filter to the image. The box_6x4_button button allows the user to apply a 6x4 blur filter to the image. The gaussian_3x3_button button allows the user to apply a 3x3 gaussian blur to the image. The high_3x3_button button applies a high pass filter to the image. The select_file_button allows users to select a file of thier choice. The filter_button allows users to choose a filter of thier choice. When the user wishes to use a filter of thier choice they must select a filter size. The select_size_button allows them to do this. Once they have selected a filter size and input values, the execute_button applies the filter.

Component Type: JTextField. This is a text area on the screen that users can input values into.

Component Names:The gamma_value_height field allows users to enter a value to be used in the gamma correction, as does width_field. Note that the filter_array is of type JTextField [] []. This means it is a two dimensional array. The purpose of this is to store information that will become apparant later in the literate program.

Component Type: JLabel. This is used to display certain information on the GUI.

Component Names: info_label, height_label, width_label, gamma_label, image_icon

Component Type: BufferedImage. This is used to display images in the GUI.

Component Names: image is used to represent the image that the operations are going to be performed on. When the reset operation is performed orig_image is used to return the image to its original form.

Component Type: JFileChooser. This is a box that allows the user to select a file from a location thier computer.

Component Name: file_chooser is used when the user wishes to select a file.

Component Type: JTable. This is a table that can be used in the GUI.

Component Name: filter_table. This is used when the user wishes to apply a unique filter to the image.

Component Type: Container. This is used to contain all the components that have been specified for the GUI.

Component Name: container. When the GUI has been created this will be used to manage thier layout.

Component Type: JPanel. This is used when a number of components need to be grouped together.

Component Name: control_container, menu_container, option_container, top_container, bottom_container

The height and width of the filter are also defined and initialised to 1 in the body of the class definition.

Constructor

After defining all the components and fields that are going to be used in the program we must create instances of them so they can be used. This is achieved in the constructor of the class.

<<Constructor>>=
public void ImageProcessor() throws IOException {
        container = getContentPane();
        container.setLayout(new BorderLayout());
        File image_file = new File(INITIAL_IMAGE_FILE);
        if (image_file.exists())
        {
           image = ImageIO.read(image_file);
           orig_image = ImageIO.read(image_file);
           image_icon = new JLabel(new ImageIcon(image)); 
        } 
        else
        {
           image_icon=new JLabel(new ImageIcon());
        }
        container.add(image_icon, BorderLayout.CENTER);
        control_container = new JPanel(new BorderLayout());
        container.add(control_container, BorderLayout.SOUTH);
        info_label = new JLabel("Ready");
        control_container.add(info_label, BorderLayout.NORTH);
        menu_container = new JPanel(new GridLayout(5, 1));
        control_container.add(menu_container, BorderLayout.WEST);
        select_image_button = new JButton("Select Image");
        menu_container.add(select_image_button);
        invert_button = new JButton("Invert");
        menu_container.add(invert_button);
        gamma_button = new JButton("Gamma");
        menu_container.add(gamma_button);
        convolve_button = new JButton("Convolve");
        menu_container.add(convolve_button);
        reset_button = new JButton("Reset");
        menu_container.add(reset_button);
        option_container = new JPanel(new BorderLayout());
        control_container.add(option_container, BorderLayout.CENTER);
        top_container = new JPanel(new FlowLayout());
        option_container.add(top_container, BorderLayout.NORTH);
        gamma_value_height = new JTextField(6);
        gamma_label = new JLabel("Gamma Value:");
        height_label = new JLabel("Height:");
        slow_gamma_button = new JButton("Slow Gamma");
        fast_gamma_button = new JButton("Fast Gamma");
        box_3x3_button = new JButton("3x3 Box");
        box_5x5_button = new JButton("5x5 Box");
        box_6x4_button = new JButton("6x4 Box");
        gaussian_3x3_button = new JButton("3x3 Gaussian Blur");
        high_3x3_button = new JButton("3x3 High Pass")<font color="#444