There are three ways to do image I/O in Java (that is, to read pictures and write pictures, without involving complex image processing):
1. The Java image I/O API, which supports common images, has been built from Java 2 version 1.4.0.
Home: http://java.sun.com/javase/6/docs/technotes/guides/imageio/index.html
2. Image I/O Tools in JAI, support for more picture types such as Jpeg-ls, JPEG2000, and TIFF.
Home: https://jai-imageio.dev.java.net/. JAI is a framework for image processing, very large,
The only Jai-imageio is the image I/O, the other can not look.
3. Jai's com.sun.media.jai.codec also has a certain image decoding ability
Of course, there are many Java open-source toolkits that can read and write images, such as Jimi, Jmagic, etc., but the JDK can now
Enough to read and write images, it is easy to develop and deploy with JDK, no additional download of jar package is required.
Since Jai is a new addition to Java, many components are not formally regulated and the JDK is not self-bringing, so development and deployment require additional
installation, installation files on the official website https://jai.dev.java.net/download get.
If you just want to read a picture of a common format, you don't need to use jai such a big thing,
With the Java Image I/O API.
The following highlights the Java Image I/O API.
The Java Image I/O API is mainly under Javax.imageio. The JDK has built-in plugins for common image formats,
But it provides a plug-in architecture, and third parties can also develop plug-ins to support other image formats.
The following code shows the supported image formats built into the JDK.
Import javax.imageio.*;
Import Java.util.Arrays;
public class HelloWorld {
public static void Main (String args[]) {
String readformats[] = Imageio.getreaderformatnames ();
String writeformats[] = Imageio.getwriterformatnames ();
System.out.println ("Readers:" + arrays.aslist (readformats));
System.out.println ("Writers:" + arrays.aslist (writeformats));
}
}
The home page has a document,Java Image I/O APIGuide, which is easy to understand and lets you get started quickly. Following
The content is mainly from the 3rd chapter of this document.
The 3rd chapter writes the image I/O program
3.1 Reading and writing pictures
The Javax.imageio.ImageIO class provides a set of static methods for the simplest image I/O operations.
Reading a picture of a standard format (GIF, PNG, or JPEG) is simple:
File F = new file ("C:\images\myimage.gif");
BufferedImage bi = imageio.read (f);
The Java image I/O API automatically detects the format of the image and calls the corresponding plug-in to decode it, when a new
Plugin, the new format will be automatically understood, the program code does not need to change.
Writing pictures is also simple:
BufferedImage bi;
File F = new file ("C:\images\myimage.png");
Imageio.write (IM, "png", f);
3.2 Further
The method mentioned in the previous section is sufficient for a simple program. However, the Java Image I/o API provides a copy of the
The ability to process miscellaneous programs. To take advantage of the advanced features of the API, applications should directly use the class ImageReader and
ImageWriter.
3.3 ImageReader Class
Instead of using the ImageIO class to perform all the decoding operations, it is better to use the ImageIO class to get a ImageReader object,
Then use this object to read the operation:
Iterator readers = imageio.getimagereadersbyformatname ("gif");
ImageReader reader = (imagereader) readers.next ();
ImageReader objects can also be obtained based on file content, file suffixes, or MIME types. This is used to find and initialize
The mechanism of ImageReader objects is used in the Javax.imageio.spi.ImageReaderSpi class, which can be
Gets the plugin information when the plugin is started. "Service provider Interfaces" (SPIs) will be next
discussed in detail in the chapter. Once a ImageReader object has been obtained, it must be referred to as an input source. Most
The ImageReader object can read data from the Imageinputstream class input source, Imageinputstream is an image
The dedicated input source defined by the I/O API.
Getting a imageinputstream is simple. Given a file or InputStream, a
The Imageinputstream object can be generated by invoking the following function:
Object source; File or InputStream
Imageinputstream IIS = Imageio.createimageinputstream (source);
Once you have an input source, you can associate it with a ImageReader object:
Reader.setinput (IIS, True);
If the input source file contains more than one picture, and the program is not guaranteed to read sequentially, the second parameter should be set to
False For those file formats that allow only one picture to be stored, it is reasonable to pass true forever.
When the ImageReader object has an input source, we can get the picture information without having to read the entire image data.
Memory. For example, calling Reader.getimagewidth (0) allows us to get the width of the first image in the file. One
A good plugin will try to decode the necessary parts of the file to get the width of the image without having to read any one pixel.
To read a picture, you can call Reader.read (ImageIndex), ImageIndex is a file (when more than one picture is included)
The index of the picture in the. This is the same as the result of calling Imageio.read () from the previous section.
3.3.1 Imagereadparam
If more control is required, you can pass a Imagereadparam type parameter to the read () method. One
The Imagereadparam object allows the program to make better use of memory. It not only allows you to specify an area of interest, but also
You can specify a sampling factor for downward sampling.
For example, to decode only 1/4 of the upper-left corner of the picture, the program can get a suitable Imagereadparam object first:
Imagereadparam param = Reader.getdefaultreadparam ();
Next, specify the picture area:
Import Java.awt.Rectangle;
int imageindex = 0;
int half_width = reader.getimagewidth (imageindex)/2;
int half_height = reader.getimageheight (imageindex)/2;
Rectangle rect = new Rectangle (0, 0, half_width, half_height);
Param.setsourceregion (rect);
Finally, read the picture:
BufferedImage bi = reader.read (imageindex, param);
The result is a new picture, which is only half the width and height of the original picture.
Another example, in order to read one of every three pixels, produces a picture of the original image 1/9 size, which can be used
Imagereadparam Specify the sampling factor:
param = Reader.getdefaultimageparam ();
Param.setsourcesubsampling (3, 3, 0, 0);
BufferedImage Bi3 = reader.read (0, param);
3.3.2 Iioparamcontroller
Plugins sometimes provide a iioparamcontroller class, which is optional. Slightly.
3.3.3 Read multiple picture files
All methods of dealing with pictures in ImageReader have a imageindex parameter, which is used to read multiple
A picture in a file.
Imagereader.getnumimages () returns the number of pictures in a multiple picture file. This method has a Boolean parameter,
Allowsearch. Some image formats, typical GIF, do not provide any way to get the number of pictures in the file, except
The entire non-read is parsed. This is expensive, so setting Allowsearch to False allows the method to return directly
-1, not the actual number of pictures. If this parameter is true, the method always returns the actual picture in the file
Number.
You can call read (ImageIndex) Even if you do not know the number of pictures in the file. If the index value is too large,
This method throws a Indexoutofboundsexception exception. Therefore, the program can increment the index to get the picture,
Until the exception.
3.3.4 Read thumbnail image
Some picture formats allow one (or more) small previews to be stored with the main picture in the file. These
Thumbnails are useful for quickly identifying images without having to decode the entire image.
The program can invoke the following code to detect how many thumbnails a picture has:
Reader.getnumthumbnails (ImageIndex);
If there is a thumbnail, you can call the following code to get it:
int thumbailindex = 0;
BufferedImage bi;
Bi = Reader.readthumbnail (ImageIndex, Thumbnailindex);
3.4 ImageWriter Class
Just as we can get a ImageReader object of some kind of image format using a ImageIO method, we can also
To get the ImageWriter object:
Iterator writers = Imageio.getimagewritersbyformatname ("png");
ImageWriter writer = (ImageWriter) writers.next ();
Once a ImageWriter object has been acquired, it must be set to an output source of Imageoutputstream.
File F = new file ("C:\images\myimage.png");
Imageoutputstream iOS = Imageio.createimageoutputstream (f);
Writer.setoutput (iOS);
Finally, you can write the picture to the output source:
BufferedImage bi;
Writer.write (BI);
3.4.1 Write multiple picture files
The Iioimage class is used to store references to pictures, thumbnails, or meta information. The next section will discuss metadata, and at present, we
Simply pass NULL to the metadata correlation parameter.
The ImageWriter class has a method write () that is used to create a new file from Iioimage, and there is a method
Writeinsert (), which is used to add an Iioimage object to an existing file. By calling these two, you can create a
Build a multi-image file:
BufferedImage First_bi, Second_bi;
Iioimage first_iioimage = new Iioimage (FIRST_BI, NULL, NULL);
Iioimage second_iioimage = new Iioimage (SECOND_BI, NULL, NULL);
Writer.write (NULL, first_iioimage, NULL);
if (Writer.caninsertimage (1)) {
Writer.writeinsert (1, second_iioimage, NULL);
} else {
System.err.println ("Writer can ' t append a second image!");
}
3.5 Handling Metadata
All pixel-independent information belongs to the metadata. Javax.imageio.metadata contains access to the
The metadata class and interface.
The image I/O API treats stream metadata and image metadata differently. Stream metadata with a
More than one picture is stored in the file, image metadata is only relevant to a single picture. If a file contains only a single
Image, then there is only image metadata.
You can call Imagereader.getstreammetadata and getimagemetadata (int imageindex) to
Gets the metadata. These methods return an object that implements the Iiometadata interface, and the object is converted Upward
is a imagereader type,
3.6 Encoding Conversion
Slightly
3.7 Event Monitoring
Slightly
This article transferred from: http://blog.csdn.net/perfect2011/article/details/6874287
Java Read and write images