Use of Image I/O library

This section illustrates you how to use Image I/O library.
The ImageIO class provides the method to read and write image. We are providing you an example which copies the specified input file into the
output file. A file image4.jpg is defined as input file which is read by the method read()
of ImageIO class and returns a buffered image after decoding the file.
A matrix is defined in the variable data of float type. This matrix is
defined by the class Kernel to describe how a pixel affect its position in the output image of a filtering
operation. The class ConvolveOp provides a correlation from the
source to the destination. The method filter(input, output) of class ConvolveOp
performs a translation on BufferedImages.
The method write(output, "GIF", outputFile) writes into the
output file logo.gif.
Here is the code of CopyImage.java
import java.io.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
public class CopyImage {
private static final float[] data = { 0.0f, -1.0f, 0.0f, -1.0f, 5.0f,
-1.0f, 0.0f, -1.0f, 0.0f };
public static void main(String args[]) throws IOException {
File fileIn = new File("image4.jpg");
BufferedImage bufferedImage1 = ImageIO.read(fileIn);
Kernel kernel = new Kernel(3, 3, data);
ConvolveOp convolveOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP,
null);
int width = bufferedImage1.getWidth();
int height = bufferedImage1.getHeight();
BufferedImage bufferedImage2 = new BufferedImage(width, height,
BufferedImage.TYPE_INT_ARGB);
convolveOp.filter(bufferedImage1, bufferedImage2);
File fileOut = new File("logo.gif");
ImageIO.write(bufferedImage2, "GIF", fileOut);
}
}
|
When you run the above program, the input file is copied into the output file
specified.
Download Source Code

|