Java provides extensive support in its core API for working with images. Image processing is a vast area in its own right. Serious programmers in this domain use more than one tool/library, such as OpenCV , to work with images. Java, being a general purpose language, supports the essential features for common programming needs. However, the OpenCV library has its Java counterpart that can be effectively used for implementing more crude operations with the images. This article explores the key aspects of working with images using the core API library with a brief introduction on some background information.
As we talk of images in computing, they are of different types; the term colloquially mean graphics, pictures, images, logos, and so forth. This article makes no distinction and designates one and all of them as images; in fact, they are same from the programming point of view. The basic property of an image is defined by its dimension, resolution measured in pixels, and the coordinate system independent of the drawing surface. An image in Java is primarily an object of the Image class. It is a part of the java.awt package, along with many other auxiliary imaging classes and interfaces. These core APIs are used mainly for;
But, before delving into these processes, let’s get an idea of image file formats.
The information about the combination of color, pixels, and dimensions make an image file. They need to be stored in memory. As a result, the information has to be represented in an array of bits. An image file becomes quite large when represented in an simple array of bits. So, a mechanism is devised to compress them so that it not only reduces the file size but also retains visual quality. This is exactly what the encoding technique does. It arranges the bits in a manner that can be stored in a digital medium or transmitted easily in a network. Each encoding technique defines an image file format.
File formats are the data structure on how image information is encoded and stored in a repository. Understandably, these encoding techniques are quite complex and heavily depend upon complex mathematical formulation. Many of them are standardized and commonly used on the Web. The three most common file formats that we always encounter are JPEG (Joint Photographer Experts Group), GIF (Graphics Interchange Format), and PNG (Portable Network Graphics). There are many others, such as TIFF, Exif, BMP, and so on. Let’s stick to only three of them for now:
The most common class in the Java API that deals with images is java.awt. Image. Image is an abstract class, sub classed by BufferedImage and VolatileImage. Among these two, BufferedImage is most frequently used.
Figure 1 : Classes of images
Images are displayed using drawImage methods of the Graphics class.
** Extracted from Java API documentation
This method displays the images passed as a reference. Apart from the coordinates passed as a parameters, the method drawing an image is associated with the ImageObserver interface and Color class. The Color class encapsulates the colors in the RGB color space and an implicit alpha value that specifies the transparency. The ImageObserver interface receives notification as the image is being updated. This is particularly useful to show the progress of image update of a large image or while downloading image from a slow network connection.
As per the Java API documentation and Chet’s VolatileImage Q&A , the concrete subclass BufferedImage uses an accessible data buffer and relies on the image manipulation techniques defined by the methods of java.awt.image. Raster and the color characterization methods of java.awt.image. ColorModel. BufferedImage objects are allocated in an area that is in full control of the application. VolatileImage objects, on the other hand, are created in the limited system resource area, such as VRAM. Such limited resource areas are not in control of the application. As a result, the objects may be garbage collected as a result of optimizing limited resources or due to a proactive call of the flush method. So, the use of the VolatileImages object is unreliable or should be used cautiously. However, when the question of rendering performance arises, VolatileImage has the leverage of hardware rendering, whereas BufferedImage has to remain satisfied with the software rendering performance.
Images that we’ll be using in the code below are as follows.
Name: starburst.jpg
Size: 350×200
Figure 2 : Starburst image
Name: blob.jpg
Size: 50×50
Figure 3 : A blob
As with working with any basic file, images are loaded from an external file stored in a digital medium or an URL from the network; then, an image is created and finally saved. The following code will show how to do it in a simple manner.
Images need to loaded or created in a GUI component. We’ll be using java.swing. JPanel as a container for our images. The paint method of this component can be overloaded to create the image. The paint method is automatically invoked to render the component on-screen. It takes a Graphics object as a parameter. This object reference is used to create the image.
Here, we’ll be loading two images superimposing one on other, write some text, and finally save the image as a jpg file onto the disk.
Figure 4 : Starburst image with control buttons and the blob
The imaging classes provided in this are article are just tip of the iceberg. The core API library has several other classes that can be used to have a better control over imaging process and support advanced imaging techniques. Some of the classes are described in package javax.imageio. Other classes such as MediaTracker which enables checking the status of arbitrary number of images in parallel, interfaces such as ImageProducer , ImageConsumer to work with image data, ImageFilter objects can be used to manipulate pixels in the images. In a nutshell, the core API library along with a third party library such as OpenCV can provide almost every need of an image processing programmer.
© Source: http://www.developer.com/java/data/working-with-images-in-java.html
All rights are reserved and belongs to a source media.