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.