Java中指定目录的图像元数据 [英] Image Metadata for a specified directory in Java

查看:48
本文介绍了Java中指定目录的图像元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我有显示 1 个文件的元数据的代码.我想知道如何使用显示指定目录的元数据?

Here I have code for displaying metadata for 1 file. I wanted to know how can I use to display metadata for a specified directory?

import java.io.*;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import javax.imageio.*;
import javax.imageio.stream.*;
import javax.imageio.metadata.*;
import java.util.Scanner;

public class Metadata{

    public static void main(String[] args) {
        Metadata meta = new Metadata();


        if (new File("./images/test.jpg").exists()) {
            System.out.println("Can find " + "test.jpg file");
            meta.readAndDisplayMetadata("test.jpg");
        } else {
            System.out.println("cannot find file: " + "test.jpg");
        }


    }


    void readAndDisplayMetadata(String fileName ) {//or String fileName
        try {

            File file = new File(fileName);

            ImageInputStream iis = ImageIO.createImageInputStream(file);
            Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);



            if (readers.hasNext()) {

                // pick the first available ImageReader
                ImageReader reader = readers.next();

                // attach source to the reader
                reader.setInput(iis, true);

                // read metadata of first image
                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]));
                }
            }

        }
        catch (Exception e) {

            e.printStackTrace();
        }
    }

    void displayMetadata(Node root) {
        displayMetadata(root, 0);
    }

    void indent(int level) {
        for (int i = 0; i < level; i++)
            System.out.print("    ");
    }

    void displayMetadata(Node node, int level) {
        // print open tag of element
        indent(level);
        System.out.print("<" + node.getNodeName());
        NamedNodeMap map = node.getAttributes();
        if (map != null) {

            // print attribute values
            int length = map.getLength();
            for (int i = 0; i < length; i++) {
                Node attr = map.item(i);
                System.out.print(" " + attr.getNodeName() +
                                 "=\"" + attr.getNodeValue() + "\"");
            }
        }

        Node child = node.getFirstChild();
        if (child == null) {
            // no children, so close element and return
            System.out.println("/>");
            return;
        }

        // children, so close current tag
        System.out.println(">");
        while (child != null) {
            // print children recursively
            displayMetadata(child, level + 1);
            child = child.getNextSibling();
        }

        // print close tag of element
        indent(level);
        System.out.println("</" + node.getNodeName() + ">");
    }
} 

我使用了 DirectoryPath 但遗憾的是 ImageInputStream 没有目录接口.

I have used DirectoryPath but sadly ImageInputStream has no interface for directories.

是否有任何可用的库或框架可以自动将目录的图像元数据作为 JSON 提供?

Are there any libraries or frameworks that are available that can automatically give image metadata for a directory as JSON?

推荐答案

鉴于您已经有代码来显示单个文件的元数据,修改它以支持多个文件是微不足道的.

Given that you already have code to displaying metadata for a single file, it's trivial to modify it to support multiple files.

您只需要FileisDirectory()listFiles() 方法(或者,如果您使用的是Java 8+,您可以使用 Files.list(Path) 方法进行延迟迭代*).

All you need is the isDirectory() and listFiles() methods of File (or, if you are on Java 8+, you can use the Files.list(Path) method, for lazy iteration*).

如果你的输入文件是一个目录,列出每个文件并递归:

If your input file is a directory, list each file and recurse:

public class Metadata {

    public static void main(String[] args) {
        Metadata meta = new Metadata();

        File file = new File(args[0]);

        if (file.exists()) {
            System.out.println("Can find " + file);
            meta.readAndDisplayMetadata(file);
        }
        else {
            System.out.println("cannot find file: " + file);
        }
    }

    void readAndDisplayMetadata(File file)  {
        // If you *don't* want recursive behavior, 
        // move this block "outside" or to a separate method 
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File f : files) {
                readAndDisplayMetadata(f);
            }
        }

        // Rest of method as is...
    }
}

*) 下面的更好,如果你可以使用它,因为如果目录中有很多文件,上面的可能会耗尽内存.

*) The below is better, if you can use it, as the above can run out of memory if you have many files in the directory.

void readAndDisplayMetadata(File file) throws IOException {
    Path path = file.toPath();
    if (Files.isDirectory(path /* optional, handle symbolic links */)) {
        Files.list(path)
             .forEach(p -> readAndDisplayMetadata(p.toFile()));
    }

    // Rest of method as is...
}

这篇关于Java中指定目录的图像元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆