Find Resolution of a Image Java

PROGRAM TO FIND THE RESOLUTION OF A IMAGE


package com.dn.image;
// Copy source from page http://www.java2s.com/Code/Java/2D-Graphics-GUI/Imageformatinfo.htm or download from
// https://dnhome.files.wordpress.com/2012/06/imageinfo-java.doc and rename imageinfo-java.doc to ImageInfo.java
 
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
 
import javax.imageio.ImageIO;
 
import org.jimageanalyst.ImageInfo;
import org.jimageanalyst.InvalidDataException;
import org.jimageanalyst.JImageAnalyst;
import org.jimageanalyst.JImageAnalystFactory;
 
public class Demo {
/**
* @param args
*/
public static void main(String[] args) {
File file = new File("K:\\demo_image.jpg");
byte fileContent[] = new byte[(int) file.length()];
FileInputStream fin;
try {
// Read image in byte array
fin = new FileInputStream(file);
fin.read(fileContent);
// Get Image information
InputStream inp = new ByteArrayInputStream(fileContent);
JImageAnalyst analyst = JImageAnalystFactory.getDefaultInstance();
ImageInfo imageInfo = analyst.analyze(inp);
// Image Height
System.out.println("Image Height : " + imageInfo.getHeight());
// Image Height
System.out.println("Image Width : " + imageInfo.getWidth());
// Horizontal resolution
System.out.println("Horizontal Resolution : " + imageInfo.getPhysicalHeightDpi() + " dpi");
// Vertical resolution
System.out.println("Vertical Resolution : "
+ imageInfo.getPhysicalHeightDpi() + " dpi");
 
} catch (FileNotFoundException e) {
System.out.println("File not found");
} catch (InvalidDataException e) {
// Default API can not read image correct
BufferedImage img = null;
InputStream inputstream = new ByteArrayInputStream(fileContent);
try {
img = ImageIO.read(inputstream);
// Image Height
System.out.println("Image Height : " + img.getHeight());
// Image Height
System.out.println("Image Width : " + img.getWidth());
com.dn.image.ImageInfo imageInfo = new com.dn.image.ImageInfo();
imageInfo.setInput(inputstream);
imageInfo.check();
// Horizontal resolution
System.out.println("Horizontal Resolution : "
+ imageInfo.getPhysicalHeightDpi() + " dpi");
// Vertical resolution
System.out.println("Vertical Resolution : "
+ imageInfo.getPhysicalHeightDpi() + " dpi");
} catch (IOException ex) {
System.out.println("Image data is not supported");
}
} catch (IOException e) {
System.out.println("Image data is not supported");
}
}
 
}



Comments

Popular posts from this blog

Solve the Sudoku Python

Solve the Sudoku Java

Find Duplicates Java