metadata-extractor icon indicating copy to clipboard operation
metadata-extractor copied to clipboard

Why not add a interface for Image dimensions?

Open yujiaao opened this issue 2 years ago • 0 comments

to get the image width and height need very much codes, why don't create a interface like

 interface DimensionDirectory{
   int getWidth();
   int  getHeight();
}

where it is possible for now I need do this to find width and height:


public static int[] readImageHeightWidth(InputStream iis) {
        try {
            Metadata metadata = ImageMetadataReader.readMetadata(iis);

            // I  am a optimist and think the first directory is what I want...
            Directory directory = metadata.getDirectories().iterator().next();
            int height=0;
            int width=0;

            if (directory instanceof JpegDirectory){
                height = directory.getInt(JpegDirectory.TAG_IMAGE_HEIGHT);
                width = directory.getInt(JpegDirectory.TAG_IMAGE_WIDTH);
            }else if(directory instanceof PngDirectory){
                height = directory.getInt(PngDirectory.TAG_IMAGE_HEIGHT);
                width = directory.getInt(PngDirectory.TAG_IMAGE_WIDTH);

            }else if(directory instanceof WebpDirectory){
                height = directory.getInt(WebpDirectory.TAG_IMAGE_HEIGHT);
                width = directory.getInt(WebpDirectory.TAG_IMAGE_WIDTH);

            }else if(directory instanceof GifImageDirectory){
                height = directory.getInt(GifImageDirectory.TAG_HEIGHT);
                width = directory.getInt(GifImageDirectory.TAG_WIDTH);

            }else if(directory instanceof ExifSubIFDDirectory){
                height = directory.getInt(ExifSubIFDDirectory.TAG_IMAGE_HEIGHT);
                width = directory.getInt(ExifSubIFDDirectory.TAG_IMAGE_WIDTH);
            }else if(directory instanceof JfifDirectory){
                height = directory.getInt(JfifDirectory.TAG_RESY);
                width = directory.getInt(JfifDirectory.TAG_RESX);
            }else {
                // try my fortune
                height = directory.getInt(1);
                width = directory.getInt(2);              
            }




            return new int[]{height, width};
        }catch (IOException | MetadataException | ImageProcessingException e){
            Log.error("read image dimension error: "+e.getMessage(), e);
        }
        return new int[] {0,0};
    }

yujiaao avatar Jul 07 '21 05:07 yujiaao