Using MetaDataExtractor, and a lot of changes

This commit is contained in:
2021-04-28 22:57:33 +02:00
parent 8ef2a6b145
commit 1877ed5154
4 changed files with 183 additions and 95 deletions

View File

@ -41,6 +41,7 @@ namespace PictureReposit
this.btnReOrganize = new System.Windows.Forms.Button();
this.label3 = new System.Windows.Forms.Label();
this.txtRootDir = new System.Windows.Forms.TextBox();
this.pbReorgProcess = new System.Windows.Forms.ProgressBar();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
@ -155,11 +156,20 @@ namespace PictureReposit
this.txtRootDir.Text = "D:\\Bildlabb\\LabRotCat";
this.txtRootDir.TextChanged += new System.EventHandler(this.txtRootDir_TextChanged);
//
// pbReorgProcess
//
this.pbReorgProcess.Location = new System.Drawing.Point(183, 72);
this.pbReorgProcess.Name = "pbReorgProcess";
this.pbReorgProcess.Size = new System.Drawing.Size(654, 79);
this.pbReorgProcess.TabIndex = 11;
this.pbReorgProcess.Visible = false;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(869, 642);
this.Controls.Add(this.pbReorgProcess);
this.Controls.Add(this.txtRootDir);
this.Controls.Add(this.label3);
this.Controls.Add(this.btnReOrganize);
@ -193,6 +203,7 @@ namespace PictureReposit
private System.Windows.Forms.Button btnReOrganize;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox txtRootDir;
private System.Windows.Forms.ProgressBar pbReorgProcess;
}
}

View File

@ -1,4 +1,5 @@
using Microsoft.Win32;
using MetadataExtractor;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
@ -64,22 +65,16 @@ namespace PictureReposit
private void listLocalFiles()
{
DirectoryInfo di = new DirectoryInfo(lblPath.Text);
FileInfo[] files = di.GetFiles("*.jpg");
FileInfo[] files = new string[] { "*.png", "*.jpg", "*.jpeg", "*.img", "*.mov" }
.SelectMany(i => di.GetFiles(i, SearchOption.AllDirectories))
.ToArray();
lstPicFiles.Items.Clear();
for (int i = 0; i < files.Length; i++)
{
lstPicFiles.Items.Add(files[i].Name);
}
files = di.GetFiles("*.jpeg");
for (int i = 0; i < files.Length; i++)
{
lstPicFiles.Items.Add(files[i].Name);
}
files = di.GetFiles("*.png");
for (int i = 0; i < files.Length; i++)
{
lstPicFiles.Items.Add(files[i].Name);
}
btnReOrganize.Enabled = true;
}
@ -89,8 +84,16 @@ namespace PictureReposit
{
string fName = ((ListBox)sender).SelectedItem.ToString();
lblFileName.Text = fName;
Image actPic = Image.FromFile(lblPath.Text + fName, true);
lblCreated.Text = "Skapad: " + TakenDate(actPic);
Image actPic = null;
if (fName.ToUpper().EndsWith(".MOV"))
{
actPic = Image.FromFile("IMG_2994.jpg", true);
}
else
{
actPic = Image.FromFile(lblPath.Text + fName, true);
}
lblCreated.Text = "Skapad: " + GetOrigCreateDateTimeFromMediaFile(new FileInfo(lblPath.Text + fName)).ToString(); ;
int w = actPic.Width;
int h = actPic.Height;
int pw = pictureBox1.Width;
@ -103,111 +106,87 @@ namespace PictureReposit
}
}
private string TakenDate(Image actPic)
{
string sPropDatTaken = "";
for (int i = 0; i < actPic.PropertyIdList.Length; i++)
{
if (actPic.PropertyIdList[i] == 306)
{
for (int j = 0; j < actPic.PropertyItems[i].Len; j++)
{
sPropDatTaken += Convert.ToChar(actPic.PropertyItems[i].Value[j]);
}
}
}
//actPic.Dispose();
return sPropDatTaken;
}
private void btnReOrganize_Click(object sender, EventArgs e)
{
bool error_upd = false;
bool slaskDir = false;
int slaskFilNr = 0;
string fromPath = lblPath.Text.Trim();
pbReorgProcess.Visible = true;
pictureBox1.Visible = false;
if (txtRootDir.Text.Trim().Substring(txtRootDir.Text.Trim().Length - 1) != @"\")
txtRootDir.Text = txtRootDir.Text.Trim() + @"\";
DirectoryInfo di = new DirectoryInfo(txtRootDir.Text);
if (!di.Exists) di.Create();
Deleting = true;
pictureBox1.Image = null;
pictureBox1.Refresh();
for (int i = lstPicFiles.Items.Count - 1; i > -1; i--)
try
{
string lFileName = lstPicFiles.Items[i].ToString().Trim();
string fName = lblPath.Text.Trim() + lFileName;
lblFileName.Text = fName;
Image actIm = Image.FromFile(fName, true);
int w = actIm.Width;
int h = actIm.Height;
int pw = pictureBox1.Width;
int ph = pictureBox1.Height;
pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
Bitmap to_bm = new Bitmap((Image)actIm.Clone(), w * ph / h, ph);
pictureBox1.Image = to_bm;
string td = TakenDate(actIm);
pbReorgProcess.Maximum = lstPicFiles.Items.Count;
pbReorgProcess.Minimum = 0;
actIm.Dispose();
if (td.Trim() != "")
for (int i = lstPicFiles.Items.Count - 1; i > -1; i--)
{
pbReorgProcess.Value = lstPicFiles.Items.Count-i;
string lFileName = lstPicFiles.Items[i].ToString().Trim();
string fName = fromPath + lFileName;
lblFileName.Text = fName;
FileInfo fi = new FileInfo(fName);
string td = GetOrigCreateDateTimeFromMediaFile(fi).ToString();
lblCreated.Text = "Skapad: " + td;
td = td.Substring(0, 10);
DateTime dttd = DateTime.Parse(td.Replace(":", "-"));
string subDir1 = dttd.Year.ToString("0000") + "-" + dttd.Month.ToString("00") + @"\";
string subDir2 = dttd.Year.ToString("0000") + "_" + dttd.Month.ToString("00") + "_" + dttd.Day.ToString("00") + @"\";
DirectoryInfo dis = new DirectoryInfo(di.FullName + subDir1);
if (!dis.Exists) dis.Create();
DirectoryInfo diss = new DirectoryInfo(dis.FullName + subDir2);
if (!diss.Exists) diss.Create();
FileInfo fi = new FileInfo(fName);
//Tommy
fi.CopyTo(diss.FullName + lFileName);
error_upd = false;
do
if (td.Trim() != DateTime.MinValue.ToString())
{
error_upd = false;
try
{
lstPicFiles.BeginUpdate();
}
catch
{
error_upd = true;
}
lblCreated.Text = "Skapad: " + td;
td = td.Substring(0, 10);
DateTime dttd = DateTime.Parse(td);
string subDir1 = dttd.Year.ToString("0000") + "-" + dttd.Month.ToString("00") + @"\";
string subDir2 = dttd.Year.ToString("0000") + "_" + dttd.Month.ToString("00") + "_" + dttd.Day.ToString("00") + @"\";
DirectoryInfo dis = new DirectoryInfo(di.FullName + subDir1);
if (!dis.Exists) dis.Create();
DirectoryInfo diss = new DirectoryInfo(dis.FullName + subDir2);
if (!diss.Exists) diss.Create();
lblPath.Text = diss.FullName;
lblFileName.Refresh();
lblCreated.Refresh();
lblPath.Refresh();
fi.CopyTo(diss.FullName + lFileName, true);
}
while (error_upd);
lstPicFiles.Items.RemoveAt(i);
lstPicFiles.EndUpdate();
lstPicFiles.Refresh();
lblCreated.Refresh();
lblFileName.Refresh();
pictureBox1.Refresh();
this.Refresh();
}
else
{
DirectoryInfo dis = new DirectoryInfo(di.FullName + "NoDateInExif\\");
if (!slaskDir)
else
{
if (!dis.Exists)
DirectoryInfo dis = new DirectoryInfo(di.FullName + "NoDateInExif\\");
if (!slaskDir)
{
dis.Create();
set_Last_number(dis.FullName, 0);
if (!dis.Exists)
{
dis.Create();
set_Last_number(dis.FullName, 0);
}
else
{
slaskFilNr = get_Last_number(dis.FullName);
}
slaskDir = true;
}
else
{
slaskFilNr = get_Last_number(dis.FullName);
}
slaskDir = true;
}
FileInfo fi = new FileInfo(fName);
//Tommy
slaskFilNr++;
fi.CopyTo(dis.FullName + slaskFilNr.ToString() + "_" + lFileName);
slaskFilNr++;
fi.CopyTo(dis.FullName + slaskFilNr.ToString() + "_" + lFileName);
}
}
}
catch (Exception ex)
{
MessageBox.Show($"Error msg: {ex.Message}");
}
finally
{
if (slaskFilNr > 0)
{
set_Last_number(di.FullName + "NoDateInExif\\", slaskFilNr);
}
}
lstPicFiles.Items.Clear();
Deleting = false;
lblCreated.Text = "";
lblFileName.Text = "";
@ -217,6 +196,8 @@ namespace PictureReposit
{
set_Last_number(di.FullName + "NoDateInExif\\", slaskFilNr);
}
pbReorgProcess.Visible = false;
pictureBox1.Visible = true;
}
private void txtRootDir_TextChanged(object sender, EventArgs e)
@ -250,11 +231,99 @@ namespace PictureReposit
outPut++;
return outPut;
}
private void set_Last_number(string path, int lastNr)
{
var lstNrFile = Path.Combine(path, "lastNrFile.txt");
var SavedNumber = lastNr.ToString();
File.WriteAllText(lstNrFile, SavedNumber);
}
//------------------------------------------------------------------------
// Nya funktioner för hantering av original datum
//------------------------------------------------------------------------
DateTime GetOrigCreateDateTimeFromMediaFile(FileInfo file)
{
DateTime origDateTime = DateTime.MinValue;
var creationDate = getCreationDate(file);
if (creationDate != "")
{
if (file.Extension.ToLower() != ".mov")
{
creationDate = prepDateString(creationDate);
}
else
{
creationDate = prepMovDateString(creationDate);
}
origDateTime = DateTime.Parse(creationDate);
}
return origDateTime;
}
private string prepMovDateString(string creationDate)
{
var x = creationDate.Split(" ");
return $"{x[0]} {x[2]} {x[1]} {x[4]} {x[3]}";
}
private string prepDateString(string creationDate)
{
creationDate = creationDate.Replace(" ", "T");
var x = creationDate.Split("T");
x[0] = x[0].Replace(":", "-");
return x[0] + "T" + x[1];
}
private string getCreationDate(FileInfo file)
{
string output = "";
switch (file.Extension.ToLower())
{
case ".jpg":
case ".jpeg":
{
output = getExifValue("Exif IFD0", "Date/Time", file);
if (output == "")
{
output = getExifValue("Exif SubIFD", "Date/Time Original", file);
}
break;
}
case ".png":
{
break;
}
case ".mov":
{
output = getExifValue("QuickTime Movie Header", "Created", file);
break;
}
}
return output;
}
private string getExifValue(string key1, string key2, FileInfo fileInfo)
{
string output = "";
foreach (var directory in ImageMetadataReader.ReadMetadata(fileInfo.FullName))
{
if (directory.Name == key1)
{
foreach (var tag in directory.Tags)
{
if (tag.Name == key2)
{
output = tag.Description;
}
}
}
}
return output;
}
}
}

BIN
PictureReposit/IMG_2994.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 MiB

View File

@ -6,4 +6,12 @@
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
<ItemGroup>
<None Update="IMG_2994.jpg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="MetadataExtractor" Version="2.4.3" />
</ItemGroup>
</Project>