Advertisement
Guest User

ScanRez

a guest
Aug 29th, 2016
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Data.SQLite;
  7. using System.Data;
  8. using System.IO;
  9. using TagLib;
  10.  
  11. namespace Azusa
  12. {
  13.     class Program
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             Console.WriteLine("Получаю список файлов из {0}", args[0]);
  18.             string[] files = Directory.GetFiles(args[0], "*.*", SearchOption.AllDirectories);
  19.             List<img_info> img_list = new List<img_info>();
  20.             for (int i = 0; i < files.Length; i++)
  21.             {
  22.                 if (IsImageFile(files[i]))
  23.                 {
  24.                     img_info ii = ParsePhoto(files[i]);
  25.                     if (ii != null)
  26.                     {
  27.                         img_list.Add(ii);
  28.                     }
  29.                     Console.WriteLine("Обрабатываю: {0} [{1}/{2}]", Path.GetFileName(files[i]), i+1, files.Length);
  30.                 }
  31.             }
  32.             WriteDB(img_list, ".\\img_info.db");
  33.         }
  34.         static void WriteDB(List<img_info> img_list, string BaseName)
  35.         {
  36.             SQLiteConnection.CreateFile(BaseName);
  37.  
  38.             using (SQLiteConnection connection = new SQLiteConnection("data source=" + BaseName))
  39.             {
  40.                 connection.Open();
  41.  
  42.                 using (SQLiteCommand command = new SQLiteCommand(connection))
  43.                 {
  44.                     command.CommandText = @"CREATE TABLE [image_info] (
  45.                    [id] integer PRIMARY KEY AUTOINCREMENT NOT NULL,
  46.                    [hash] char(32) NOT NULL,
  47.                    [width] integer NOT NULL,
  48.                    [height] integer NOT NULL,
  49.                    [ratio] real NOT NULL
  50.                    );";
  51.                     command.CommandType = CommandType.Text;
  52.                     command.ExecuteNonQuery();
  53.                 }
  54.             }
  55.             //hguihuihihiu
  56.             using (SQLiteConnection connection = new SQLiteConnection("data source=" + BaseName))
  57.             {
  58.                 int count_file = 0;
  59.                 int all_files = img_list.Count;
  60.                 DateTime start = DateTime.Now;
  61.                 connection.Open();
  62.                 SQLiteTransaction transact = connection.BeginTransaction();
  63.                 foreach (img_info img in img_list)
  64.                 {
  65.                     count_file++;
  66.                     using (SQLiteCommand command = new SQLiteCommand(connection))
  67.                     {
  68.                         command.CommandText = "INSERT INTO image_info(hash, width, height, ratio) VALUES(@hash, @width, @height, @ratio);";
  69.                         command.Parameters.AddWithValue("hash", img.hash);
  70.                         command.Parameters.AddWithValue("width", img.Width);
  71.                         command.Parameters.AddWithValue("height", img.Height);
  72.                         command.Parameters.AddWithValue("ratio", img.Ratio);
  73.                         command.ExecuteNonQuery();
  74.                     }
  75.                     Console.WriteLine("Фаил {0} добавлен. [{1}/{2}]", Path.GetFileName(img.hash), count_file, all_files);
  76.                 }
  77.                 transact.Commit();
  78.                 DateTime finish = DateTime.Now;
  79.                 Console.WriteLine("Файлов проверено: {0} за: {1} секунд ({2} в секунду)", img_list.Count, (finish - start).TotalSeconds.ToString("0.00"), (img_list.Count / (finish - start).TotalSeconds));
  80.             }
  81.         }
  82.         static bool IsImageFile(string s)
  83.         {
  84.             int t = s.LastIndexOf('.');
  85.             if (t >= 0)
  86.             {
  87.                 string ext = s.Substring(t).ToLower();
  88.                 switch (ext)
  89.                 {
  90.                     case ".jpg":
  91.                         return true;
  92.                     //break;
  93.                     case ".jpeg":
  94.                         return true;
  95.                     //break;
  96.                     case ".png":
  97.                         return true;
  98.                     //break;
  99.                     case ".bmp":
  100.                         return true;
  101.                     //break;
  102.                     case ".gif":
  103.                         return true;
  104.                     //break;
  105.                     case ".tif":
  106.                         return true;
  107.                     //break;
  108.                     case ".tiff":
  109.                         return true;
  110.                         //break;
  111.  
  112.                 }
  113.             }
  114.             return false;
  115.         }
  116.         static img_info ParsePhoto(string path)
  117.         {
  118.             img_info ii = new img_info();
  119.             TagLib.File file = null;
  120.             try
  121.             {
  122.                 file = TagLib.File.Create(path);
  123.             }
  124.             catch (TagLib.UnsupportedFormatException)
  125.             {
  126.                 return null;
  127.             }
  128.             catch (TagLib.CorruptFileException)
  129.             {
  130.                 return null;
  131.             }
  132.  
  133.             var image = file as TagLib.Image.File;
  134.             if (file == null)
  135.             {
  136.                 return null;
  137.             }
  138.             if (image.Properties != null)
  139.             {
  140.                 ii.Height = image.Properties.PhotoHeight;
  141.                 ii.Width = image.Properties.PhotoWidth;
  142.                 ii.Ratio = (float)ii.Width / (float)ii.Height;
  143.                 ii.hash = Path.GetFileNameWithoutExtension(path);
  144.             }
  145.             return null;
  146.         }
  147.     }
  148.     class img_info
  149.     {
  150.         public string hash;
  151.         public int Width;
  152.         public int Height;
  153.         public float Ratio;
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement