Metadata Extractor Unable to read Camera specific Orientation

1.9k views Asked by At

I have an image which contains 2 Orientation attributes. When I try to read the meta data of the image, it gives out the value from the second orientation attribute always.

Is there a way to read the first orientation value?

I have attached 2 files. 1 is the image and 2 is the meta data of the image in the form of text file.

  1. Image enter image description here

  2. MetaData fileenter image description here

I am using the code as shown below:

public int getImage(String name,String outputFileName) throws ImageProcessingException, IOException {

        Metadata metadata;
        try {
            File f = new File(name);
            metadata = ImageMetadataReader.readMetadata(f);

            for (Directory directory : metadata.getDirectories()) {
                for (Tag tag : directory.getTags()) {

                    if(tag.getTagName().equals("Orientation")){
                        final Integer index = directory.getInteger(274);
                        System.out.format("[%s] - %s = %s",
                                directory.getName(), tag.getTagName(),index);
                        BufferedImage image = getRotatedImage(f,index);
                        File img = new File(outputFileName+".png");
                        ImageIO.write(image,"png",img);
                        //return 0;

                    }
                }
                if (directory.hasErrors()) {
                    for (String error : directory.getErrors()) {
                        System.err.format("ERROR: %s", error);
                    }
                }
            }
        } catch (ImageProcessingException e) {
            System.out.println("Image processing exception while rotating image");
            throw e;
        } catch (IOException e) {
            System.out.println("IO exception while rotating image");
            throw e;
        }
        return 0;
    }
1

There are 1 answers

2
Drew Noakes On

There are two orientations because one is for the image, and the other is for the thumbnail.

Rather than iterating over all directories, get just the directory you want via Metadata.getFirstDirectoryOfType or Metadata.getDirectoriesOfType. You can then ask that directory directly for the tag's value. Again, there is no need to iterate all tags -- that just wastes resources (CPU/memory).