There are three ways to do image I/O in Java (that is, read pictures and write pictures, and do not involve complex image processing):
The Java image I/O API, which supports common images, is built from Java 2 version 1.4.0. Home: http://java.sun.com/javase/6/docs/technotes/guides/imageio/index.html
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, where only Jai-imageio is about image I/O, others can not see.
Jai's Com.sun.media.jai.codec also has a certain image decoding ability
Of course, there are many Java Open Source Toolkit can read and write images, such as Jimi, Jmagic, etc., but the JDK is currently able to read and write pictures, with JDK, development and deployment of convenient, no need to download the additional jar package.
Since Jai is a new addition to Java, many components are not formal specifications and JDK are not self-contained, so development and deployment requires additional installation, and installation files are downloaded from the website https://jai.dev.java.net/.
If you just want to read the common format of the picture, do not need to use Jai so high such a large thing, with the Java Image I/O API.
The Java Image I/O APIs are highlighted below.
The Java Image I/O API is mainly under Javax.imageio. The JDK has built-in plugins for common picture formats, but it provides a plug-in architecture, and third parties can also develop plug-ins to support other picture formats.
The following code shows a built-in image format supported by the JDK.
Import javax.imageio.*;
Import Java.util.Arrays;
public class HelloWorld {public
static void main (string args[]) {
string readformats[] = Imageio.getreaderformat Names ();
String writeformats[] = Imageio.getwriterformatnames ();
System.out.println ("Readers: " + arrays.aslist (readformats));
System.out.println ("Writers: " + arrays.aslist (writeformats));
}
There is a document on the home page, Java Image I/O API Guide, very easy to understand, can let you quickly start. The following are mainly from chapter 3rd of this document.
The 3rd chapter writes the image I/O program
# #3.1 Read and write 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:imagesmyimage.gif");
BufferedImage bi = imageio.read (f);
The Java image I/O API automatically detects the format of the picture and invokes the corresponding plugin for decoding, and when a new plugin is installed, the new format is automatically understood and the program code does not need to be changed.
Writing pictures is also simple:
BufferedImage bi;
File F = new file ("C:imagesmyimage.png");
Imageio.write (IM, "png", f);
3.2 Further
The method discussed in the previous section is sufficient for simple programs. However, the Java Image I/O API provides the ability to write complex programs. To take advantage of the advanced nature of the API, applications should use class ImageReader and imagewriter directly.
3.3 ImageReader Class
Rather than using the ImageIO class for all decoding operations, it is better to use the ImageIO class to get a ImageReader object, and then use this object to read the operation:
Iterator readers = imageio.getimagereadersbyformatname ("gif");
ImageReader reader = (imagereader) readers.next ();
The ImageReader object can also be obtained based on the file content, file suffix, or MIME type. The mechanism used to find and initialize the ImageReader object uses the Javax.imageio.spi.ImageReaderSpi class, which can obtain information about Plug-ins without initializing plug-ins. "Service provider Interfaces" (SPIS) will be discussed in detail in the next chapter. Once a ImageReader object is obtained, it must refer to an input source. Most ImageReader objects can read data from the Imageinputstream class input source, Imageinputstream is a dedicated input source defined by the image I/O API.
Getting a imageinputstream is simple. Given a file or InputStream, a Imageinputstream object can be generated by calling 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 does not guarantee sequential reads, the second argument should be set to false. For those file formats that allow only one picture to be stored, it is reasonable to always pass true. When the ImageReader object has an input source, we can get the picture information without having to read the entire picture data into memory. For example, calling Reader.getimagewidth (0) allows us to get the width of the first picture in the file. A good plugin will try to decode the necessary parts of the file to get the width of the picture without having to read any one pixel.
To read a picture, you can call Reader.read (ImageIndex), ImageIndex is the index of the picture in the file (when more than one picture is included). This is the same result that was generated by the call to Imageio.read () in the previous section.
3.3.1 Imagereadparam
If more control is required, you can pass a Imagereadparam type parameter to the read () method. A Imagereadparam object allows the program to make better use of memory. Not only does it allow you to specify an area of interest, you can also specify a sampling factor for sampling down.
For example, to decode only 1/4 of the upper-left corner of a picture, the program can first get a suitable Imagereadparam object:
Reader.setinput (IIS, True);
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, and the width and height are only half the original picture.
Another example, in order to read one of each three pixels, to produce a picture of the original picture 1/9 size, you can use Imagereadparam to specify the sampling factor:
param = Reader.getdefaultimageparam ();
Param.setsourcesubsampling (3, 3, 0, 0);
BufferedImage Bi3 = reader.read (0, param);
3.3.2 Iioparamcontroller
Plug-ins sometimes provide a Iioparamcontroller class, which is optional. Slightly.
3.3.3 Read multiple picture files
All the methods used to deal with pictures in ImageReader have a imageindex parameter, which reads one of the multiple image files.
Imagereader.getnumimages () returns the number of pictures in a multiple-picture file. This method has a Boolean parameter, Allowsearch. Some picture formats, typically GIF, do not provide any way to get the number of pictures in a file unless the entire parse is read. This is expensive, so setting Allowsearch to False allows the method to return directly-1 instead of the actual number of pictures. If this argument is true, the method always returns the actual number of pictures in the file.
Even if you do not know the number of pictures in the file, you can still call read (IMAGEINDEX); If the index value is too large, the method throws a Indexoutofboundsexception exception. Therefore, the program can increment the index to get the picture until the exception.
3.3.4 Read thumbnails
Some picture formats allow one (or more) small previews to be stored with the main picture in a file. These "thumbnails" are useful for quickly identifying pictures without decoding the entire picture.
The program can invoke the following code to detect how many thumbnails a picture has: Reader.getnumthumbnails (imageindex);
If thumbnails exist, you can invoke the following code to obtain:
int thumbailindex = 0;
BufferedImage bi;
Bi = Reader.readthumbnail (ImageIndex, thumbnailindex);
3.4 ImageWriter Class
Just as we can get a ImageReader object of a picture format using a ImageIO method, we can also 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 imageoutputstream.
File F = new file ("C:imagesmyimage.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. Metadata is discussed in the next section, and for the moment we simply pass NULL to the metadata correlation parameter. The ImageWriter class has a method of write (), which is used to create a new file from Iioimage, and a Method Writeinsert () to add a Iioimage object to an existing file. By calling both, you can create a multiple-picture 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 Processing Metadata
All information that is not pixel-independent belongs to the metadata. The javax.imageio.metadata contains classes and interfaces for accessing metadata.
The image I/O API treats stream metadata and image metadata differently. The stream metadata is associated with more than one picture stored in a file, and image metadata is only related to a single picture. If a file contains only one picture, then only image metadata exists.
You can get metadata by calling Imagereader.getstreammetadata and getimagemetadata (int imageindex). These methods return an object that implements the Iiometadata interface, which is converted up to the ImageReader type.
3.6 Code Conversion
Slightly
3.7 Event Monitoring
Slightly
3.8 Cases
Package com.test.image;
Import Java.awt.Rectangle;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import java.io.IOException;
Import Java.util.Iterator;
Import Javax.imageio.ImageIO;
Import Javax.imageio.ImageReadParam;
Import Javax.imageio.ImageReader;
Import Javax.imageio.ImageWriter;
Import Javax.imageio.stream.ImageInputStream;
Import Javax.imageio.stream.ImageOutputStream; /** * Read/write pictures * @author TAOWW * Reference: http://www.360doc.com/content/16/0224/16/12500151_537069462.shtml * */public class R
wimage {public static void main (string[] args) {//simplerwimage ();
Complexrwimage (); /** * Read and write picture simple mode */public static void Simplerwimage () {try {//Read picture file F1 = new file ("C:\\users\\publ
Ic\\pictures\\sample pictures\\chrysanthemum.jpg ");
BufferedImage Bi=imageio.read (F1);
Writes the read picture to the local file F2 = new file ("C:\\users\\public\\pictures\\sample pictures\\lihua.png");
if (!f2.exists ()) {f2.createnewfile (); Imageio.write (BI, PNG),F2);
catch (IOException e) {e.printstacktrace ();
The/** * Java Image I/O API provides the ability to write complex programs. * To take advantage of the advanced features of the API, applications should directly use class ImageReader and ImageWriter read/write Pictures/public static void Complexrwimage () {try {/************ Read the picture *********************************/file F = new file ("C:\\users\\public\\pictures\\sample pictures\\
Chrysanthemum.jpg ");
Iterator readers = imageio.getimagereadersbyformatname ("jpg");
ImageReader reader = (imagereader) readers.next ();
* * Gets a ImageReader object that must be referred to as an input source. * Most ImageReader objects can read data from the Imageinputstream class input source, * Imageinputstream is a dedicated input source defined by the image I/O API * * Imageinputstream IIS
= Imageio.createimageinputstream (f);
* * Once you have an input source, you can associate it with a ImageReader object.
* If the input source file contains more than one picture, and the program does not guarantee sequential reads, the second argument should be set to false.
* For those file formats that allow only one picture to be stored, it is reasonable to always pass true */Reader.setinput (IIS, True);
* * * If more control is required, you can pass a Imagereadparam type parameter to the read () method.
* A Imagereadparam object allows the program to make better use of memory. * It not only allows you to specify an area of interest, you can also specify a sampleFactor that is used to sample down.
* * */Imagereadparam Param=reader.getdefaultreadparam ();
int imageindex=0;
int Halfwidth=reader.getwidth (IMAGEINDEX)/2;
int Halfhegiht=reader.getheight (IMAGEINDEX)/2;
Rectangle rectangle=new Rectangle (0,0,HALFWIDTH,HALFHEGIHT);
Param.setsourceregion (Rectangle);
BufferedImage bi=reader.read (0, param); /********************** Write pictures *********************************/iterator Writes=imageio.getimagewritersbyformatname
("PNG");
ImageWriter imagewriter= (ImageWriter) writes.next ();
F=new File ("C:\\users\\public\\pictures\\sample pictures\\lihua.png");
Imageoutputstream Iops=imageio.createimageoutputstream (f);
Imagewriter.setoutput (IOPS);
Imagewriter.write (BI);
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
}
}
}
Reference Address: http://www.360doc.com/content/16/0224/16/12500151_537069462.shtml