我有一个程序可以查找图像目录的元数据。我可以找到1张图片的元数据,但无法为第二张图片找到元数据。
我尝试使用file.exists来检查File对象的行为,并且它仅报告第一张图像。即使我所有的图像都是相同格式(JPG),它也适用于该特定图像。
如果您知道如何查找图像目录的元数据,对我来说意义重大。
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();
}
}
这是控制台
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>
谢谢。
答案 0 :(得分:2)
您只是将文件名传递给File构造函数。因此,一旦找到路径./images/JPG_A.jpg
,便使用new File("JPG_A.jpg")
创建一个文件,因此尝试读取当前目录中的JPG_A.jpg文件,而不是文件{{ 1}}。
不要将文件名传递给您的方法。传递完整的Path对象。使用./images/JPG_A.jpg
转换文件路径。