Skip to content

Instantly share code, notes, and snippets.

@mumer92
Created September 6, 2015 10:00
Show Gist options
  • Select an option

  • Save mumer92/4da48d7c83d30b30bba2 to your computer and use it in GitHub Desktop.

Select an option

Save mumer92/4da48d7c83d30b30bba2 to your computer and use it in GitHub Desktop.
Class that dumps image information
package Classes;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.stream.ImageInputStream;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
public class DumpImageMetadata {
private static String getFileExtension(File file) {
String fileName = file.getName();
int lastDot = fileName.lastIndexOf('.');
return fileName.substring(lastDot + 1);
}
private static void indent(int level) {
for (int i = 0; i < level; i++) {
System.out.print(" ");
}
}
private static void displayAttributes(NamedNodeMap attributes) {
if (attributes != null) {
int count = attributes.getLength();
for (int i = 0; i < count; i++) {
Node attribute = attributes.item(i);
System.out.print(" ");
System.out.print(attribute.getNodeName());
System.out.print("='");
System.out.print(attribute.getNodeValue());
System.out.print("'");
}
}
}
private static void displayMetadataNode(Node node, int level) {
indent(level);
System.out.print("<");
System.out.print(node.getNodeName());
NamedNodeMap attributes = node.getAttributes();
displayAttributes(attributes);
Node child = node.getFirstChild();
if (child == null) {
String value = node.getNodeValue();
if (value == null || value.length() == 0) {
System.out.println("/>");
} else {
System.out.print(">");
System.out.print(value);
System.out.print("<");
System.out.print(node.getNodeName());
System.out.println(">");
}
return;
}
System.out.println(">");
while (child != null) {
displayMetadataNode(child, level + 1);
child = child.getNextSibling();
}
indent(level);
System.out.print("</");
System.out.print(node.getNodeName());
System.out.println(">");
}
private static void dumpMetadata(IIOMetadata metadata) {
String[] names = metadata.getMetadataFormatNames();
int length = names.length;
for (int i = 0; i < length; i++) {
indent(2);
System.out.println("Format name: " + names[i]);
displayMetadataNode(metadata.getAsTree(names[i]), 3);
}
}
private static void processFileWithReader(File file, ImageReader reader) throws IOException {
ImageInputStream stream = null;
try {
stream = ImageIO.createImageInputStream(file);
reader.setInput(stream, true);
IIOMetadata metadata = reader.getImageMetadata(0);
indent(1);
System.out.println("Image metadata");
dumpMetadata(metadata);
metadata = reader.getStreamMetadata();
if (metadata != null) {
indent(1);
System.out.println("Stream metadata");
dumpMetadata(metadata);
}
} finally {
if (stream != null) {
stream.close();
}
}
}
private static void processFile(File file) throws IOException {
System.out.println("\nProcessing " + file.getName() + ":\n");
String extension = getFileExtension(file);
Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName(extension);
while (readers.hasNext()) {
ImageReader reader = readers.next();
System.out.println("Reader: " + reader.getClass().getName());
processFileWithReader(file, reader);
}
}
private static void processDirectory(File directory) throws IOException {
System.out.println("Processing all files in " + directory.getAbsolutePath());
File[] contents = directory.listFiles();
for (File file : contents) {
if (file.isFile()) {
processFile(file);
}
}
}
public static void DumpData(String[] args) {
try {
for (int i = 0; i < args.length; i++) {
File fileOrDirectory = new File(args[i]);
if (fileOrDirectory.isFile()) {
processFile(fileOrDirectory);
} else {
processDirectory(fileOrDirectory);
}
}
System.out.println("\nDone");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void DumpData(String inFilePath) {
File fileOrDirectory = new File(inFilePath);
try {
processFile(fileOrDirectory);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment