Javax. imageio
You can use static methods of the ImageIO class to perform many common image I/O operations.
This package contains some basic classes and interfaces, and some are used to describe the image file content (including metadata and thumbnails) (IIOImage );
Some are used to control the image reading process (ImageReader, ImageReadParam) and image writing process (ImageWriter and ImageWriteParam );
[Java]
Package com. mapbar. graphics;
Import java. awt. Graphics;
Import java. awt. Image;
Import java. awt. Rectangle;
Import java. awt. image. BufferedImage;
Import java. io. File;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. util. Arrays;
Import java. util. Iterator;
Import javax. imageio. ImageIO;
Import javax. imageio. ImageReadParam;
Import javax. imageio. ImageReader;
Import javax. imageio. metadata. IIOMetadata;
Import javax. imageio. stream. ImageInputStream;
Import com.sun.image.codec.jpeg. Unzip codec;
Import com.sun.image.codec.jpeg. encode imageencoder;
/**
*
* Class ImageIOTest. java
*
* Description javax. imageio operation
*
* Company mapbar
*
* Author Chenll E-mail: Chenll@mapbar.com
*
* Version 1.0
*
* Date 11:16:17
*/
Public class ImageIOTest {
/**
* The Java Image I/O API supports reading and writing common formats. If you need to develop other formats, you also need third-party plug-ins, such as eg JIMI, JMagic
*/
Public void formatImageNames (){
String [] imageFormats = ImageIO. getReaderFormatNames ();
// [Jpg, BMP, bmp, JPG, wbmp, jpeg, png, PNG, JPEG, WBMP, GIF, gif]
System. out. println (Arrays. asList (imageFormats ));
String [] imageFormats1 = ImageIO. getWriterFormatNames ();
// [BMP, bmp, jpg, JPG, wbmp, jpeg, png, PNG, JPEG, WBMP, GIF]
System. out. println (Arrays. asList (imageFormats1 ));
}
/**
* Image I/O Advanced Operations ImageReader ImageWriter uses ImageReader
* You can obtain image information without reading the entire image data into the memory.
*
* @ Throws IOException
*/
Public void imageReaderOp () throws IOException {
Iterator <ImageReader> readers = ImageIO
. GetImageReadersByFormatName ("JPG ");
ImageReader reader = readers. next ();
// Construct the Input Source
File bigFile = new File ("E: \ big. JPG ");
ImageInputStream iis = ImageIO. createImageInputStream (bigFile );
// Associate the Input Source with ImageReader
Reader. setInput (iis, true );
// Obtain the height of the first image, and try to decode the necessary part of the file to obtain the image width without reading any pixel.
Int imageIndex = 0;
Int width = reader. getWidth (imageIndex );
}
/**
* Controls image operations. ImageReadParam allows the program to better control the memory, for example, specifying an area of interest.
*
* @ Throws IOException
*/
Public void imageReadParamOp () throws IOException {
Int imageIndex = 0;
Iterator <ImageReader> readers = ImageIO
. GetImageReadersByFormatName ("JPG ");
ImageReader reader = readers. next ();
File bigFile = new File ("E: \ big. JPG ");
ImageInputStream iis = ImageIO. createImageInputStream (bigFile );
Reader. setInput (iis, true );
ImageReadParam irp = reader. getDefaultReadParam ();
Int halfWidth = reader. getWidth (imageIndex)/2;
Int halfHeight = reader. getHeight (imageIndex)/2;
Rectangle rect = new Rectangle (0, 0, halfWidth, halfHeight );
Irp. setSourceRegion (rect );
BufferedImage bi = reader. read (imageIndex, irp );
ImageIO. write (bi, "JPG", new File ("E: \ big_half.JPG "));
}
/**
* Image metadata
*
* @ Throws IOException
*/
Public void ImageMetadata () throws IOException {
Int imageIndex = 0;
Iterator <ImageReader> readers = ImageIO
. GetImageReadersByFormatName ("JPG ");
ImageReader reader = readers. next ();
File bigFile = new File ("E: \ big. JPG ");
ImageInputStream iis = ImageIO. createImageInputStream (bigFile );
Reader. setInput (iis, true );
IIOMetadata metadata = reader. getImageMetadata (imageIndex );
}
/**
* Generating thumbnails
*
* @ Throws IOException
*/
Public void generateSmall () throws IOException {
File bigFile = new File ("E: \ big. JPG ");
Image image = ImageIO. read (bigFile );
Int width = image. getWidth (null); // 3264
Int height = image. getHeight (null); // 2448
BufferedImage buffi = new BufferedImage (width/2, height/2,
BufferedImage. TYPE_INT_RGB );
Graphics g = buffi. getGraphics ();
G. drawImage (image, 0, 0, width/2, height/2, null );
G. dispose ();
ImageIO. write (buffi, "JPG", new File ("E: \ small. JPG"); // width: 1632 height: 1224
}
Public static void main (String [] args) throws IOException {
ImageIOTest iot = new ImageIOTest ();
Iot. generateSmall ();
Iot. formatImageNames ();
Iot. imageReaderOp ();
Iot. imageReadParamOp ();
Iot. ImageMetadata ();
}
}
Author: cdl2008sky