Java ImageIO Processing

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.