ImageIO: createImageInputStream(Object input) : ImageIO « javax.imageio « Java by API






ImageIO: createImageInputStream(Object input)

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

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

public class Main {
  public static void main(String[] argv) throws Exception {
    File file = new File("image.gif");
    System.out.println(getFormatName(file));

    InputStream is = new FileInputStream(file);
    is.close();
    System.out.println(getFormatName(is));
  }
  private static String getFormatName(Object o) {
    try {
      ImageInputStream iis = ImageIO.createImageInputStream(o);
      Iterator iter = ImageIO.getImageReaders(iis);
      if (!iter.hasNext()) {
        return null;
      }
      ImageReader reader = (ImageReader) iter.next();
      iis.close();

      return reader.getFormatName();
    } catch (IOException e) {
    }
    return null;
  }
}

   
    
  








Related examples in the same category

1.ImageIO: getImageReaders(Object input)
2.ImageIO: getImageReadersByFormatName(String formatName)
3.ImageIO: getImageReadersByMIMEType(String MIMEType)
4.ImageIO: getImageReadersBySuffix(String fileSuffix)
5.ImageIO: getImageWritersByFormatName(String formatName)
6.ImageIO: getImageWritersByMIMEType(String MIMEType)
7.ImageIO: getImageWritersBySuffix(String fileSuffix)
8.ImageIO: getReaderFormatNames()
9.ImageIO: getReaderMIMETypes()
10.ImageIO: getWriterFormatNames()
11.ImageIO: getWriterMIMETypes()
12.ImageIO: read(File input)
13.ImageIO: read(URL input)
14.ImageIO: write(RenderedImage im, String formatName, File output)