-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageMetadata.cs
73 lines (64 loc) · 2.31 KB
/
ImageMetadata.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using ExifLib;
using MetadataExtractor;
namespace image_organizer
{
public class ImageMetadata
{
FileInfo _filePath;
ExifReader _exifReader;
IEnumerable<MetadataExtractor.Directory> _directories;
public static ImageMetadata GetMetadata(FileInfo filePath)
{
try
{
IEnumerable<MetadataExtractor.Directory> directories = ImageMetadataReader.ReadMetadata(filePath.FullName);
return new ImageMetadata(filePath, directories);
}
catch (Exception e)
{
Console.WriteLine("Exception while trying to fetch metadata for " + filePath.FullName + ": " + e.Message);
return null;
}
}
private ImageMetadata(FileInfo filePath, IEnumerable<MetadataExtractor.Directory> directories)
{
_filePath = filePath;
_exifReader = new ExifReader(_filePath.FullName);
_directories = directories;
}
public string Caption()
{
var subIfdDirectory = _directories.OfType<MetadataExtractor.Formats.Exif.ExifSubIfdDirectory>().FirstOrDefault();
var userComment = subIfdDirectory?.GetDescription(MetadataExtractor.Formats.Exif.ExifDirectoryBase.TagUserComment);
return userComment;
}
public string DirectoryName()
{
return _filePath.Directory.Name;
}
public bool HasDateTaken()
{
return DateTaken() != null;
}
public DateTime? DateTaken()
{
DateTime? dateTaken = null;
if (GetExifSubIfdDirectory() != null)
{
if (GetExifSubIfdDirectory().ContainsTag(MetadataExtractor.Formats.Exif.ExifDirectoryBase.TagDateTimeOriginal))
{
dateTaken = GetExifSubIfdDirectory().GetDateTime(MetadataExtractor.Formats.Exif.ExifDirectoryBase.TagDateTimeOriginal);
}
}
return dateTaken;
}
private MetadataExtractor.Directory GetExifSubIfdDirectory()
{
return _directories.OfType<MetadataExtractor.Formats.Exif.ExifSubIfdDirectory>()?.FirstOrDefault();
}
}
}