C# 比较日志文件和图像文件之间的时间

C# 比较日志文件和图像文件之间的时间,c#,.net,readfile,exif,C#,.net,Readfile,Exif,我有1000张图片和100000多行日志文件。我需要检查日志文件中是否存在与每个unix时间关联的映像。为此,我首先读取所有图像并将时间信息存储在一个数组中。然后我通读日志文件的所有行,分割每个信息(lat、long、time)并将它们存储在一个数组中。最后,我一次取一个时间元素,并检查它是否与映像时间数组匹配。如果没有找到匹配项,我从log中获取时间,从同一数组中获取lat和long,并将其写入文本文件。但整个过程需要很长时间。我正在研究如何提高这一过程的效率 var file

我有1000张图片和100000多行日志文件。我需要检查日志文件中是否存在与每个unix时间关联的映像。为此,我首先读取所有图像并将时间信息存储在一个数组中。然后我通读日志文件的所有行,分割每个信息(lat、long、time)并将它们存储在一个数组中。最后,我一次取一个时间元素,并检查它是否与映像时间数组匹配。如果没有找到匹配项,我从log中获取时间,从同一数组中获取lat和long,并将其写入文本文件。但整个过程需要很长时间。我正在研究如何提高这一过程的效率

        var fileList = Directory.GetFiles(imageLocation, "*.jpg");
        //Array that will store all the time information obtained from image property
        double[] imgTimeInfo = new double[fileList.Length];
        int imgTimeCounter =0;
        foreach (var fileName in fileList)
        {
            x++;
            string fileNameShort = fileName.Substring(fileName.LastIndexOf('\\') + 1);
            richTextBox1.AppendText("Getting time information from image " + x + " of " + fileList.Length + " : " + fileNameShort + Environment.NewLine);
            richTextBox1.Refresh();
            using (var fs = File.OpenRead(fileName))
            {
                //create an instance of a bitmap image
                var image = new Bitmap(fs);
                //get the date/time image property of the image
                PropertyItem property = image.GetPropertyItem(36867);
                System.Text.Encoding encoding = new System.Text.ASCIIEncoding();
                string valueFrmProperty = encoding.GetString(property.Value);


                //Format the value obtained to convert it into unix equivalent for comparison
                string valueCorrected = valueFrmProperty.Split(' ')[0].Replace(":", "/") + " " + valueFrmProperty.Split(' ')[1];
                var unixTime = ConvertToUnixTimeStamp(DateTime.Parse(valueCorrected));

                imgTimeInfo[imgTimeCounter] = unixTime;
                imgTimeCounter++;
                //It is very important to dispose the image resource before trying to read the property of another image. image.dispose frees the resources or else we get 
                //outofmemoryexception.
                image.Dispose();
             }
        }
        MessageBox.Show("Images done.");
        richTextBox1.AppendText("Fetching time information from log files..."+Environment.NewLine);
        richTextBox1.Refresh();
        int counter4Time = contentBathy.Length / 6;
        //assign counter for lat,long and time 

        int timeCounter = 3;

        for (int i = 0; i < counter4Time; i++)
        {
            richTextBox1.AppendText("Searching time match with image files..." + Environment.NewLine);
            richTextBox1.Refresh();
            double timeValue = Int32.Parse(contentBathy[timeCounter]);
            //Looks for values that is +- 3 seconds different in the image file.
           if (Array.Exists(imgTimeInfo, a => a == timeValue || a == timeValue + 1 ||  a == timeValue + 2|| a == timeValue+3
                ||a == timeValue-1|| a== timeValue-2||a == timeValue-3))
            {                    
                File.AppendAllText(@"c:\temp\matched.txt", "Lat : " + contentBathy[timeCounter - 3] + "  Log : " + contentBathy[timeCounter - 2] + Environment.NewLine);
                richTextBox1.AppendText("Image with same time information found. Looking for another match."+ Environment.NewLine);
            }
            else
            {
                //richTextBox1.AppendText("Time did not match...Writing GPX cordinates..." + Environment.NewLine);
                //richTextBox1.Refresh();
                File.AppendAllText(gpxLocation, "Lat : " + contentBathy[timeCounter - 3] + "  Log : " + contentBathy[timeCounter - 2] + Environment.NewLine);
            }

            if(timeCounter < contentBathy.Length-3)
                timeCounter += 6;
         }
    }
var fileList=Directory.GetFiles(imageLocation,*.jpg”);
//存储从图像属性获取的所有时间信息的数组
double[]imgTimeInfo=新的double[fileList.Length];
int imgTimeCounter=0;
foreach(文件列表中的var文件名)
{
x++;
字符串fileNameShort=fileName.Substring(fileName.LastIndexOf('\\')+1);
richTextBox1.AppendText(“从“+fileList.Length+”:“+fileNameShort+Environment.NewLine”的图像“+x+”获取时间信息);
richTextBox1.Refresh();
使用(var fs=File.OpenRead(文件名))
{
//创建位图图像的实例
var图像=新位图(fs);
//获取图像的日期/时间图像属性
PropertyItem属性=image.GetPropertyItem(36867);
System.Text.Encoding Encoding=新的System.Text.asciencoding();
字符串值FRMProperty=encoding.GetString(property.Value);
//设置获取的值的格式,以便将其转换为unix等效值进行比较
字符串valueCorrected=valueFrmProperty.Split(“”)[0]。替换(“:”,“/”+“”+valueFrmProperty.Split(“”)[1];
var unixTime=ConvertToUnixTimeStamp(DateTime.Parse(valueCorrected));
imgTimeInfo[imgtimeconter]=unixTime;
imgtimeconter++;
//在尝试读取另一个映像的属性之前,处理映像资源是非常重要的。image.dispose释放资源,否则我们将获得
//outofmemoryexception。
image.Dispose();
}
}
MessageBox.Show(“图像完成”);
richTextBox1.AppendText(“从日志文件中获取时间信息…”+Environment.NewLine);
richTextBox1.Refresh();
int counter4Time=contentBathy.Length/6;
//为lat、long和time分配计数器
int计时器=3;
for(int i=0;ia==timeValue | | a==timeValue+1 | | a==timeValue+2 | | a==timeValue+3
||a==timeValue-1 | | a==timeValue-2 | | a==timeValue-3))
{                    
AppendAllText(@“c:\temp\matched.txt”,“Lat:”+contentBathy[timeCounter-3]+“Log:”+contentBathy[timeCounter-2]+Environment.NewLine);
richTextBox1.AppendText(“找到了具有相同时间信息的图像。正在寻找另一个匹配项。”+Environment.NewLine);
}
其他的
{
//richTextBox1.AppendText(“时间不匹配…写入GPX cordinates…”+环境.NewLine);
//richTextBox1.Refresh();
AppendAllText(gpxLocation,“Lat:”+contentBathy[timeCounter-3]+“Log:”+contentBathy[timeCounter-2]+Environment.NewLine);
}
if(计时器
哪个部分需要很长时间?我猜这是在解析(加载…)图像,对吧?加载图像其实没那么糟糕。我尝试了3000张图片,并没有花那么长时间。数组元素之间的值比较耗时最长。请尝试改用sortedlist(
System.Collections.Generic.sortedlist
)。它可能会在确认存在方面做得更好,因为代码不完整,这使得它没有意义。contentBathy不存在,counter4Time也没有解释。所提供的代码实际上并不像您所说的那样(将日志行与图像元数据进行比较),这一逻辑完全缺失。@Safari Jones,我认为理解这种情况不需要超过上面列出的内容。请仔细阅读代码,找出什么是counter4Time,在哪里声明,以及如何使用。上面的代码完成了我所说的一切,因为比较是在第二个for循环中完成的。我只是在这里寻找效率。所以,我不确定你所说的全部逻辑是什么意思。