Пример #1
0
        public MainWindow()
        {
            InitializeComponent();

            imageSources = new List<string>();
            imageSources.Add(@"C:\Users\Chan\Documents\Visual Studio 2013\Projects\ImageDiff\ImageDiff\TestImages\20150511_114433_01.jpg");
            imageSources.Add(@"C:\Users\Chan\Documents\Visual Studio 2013\Projects\ImageDiff\ImageDiff\TestImages\20150511_114433_02.jpg");
            imageSources.Add(@"C:\Users\Chan\Documents\Visual Studio 2013\Projects\ImageDiff\ImageDiff\TestImages\20150511_114433_03.jpg");
            imageSources.Add(@"C:\Users\Chan\Documents\Visual Studio 2013\Projects\ImageDiff\ImageDiff\TestImages\20150511_114433_04.jpg");
            imageSources.Add(@"C:\Users\Chan\Documents\Visual Studio 2013\Projects\ImageDiff\ImageDiff\TestImages\20150511_114433_05.jpg");
            imageSources.Add(@"C:\Users\Chan\Documents\Visual Studio 2013\Projects\ImageDiff\ImageDiff\TestImages\20150511_114433_06.jpg");

            for (int i = 0; i < 5; ++i)
            {
                Comparison c = new Comparison(imageSources[i], imageSources[i+1]);
               comparisons.Add(c);
            }
            // Comparison = c;
        }
Пример #2
0
        private void Compare_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
            openFileDialog.Title = "Files to compare";
            openFileDialog.Multiselect = true;
            bool? result = openFileDialog.ShowDialog();
            if (result.HasValue && result.Value)
            {
                imageSources = new List<string>();
                for (int i = 0; i < openFileDialog.FileNames.Length; ++i)
                {
                    imageSources.Add(openFileDialog.FileNames[i]);
                }
            }
            else
            {
                return;
            }

            if (openFileDialog.SafeFileNames.Length == 1)
            {
                openFileDialog.Title = "Right File";
                openFileDialog.Multiselect = false;
                result = openFileDialog.ShowDialog();
                if (result.HasValue && result.Value)
                {
                    imageSources.Add(openFileDialog.FileName);
                }
                else
                {
                    return;
                }
            }

            comparisons.Clear();
            for (int i = 0; i < imageSources.Count - 1; ++i)
            {
                Comparison c = new Comparison(imageSources[i], imageSources[i+1]);
                comparisons.Add(c);
            }
        }
Пример #3
0
        static int Main(string[] args)
        {
            double threshold = 10;
            int verbose = 0;
            string folderPath = null;
            bool continuous = false;
            int continuousWait = 10000;
            bool dryRun = false;
            string logFileName = null;

            for (int arg = 0; arg < args.Length; ++arg)
            {
                switch (args[arg])
                {
                    case "-t":
                        ++arg;
                        if (arg >= args.Length)
                        {
                            Usage();
                            return 1;
                        }

                        if (!double.TryParse(args[arg], out threshold))
                        {
                            Usage();
                            return 1;
                        }
                        break;

                    case "-v":
                        verbose = 1;
                        break;

                    case "-V":
                        verbose = 2;
                        break;

                    case "-c":
                        continuous = true;
                        break;

                    case "-w":
                        ++arg;
                        if (arg >= args.Length)
                        {
                            Usage();
                            return 1;
                        }

                        if (!int.TryParse(args[arg], out continuousWait))
                        {
                            Usage();
                            return 1;
                        }
                        break;

                    case "-l":
                        ++arg;
                        if (arg >= args.Length)
                        {
                            Usage();
                            return 1;
                        }

                        logFileName = args[arg];
                        break;

                    case "-d":
                        dryRun = true;
                        break;

                    default:
                        if (folderPath != null)
                        {
                            Usage();
                            return 1;
                        }
                        folderPath = args[arg];
                        break;
                }
            }
            if (folderPath == null)
            {
                Usage();
                return 1;
            }

            TextWriter logWriter = Console.Out;
            string comparisonFilePath = null;
            bool firstRun = true;

            while (continuous || firstRun)
            {
                if (logFileName != null)
                {
                    logWriter = new StreamWriter(logFileName, true);
                }

                int deletedFileCount = 0;
                firstRun = false;
                if (verbose > 0)
                {
                    logWriter.WriteLine("==========================");
                    logWriter.WriteLine("Prune " + folderPath + " at " + DateTime.Now + " (" + GetTimeZoneName() + ")");
                    logWriter.WriteLine("==========================");
                    logWriter.Flush();
                }

                foreach (string filePath in Directory.EnumerateFiles(folderPath, "*.jpg"))
                {
                    if (comparisonFilePath == null)
                    {
                        comparisonFilePath = filePath;
                    }
                    else if (string.Compare(comparisonFilePath, filePath) < 0)
                    {
                        if (verbose > 1)
                        {
                            logWriter.WriteLine("Compare " + filePath + " with " + comparisonFilePath);
                        }

                        Comparison c = new Comparison(comparisonFilePath, filePath);
                        try
                        {
                            c.CompareImages(false);

                            if (verbose > 1)
                            {
                                logWriter.WriteLine("\tAAD: " + c.AverageAbsoluteDifferenceText + "\tASD: " + c.AverageSquareDifferenceText);
                            }

                            if (c.AverageSquareDifference < threshold)
                            {
                                ++deletedFileCount;
                                if (verbose > 0)
                                {
                                    logWriter.WriteLine("Old is same as new (ASD = " + c.AverageSquareDifferenceText + "), delete new: " + Path.GetFileName(filePath));
                                }
                                if (!dryRun)
                                {
                                    if (verbose > 1)
                                    {
                                        logWriter.WriteLine("Delete " + filePath);
                                    }
                                    File.Delete(filePath);
                                }
                            }
                            else
                            {
                                comparisonFilePath = filePath;
                            }
                        }
                        // I've seen two kinds of errors, but we should survive any.
                        // I've seen IOExceptions because some other process had the file open, and
                        // I've seen a NotSupportedException due to a failure to create the decoder because of file corruption issues.
                        catch (IOException e)
                        {
                            Console.Error.WriteLine("IOException: " + e.Message);
                            Console.Error.WriteLine("Files");
                            Console.Error.WriteLine("\tLeft:\t" + comparisonFilePath);
                            Console.Error.WriteLine("\tRight:\t" + filePath);
                            if (logWriter != Console.Out)
                            {
                                logWriter.WriteLine("IOException: " + e.Message);
                                logWriter.WriteLine("Files");
                                logWriter.WriteLine("\tLeft:\t" + comparisonFilePath);
                                logWriter.WriteLine("\tRight:\t" + filePath);
                            }
                            comparisonFilePath = filePath;
                        }
                        catch (NotSupportedException e)
                        {
                            Console.Error.WriteLine("NotSupportedException: " + e.Message);
                            Console.Error.WriteLine("Files");
                            Console.Error.WriteLine("\tLeft:\t" + comparisonFilePath);
                            Console.Error.WriteLine("\tRight:\t" + filePath);
                            if (logWriter != Console.Out)
                            {
                                logWriter.WriteLine("NotSupportedException: " + e.Message);
                                logWriter.WriteLine("Files");
                                logWriter.WriteLine("\tLeft:\t" + comparisonFilePath);
                                logWriter.WriteLine("\tRight:\t" + filePath);
                            }
                            comparisonFilePath = filePath;
                        }
                    }
                    else
                    {
                        // This is the case where continuous is true, and
                        // we've already seen the file in the prior iteration.
                    }

                    logWriter.Flush();
                }

                if (verbose > 0)
                {
                    logWriter.WriteLine("Deleted " + deletedFileCount + " files.");

                    if (continuous)
                    {
                        logWriter.WriteLine("Sleeping " + continuousWait + " milliseconds.");
                    }
                    logWriter.Flush();
                }

                if (logWriter != Console.Out)
                {
                    logWriter.Close();
                }

                if (continuous)
                {
                    System.Threading.Thread.Sleep(continuousWait);
                }
            }

            return 0;
        }