tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 Java > Image IO > How to get image width, height and format

How to get image width, height and format 

Java Image IO provides pluggable architecture for accessing images transparently. It is simple, flexible and allows us to plugin various image readers and writers with ease. The following example shows how to get image width, height and format.

stpatricks_08.gif

File Name  :  
com/bethecoder/tutorials/imageio/GetImageInfoTest.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.imageio;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;


public class GetImageInfoTest {

  /**
   @param args
   @throws IOException 
   */
  public static void main(String[] argsthrows IOException {

    File file = new File("C:/Temp/stpatricks_08.gif");
    ImageInputStream iis = ImageIO.createImageInputStream(file);
        Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);

        if (readers.hasNext()) {
          //Get the first available ImageReader
            ImageReader reader = readers.next();
            reader.setInput(iis, true);

            System.out.println("Format : " + reader.getFormatName());
            System.out.println("Width : " + reader.getWidth(0" pixels");
            System.out.println("Height : " + reader.getHeight(0" pixels");
        }
  }

}
   

It gives the following output,
Format : gif
Width : 320 pixels
Height : 110 pixels


                    Print

   



Books Recommendation

 
 
bl  br