• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

How To Get Image Format Using Java

April 21, 2014 //  by Krishna Srinivasan//  Leave a Comment

Java provides the ImageIO to work with image files. This example demonstrates how to get the type or format of the image file. It is necessary to know the type of the file when you are processing the images. Java has ImageReader which stores the type of image file. One image can have more that one readers associated with that file.

If you look at the below example, I have read a JPEG file and then printing the image format using the ImageIO classes.

ImageFormatExample.java

package javabeat.net.core;

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;

/**
 * Get Image Format Example
 *
 * @author Krishna
 *
 */
public class ImageFormatExample {
	public static void main(String[] args) throws IOException {

		//Create Image File
		File imageFile = new File("Desert.jpg");

		//Create ImageInputStream using Image File
		ImageInputStream imageInputStream = ImageIO.createImageInputStream(imageFile);

		//Get the image readers for that file
		Iterator<ImageReader> imageReadersList = ImageIO.getImageReaders(imageInputStream);

		if (!imageReadersList.hasNext()) {
			throw new RuntimeException("Image Readers Not Found!!!");
		}

		//Get the image type
		ImageReader reader = imageReadersList.next();
		System.out.println("Image Format: " + reader.getFormatName());

		//Close stream (best practice)
		imageInputStream.close();

	}
}

Output…

Image Format: JPEG

Category: JavaTag: Java ImageIO

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « How To Create Zip File Using Java
Next Post: How To Compress Image Using Java java»

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact