93 lines
3.3 KiB
C#
93 lines
3.3 KiB
C#
|
|
using MetadataExtractor;
|
||
|
|
using MetadataExtractor.Formats.Exif;
|
||
|
|
|
||
|
|
namespace GhostImageManager.Services;
|
||
|
|
|
||
|
|
public class ExifData
|
||
|
|
{
|
||
|
|
public string? CameraMake { get; set; }
|
||
|
|
public string? CameraModel { get; set; }
|
||
|
|
public string? Lens { get; set; }
|
||
|
|
public string? Aperture { get; set; }
|
||
|
|
public string? ShutterSpeed { get; set; }
|
||
|
|
public string? Iso { get; set; }
|
||
|
|
public string? FocalLength { get; set; }
|
||
|
|
public DateTime? CaptureDate { get; set; }
|
||
|
|
}
|
||
|
|
|
||
|
|
public class ExifService
|
||
|
|
{
|
||
|
|
private readonly ILogger<ExifService> _logger;
|
||
|
|
|
||
|
|
public ExifService(ILogger<ExifService> logger)
|
||
|
|
{
|
||
|
|
_logger = logger;
|
||
|
|
}
|
||
|
|
|
||
|
|
public ExifData ExtractExifData(string filePath)
|
||
|
|
{
|
||
|
|
var result = new ExifData();
|
||
|
|
|
||
|
|
try
|
||
|
|
{
|
||
|
|
if (!File.Exists(filePath))
|
||
|
|
{
|
||
|
|
_logger.LogWarning("EXIF-Extraktion: Datei nicht gefunden {FilePath}", filePath);
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
var directories = ImageMetadataReader.ReadMetadata(filePath);
|
||
|
|
var ifd0Directory = directories.OfType<ExifIfd0Directory>().FirstOrDefault();
|
||
|
|
var subIfdDirectory = directories.OfType<ExifSubIfdDirectory>().FirstOrDefault();
|
||
|
|
var exifDirectory = directories.OfType<ExifDirectoryBase>().FirstOrDefault();
|
||
|
|
|
||
|
|
// Kamera
|
||
|
|
if (ifd0Directory != null)
|
||
|
|
{
|
||
|
|
result.CameraMake = ifd0Directory.GetDescription(ExifDirectoryBase.TagMake);
|
||
|
|
result.CameraModel = ifd0Directory.GetDescription(ExifDirectoryBase.TagModel);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Aufnahmedatum
|
||
|
|
if (exifDirectory != null && exifDirectory.TryGetDateTime(ExifDirectoryBase.TagDateTimeOriginal, out var captureDate))
|
||
|
|
{
|
||
|
|
result.CaptureDate = captureDate;
|
||
|
|
}
|
||
|
|
else if (exifDirectory != null && exifDirectory.TryGetDateTime(ExifDirectoryBase.TagDateTime, out var dateTime))
|
||
|
|
{
|
||
|
|
result.CaptureDate = dateTime;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (subIfdDirectory != null)
|
||
|
|
{
|
||
|
|
// Blende
|
||
|
|
result.Aperture = subIfdDirectory.GetDescription(ExifDirectoryBase.TagAperture);
|
||
|
|
if (string.IsNullOrEmpty(result.Aperture))
|
||
|
|
result.Aperture = subIfdDirectory.GetDescription(ExifDirectoryBase.TagFNumber);
|
||
|
|
|
||
|
|
// Verschlusszeit
|
||
|
|
result.ShutterSpeed = subIfdDirectory.GetDescription(ExifDirectoryBase.TagExposureTime);
|
||
|
|
|
||
|
|
// ISO
|
||
|
|
result.Iso = subIfdDirectory.GetDescription(ExifDirectoryBase.TagIsoEquivalent);
|
||
|
|
|
||
|
|
// Brennweite
|
||
|
|
result.FocalLength = subIfdDirectory.GetDescription(ExifDirectoryBase.TagFocalLength);
|
||
|
|
|
||
|
|
// Objektiv
|
||
|
|
result.Lens = subIfdDirectory.GetDescription(ExifDirectoryBase.TagLensModel);
|
||
|
|
if (string.IsNullOrEmpty(result.Lens))
|
||
|
|
result.Lens = subIfdDirectory.GetDescription(ExifDirectoryBase.TagLensSpecification);
|
||
|
|
}
|
||
|
|
|
||
|
|
_logger.LogDebug("EXIF-Daten für {File} extrahiert", Path.GetFileName(filePath));
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
_logger.LogWarning(ex, "Fehler beim Extrahieren der EXIF-Daten für {FilePath}", filePath);
|
||
|
|
}
|
||
|
|
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
}
|