Skip to content

Instantly share code, notes, and snippets.

@flxw
Created April 22, 2020 23:08
Show Gist options
  • Save flxw/1a97e79d49158aecbaa9b16d07465f5c to your computer and use it in GitHub Desktop.
Save flxw/1a97e79d49158aecbaa9b16d07465f5c to your computer and use it in GitHub Desktop.
Print the actually visible image resolution in accordance with the image's rotation tag
import com.drew.imaging.ImageMetadataReader;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;
import com.drew.metadata.exif.ExifSubIFDDirectory;
import org.apache.commons.imaging.ImageInfo;
import org.apache.commons.imaging.Imaging;
import java.io.File;
import java.util.Iterator;
public class Test {
public static void main(String[] args) {
String[] fileNames = { "20200321_154441.jpg", "20200314_100712.jpg" };
System.out.println("Apache Commons Imaging");
for (String fileName: fileNames) {
File f = new File(fileName);
try {
final ImageInfo imageInfo = Imaging.getImageInfo(f);
int width = (int) imageInfo.getWidth();
int height = (int) imageInfo.getHeight();
Metadata metadata = ImageMetadataReader.readMetadata(f);
Iterator<Directory> directoryIterator = metadata.getDirectories().iterator();
int or = -1;
for (Directory directory = directoryIterator.next();
directoryIterator.hasNext() && or < 0;
directory = directoryIterator.next()) {
for (Tag tag : directory.getTags()) {
if (tag.getTagName().equals("Orientation")) {
System.out.println(tag.getDirectoryName() + " - " + tag.toString());
or = directory.getInt(ExifSubIFDDirectory.TAG_ORIENTATION);
break;
}
}
}
if (or >= 4) {
int x = width;
width = height;
height = x;
}
System.out.println(fileName + " - " + height + "x" + width + " --- " + or);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment