Skip to content

Instantly share code, notes, and snippets.

@cwilper
Last active April 20, 2017 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cwilper/ed0c6ce19883ced299e547d554ccc669 to your computer and use it in GitHub Desktop.
Save cwilper/ed0c6ce19883ced299e547d554ccc669 to your computer and use it in GitHub Desktop.
Get image size from file
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.FileImageInputStream;
import javax.imageio.stream.ImageInputStream;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
/**
* Utility to get the dimensions of an image without reading it all into memory.
*/
public class ImDim {
/**
* Demonstrates use from the command-line.
*
* First arg is a filename, remaining optional args are mime types.
* Output is a single line of the form "width height".
*/
public static void main(final String[] args) {
try {
if (args.length < 1) {
System.err.println("Usage: ImDim path/to/file [mime/type1, [mime/type2], ..]");
System.exit(1);
}
final String[] mimeTypes = new String[args.length - 1];
System.arraycopy(args, 1, mimeTypes, 0, args.length - 1);
final Dimension dim = getDimensions(new File(args[0]), mimeTypes);
System.out.println((int) dim.getWidth() + " " + (int) dim.getHeight());
} catch (final Exception e) {
if (e instanceof IOException) {
System.err.println("Error: " + e.getMessage());
} else {
e.printStackTrace();
System.err.println("Error: See above.");
}
System.exit(1);
}
}
/**
* Gets the dimensions of the given file.
*
* @param file the file.
* @param mimeTypes the optional known mime type(s).
* @return the dimensions.
* @throws IOException if there was an error reading the file or no suitable reader could be found.
*/
public static Dimension getDimensions(final File file, String... mimeTypes) throws IOException {
if (!file.isFile()) {
throw new IOException("No such file: " + file);
}
final ImageReader reader = getReader(file, mimeTypes);
try {
final ImageInputStream stream = new FileImageInputStream(file);
reader.setInput(stream);
return new Dimension(reader.getWidth(reader.getMinIndex()), reader.getHeight(reader.getMinIndex()));
} finally {
reader.dispose();
}
}
private static ImageReader getReader(final File file, String... mimeTypes) throws IOException {
for (final String mimeType : mimeTypes) {
Iterator<ImageReader> iter = ImageIO.getImageReadersByMIMEType(mimeType);
if (iter.hasNext()) {
return iter.next();
}
}
String suffix = getExtension(file);
if (suffix != null) {
Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(getExtension(file));
if (iter.hasNext()) {
return iter.next();
}
}
throw new IOException("No suitable image reader found for " + file.getName());
}
private static String getExtension(final File file) {
final int i = file.getName().lastIndexOf('.');
if (i > -1) {
return file.getName().substring(i + 1);
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment