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

时间:2015-07-09 20:06:23

标签: c# .net readfile exif

我有1000张图片和超过100,000行日志文件。我需要检查是否存在与日志文件中的每个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;
         }
    }

0 个答案:

没有答案