1

我正在尝试制作一个程序,使用 Metadata Extractor 2.4.3 自动将图片从 SD 卡复制和排序到外部硬盘驱动器我似乎没有发现任何问题,但每次运行代码时,都会出现未处理的异常提示。

这是错误:

Unhandled Exception: MetadataExtractor.ImageProcessingException: File format could not be determined 
  at MetadataExtractor.ImageMetadataReader.ReadMetadata(Stream stream)
  at MetadataExtractor.ImageMetadataReader.ReadMetadata(String filePath)
  at file_sorter.File..ctor(String filepath) in 
  C:\Users\ropra\Documents\file_sorter\file_sorter\File.cs:line 27
  at file_sorter.Program.Main(String[] args) in 
  C:\Users\ropra\Documents\file_sorter\file_sorter\Program.cs:line 27

这是代码:

程序.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MetadataExtractor;

namespace file_sorter
{

class Program
{
    static void Main(string[] args)
    {
        // Define paths
        string sandisk = @"Z:/Images/RAW";
        string sd = @"Y:/DCIM/100_FUJI";

        // Count elements in sd 
        string[] photoPaths = System.IO.Directory.GetFiles(sd);
        Console.WriteLine("Counting elements in SD card...");

        //  Create object array
        File[] photos = new File[photoPaths.Count()];

        for (int i = 0; i < photos.Count(); i++)
        {
            photos[i] = new File(photoPaths[i]);
        }

        //  Create tree and copy files
        foreach (var item in photos)
        {
            string fileName = item.filename;
            string sourcePath = item.sourcepath;
            string targetPath = sandisk + "/" + item.year + "/" + item.month + "/" + item.day;
            string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
            string destFile = System.IO.Path.Combine(targetPath, fileName);

            Console.WriteLine("Now copying: {0} into {1}", fileName, targetPath);

            System.IO.Directory.CreateDirectory(targetPath);
            System.IO.File.Copy(sourceFile, destFile, true);


        }
    }
}
}

文件.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MetadataExtractor;

namespace file_sorter
{
    public class File
    {
    public string filename;
    public string path;
    public string year;
    public string month;
    public string day;
    public string sourcepath;

    public File(string filepath)
    {
        path = filepath;
        filename = path.Substring(path.LastIndexOf("\\") + 1);
        sourcepath = path.Substring(0, path.LastIndexOf("\\"));

        string rawDate = "";

        var metadata = ImageMetadataReader.ReadMetadata(path);

        for (int i = 0; i < metadata.Count(); i++)
        {
            for (int j = 0; j < metadata[i].TagCount; j++)
            {
                if (metadata[i].Name == "Exif IFD0" && metadata[i].Tags[j].Name == "Date/Time")
                {
                    rawDate = metadata[i].Tags[j].Description;
                }
            }
        }

        int separator = rawDate.IndexOf(":");
        year = rawDate.Substring(0, separator);
        string sub = rawDate.Substring(separator + 1);
        separator = sub.IndexOf(":");
        month = sub.Substring(0, separator);
        sub = sub.Substring(separator + 1);
        separator = sub.IndexOf(" ");
        day = sub.Substring(0, separator);
    }

    public void ShowFormatedDate()
    {
        Console.WriteLine("Path: {0}", path);
        Console.WriteLine("File: {0}", filename);
        Console.WriteLine("Dir: {0}", sourcepath);
        Console.WriteLine("Year: {0}", year);
        Console.WriteLine("Month: {0}", month);
        Console.WriteLine("Day: {0}", day);
        Console.WriteLine("");
    }
}
}

先感谢您。

4

2 回答 2

0

好的,所以放弃并手动对所有文件进行排序后,我遇到了一个 .xml 文件,其中包含单个 .RAF 文件的元数据,我猜我的相机把它放在那里了。

该程序不处理 MetadataExtractor 不支持的文件,因此存在问题。

于 2021-03-07T16:00:16.937 回答
0

ImageProcessingException:无法确定文件格式

这个异常意味着库没有将给出的文件识别为它知道如何读取的任何文件。例如,如果您将文本文件提供给库,您将看到此异常。

也许您可以捕获此异常类型并为此类文件使用不同的代码路径。

于 2021-05-13T02:23:37.277 回答