Java中指定目录的图像元数据

Java中指定目录的图像元数据

本文介绍了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中指定目录的图像元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 21:01