This commit is contained in:
2022-11-20 23:20:28 -07:00
parent c6aa7e8e3c
commit 3b988ba152
85 changed files with 1355 additions and 980 deletions

View File

@ -1,3 +1,10 @@
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Text;
namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class Property
@ -85,7 +92,46 @@ internal abstract class Property
return new(result, results);
}
internal static List<DateTime> GetDateTimes(DateTime creationTime, DateTime lastWriteTime, DateTime? dateTime, DateTime? dateTimeDigitized, DateTime? dateTimeOriginal, DateTime? gpsDateStamp)
internal static DateTime? GetDateTimeFromName(Models.FileHolder fileHolder)
{
DateTime? result = null;
int length;
string format;
string fullFormat;
StringBuilder value = new();
string[][] dateFormats = new string[][]
{
new string[] { string.Empty, "yyyyMMdd_HHmmss", string.Empty },
new string[] { string.Empty, "yyyy-MM-dd HH.mm.ss", string.Empty },
new string[] { string.Empty, "yyyyMMdd_HHmmss", "_LLS" },
new string[] { string.Empty, "yyyyMMdd_HHmmss", "_HDR" },
new string[] { "IMG_", "yyyyMMdd_HHmmss", string.Empty },
new string[] { "IMG#####-", "yyyyMMdd-HHmm", string.Empty },
new string[] { "CameraZOOM-", "yyyyMMddHHmmss", string.Empty },
new string[] { "VideoCapture_", "yyyyMMdd-HHmmss ", string.Empty }
};
foreach (string[] dateFormat in dateFormats)
{
_ = value.Clear();
fullFormat = string.Join(string.Empty, dateFormat);
if (fileHolder.NameWithoutExtension.Length != fullFormat.Length)
continue;
format = dateFormat[1];
length = dateFormat[0].Length + dateFormat[1].Length;
for (int i = dateFormat[0].Length; i < length; i++)
_ = value.Append(fileHolder.NameWithoutExtension[i]);
if (value.Length != format.Length)
continue;
if (DateTime.TryParseExact(value.ToString(), format, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime checkDateTime))
{
result = checkDateTime;
break;
}
}
return result;
}
internal static List<DateTime> GetDateTimes(DateTime creationTime, DateTime lastWriteTime, DateTime? dateTime, DateTime? dateTimeDigitized, DateTime? dateTimeFromName, DateTime? dateTimeOriginal, DateTime? gpsDateStamp)
{
List<DateTime> results = new()
{
@ -96,6 +142,8 @@ internal abstract class Property
results.Add(dateTime.Value);
if (dateTimeDigitized.HasValue)
results.Add(dateTimeDigitized.Value);
if (dateTimeFromName.HasValue)
results.Add(dateTimeFromName.Value);
if (dateTimeOriginal.HasValue)
results.Add(dateTimeOriginal.Value);
if (gpsDateStamp.HasValue)
@ -119,6 +167,8 @@ internal abstract class Property
dateTimes.Add(property.DateTime.Value);
if (property.DateTimeDigitized.HasValue)
dateTimes.Add(property.DateTimeDigitized.Value);
if (property.DateTimeFromName.HasValue)
dateTimes.Add(property.DateTimeFromName.Value);
if (property.DateTimeOriginal.HasValue)
dateTimes.Add(property.DateTimeOriginal.Value);
if (property.GPSDateStamp.HasValue)
@ -269,4 +319,119 @@ internal abstract class Property
return result;
}
internal static List<DateTime> GetMetadataDateTimesByPattern(string dateTimeFormat, string sourceDirectoryFile)
{
List<DateTime> results = new();
try
{
DateTime checkDateTime;
DateTime kristy = new(1976, 3, 8);
IReadOnlyList<MetadataExtractor.Directory> directories = MetadataExtractor.ImageMetadataReader.ReadMetadata(sourceDirectoryFile);
foreach (MetadataExtractor.Directory directory in directories)
{
foreach (MetadataExtractor.Tag tag in directory.Tags)
{
if (string.IsNullOrEmpty(tag.Description) || tag.Description.Length != dateTimeFormat.Length)
continue;
if (!DateTime.TryParseExact(tag.Description, dateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out checkDateTime))
continue;
if (checkDateTime < kristy)
continue;
results.Add(checkDateTime);
}
}
}
catch (Exception) { }
return results;
}
#pragma warning disable CA1416
internal static (DateTime?, int?, string?) Get(Models.FileHolder fileHolder)
{
byte[] bytes;
string value;
int? id = null;
DateTime checkDateTime;
string? message = null;
DateTime? dateTime = null;
PropertyItem? propertyItem;
DateTime? gpsDateStamp = null;
DateTime? dateTimeOriginal = null;
DateTime? dateTimeDigitized = null;
ASCIIEncoding asciiEncoding = new();
string dateTimeFormat = IProperty.DateTimeFormat();
try
{
using Image image = Image.FromFile(fileHolder.FullName);
using Bitmap bitmap = new(image);
Rectangle rectangle = new(0, 0, image.Width, image.Height);
BitmapData bitmapData = bitmap.LockBits(rectangle, ImageLockMode.ReadOnly, bitmap.PixelFormat);
IntPtr intPtr = bitmapData.Scan0;
int length = bitmapData.Stride * bitmap.Height;
bytes = new byte[length];
Marshal.Copy(intPtr, bytes, 0, length);
bitmap.UnlockBits(bitmapData);
id = IProperty.GetDeterministicHashCode(bytes);
if (image.PropertyIdList.Contains((int)IExif.Tags.DateTime))
{
propertyItem = image.GetPropertyItem((int)IExif.Tags.DateTime);
if (propertyItem?.Value is not null)
{
value = asciiEncoding.GetString(propertyItem.Value, 0, propertyItem.Len - 1);
if (value.Length > dateTimeFormat.Length)
value = value[..dateTimeFormat.Length];
if (value.Length == dateTimeFormat.Length && DateTime.TryParseExact(value, dateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out checkDateTime))
dateTime = checkDateTime;
}
}
if (image.PropertyIdList.Contains((int)IExif.Tags.DateTimeDigitized))
{
propertyItem = image.GetPropertyItem((int)IExif.Tags.DateTimeDigitized);
if (propertyItem?.Value is not null)
{
value = asciiEncoding.GetString(propertyItem.Value, 0, propertyItem.Len - 1);
if (value.Length > dateTimeFormat.Length)
value = value[..dateTimeFormat.Length];
if (value.Length == dateTimeFormat.Length && DateTime.TryParseExact(value, dateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out checkDateTime))
dateTimeDigitized = checkDateTime;
}
}
if (image.PropertyIdList.Contains((int)IExif.Tags.DateTimeOriginal))
{
propertyItem = image.GetPropertyItem((int)IExif.Tags.DateTimeOriginal);
if (propertyItem?.Value is not null)
{
value = asciiEncoding.GetString(propertyItem.Value, 0, propertyItem.Len - 1);
if (value.Length > dateTimeFormat.Length)
value = value[..dateTimeFormat.Length];
if (value.Length == dateTimeFormat.Length && DateTime.TryParseExact(value, dateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out checkDateTime))
dateTimeOriginal = checkDateTime;
}
}
if (image.PropertyIdList.Contains((int)IExif.Tags.GPSDateStamp))
{
propertyItem = image.GetPropertyItem((int)IExif.Tags.GPSDateStamp);
if (propertyItem?.Value is not null)
{
value = asciiEncoding.GetString(propertyItem.Value, 0, propertyItem.Len - 1);
if (value.Length > dateTimeFormat.Length)
value = value[..dateTimeFormat.Length];
if (value.Length == dateTimeFormat.Length && DateTime.TryParseExact(value, dateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out checkDateTime))
gpsDateStamp = checkDateTime;
}
}
bitmap.Dispose();
image.Dispose();
}
catch (Exception)
{
message = string.Concat(new StackFrame().GetMethod()?.Name, " <", fileHolder.FullName, ">");
}
DateTime?[] dateTimes = new DateTime?[] { fileHolder.LastWriteTime, fileHolder.CreationTime, dateTime, dateTimeDigitized, dateTimeOriginal, gpsDateStamp };
return new(dateTimes.Min(), id, message);
}
#pragma warning restore CA1416
}