-1

I have a program to find Metadata for a directory of images. I am able to find metadata for 1 image but I am unable to do it for the second image.

I tried file.exists to check how my File object behaves and it reports for 1st image only. It works for that particular image even though all my images are in same format (JPG).

If you know how to find metadata for a directory of images it would mean a lot to me.

 public class Metadata{
        private final static Logger LOG = LoggerFactory.getLogger(Metadata.class);
        public static void main(String[] args) {
            Metadata meta = new Metadata();

            Path dir = Paths.get("./images");
            try(DirectoryStream<Path> stream = Files.newDirectoryStream(dir)){

                int i = 0;
                for(Path file:stream){

                    System.out.println(file.getFileName().toString());//getting for both image
                    meta.readAndDisplayMetadata(file.getFileName().toString());
                }
            }catch(IOException IO){
            }
        }

        void readAndDisplayMetadata(String fileName ) {
            try {
                LOG.info(fileName);//working for both images
                File file = new File(fileName);
                if(file.exists())//not working for 1 image
                LOG.info(fileName);
                ImageInputStream iis = ImageIO.createImageInputStream(file);
                Iterator<ImageReader> readers = ImageIO.getImageReaders(iis); //Exception traces back here
                if (readers.hasNext()) {
                    ImageReader reader = readers.next();

                    reader.setInput(iis, true);

                    IIOMetadata metadata = reader.getImageMetadata(0);

                    String[] names = metadata.getMetadataFormatNames();
                    int length = names.length;
                    for (int i = 0; i < length; i++) {
                        System.out.println( "Format name: " + names[ i ] );
                        displayMetadata(metadata.getAsTree(names[i])); //consider this method is working
                    }
                }
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        } 

This is Console

JPG1_A.jpg
15:42:17.251 [main] INFO metadata.Metadata - JPG1_A.jpg
test.jpg
15:42:17.324 [main] INFO metadata.Metadata - test.jpg
java.lang.IllegalArgumentException: input == null! //EXCEPTION FOR JPEG_A.jpg
15:42:17.324 [main] INFO metadata.Metadata - test.jpg
    at javax.imageio.ImageIO.getImageReaders(ImageIO.java:641)
    at metadata.Metadata.readAndDisplayMetadata(Metadata.java:59)
    at metadata.Metadata.main(Metadata.java:33)

Format name: javax_imageio_jpeg_image_1.0
<javax_imageio_jpeg_image_1.0>
    <JPEGvariety>
        <app0JFIF majorVersion="1" minorVersion="1" resUnits="1" Xdensity="96" Ydensity="96" thumbWidth="0" thumbHeight="0"/>
    </JPEGvariety>
    <markerSequence>
        <dqt>
            <dqtable elementPrecision="0" qtableId="0"/>
        </dqt>
        <dqt>
            <dqtable elementPrecision="0" qtableId="1"/>
        </dqt>
        <sof process="0" samplePrecision="8" numLines="649" samplesPerLine="1280" numFrameComponents="3">
            <componentSpec componentId="1" HsamplingFactor="2" VsamplingFactor="2" QtableSelector="0"/>
            <componentSpec componentId="2" HsamplingFactor="1" VsamplingFactor="1" QtableSelector="1"/>
            <componentSpec componentId="3" HsamplingFactor="1" VsamplingFactor="1" QtableSelector="1"/>
        </sof>
        <dht>
            <dhtable class="0" htableId="0"/>
        </dht>
        <dht>
            <dhtable class="1" htableId="0"/>
        </dht>
        <dht>
            <dhtable class="0" htableId="1"/>
        </dht>
        <dht>
            <dhtable class="1" htableId="1"/>
        </dht>
        <sos numScanComponents="3" startSpectralSelection="0" endSpectralSelection="63" approxHigh="0" approxLow="0">
            <scanComponentSpec componentSelector="1" dcHuffTable="0" acHuffTable="0"/>
            <scanComponentSpec componentSelector="2" dcHuffTable="1" acHuffTable="1"/>
            <scanComponentSpec componentSelector="3" dcHuffTable="1" acHuffTable="1"/>
        </sos>
    </markerSequence>
</javax_imageio_jpeg_image_1.0>

Format name: javax_imageio_1.0
<javax_imageio_1.0>
    <Chroma>
        <ColorSpaceType name="YCbCr"/>
        <NumChannels value="3"/>
    </Chroma>
    <Compression>
        <CompressionTypeName value="JPEG"/>
        <Lossless value="FALSE"/>
        <NumProgressiveScans value="1"/>
    </Compression>
    <Dimension>
        <PixelAspectRatio value="1.0"/>
        <ImageOrientation value="normal"/>
        <HorizontalPixelSize value="0.26458332"/>
        <VerticalPixelSize value="0.26458332"/>
    </Dimension>
</javax_imageio_1.0>

Thanks in advance.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Alex Smith
  • 463
  • 2
  • 8
  • 17
  • 1
    You ignore exceptions, and you forgot curly braces around your if block. Thos are the immediate things that you should fix. For the rest, tell us **precisely** what you expect to happen, and what happens instead. Saying "not working" doesn't tell us anything. If you get an exception, then, of course, post its complete and exact stack trace. – JB Nizet Jul 02 '18 at 10:11
  • It is working for test.jpg and it is not working for JPG_A.jpg, I have both images in "./images" directory. – Alex Smith Jul 02 '18 at 10:16
  • 1
    I already fixed the code for you [here](https://stackoverflow.com/questions/51021122/image-metadata-for-a-specified-directory-in-java), why the need to re-invent the same bug? Use `File` (or `Path`) as the parameter to your metadata method, not just the file name. – Harald K Jul 02 '18 at 11:13

1 Answers1

2

You're only passing the file name to the File constructor. So, once you've found the path ./images/JPG_A.jpg, you create a file using new File("JPG_A.jpg"), and you're thus trying to read the file JPG_A.jpg in the current directory, rather then the file ./images/JPG_A.jpg.

Don't pass the file name to your method. Pass the complete Path object. Transform the Path to a File using Path.toFile().

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255