Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Data.SQLite;
- using System.Data;
- using System.IO;
- using TagLib;
- namespace Azusa
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Получаю список файлов из {0}", args[0]);
- string[] files = Directory.GetFiles(args[0], "*.*", SearchOption.AllDirectories);
- List<img_info> img_list = new List<img_info>();
- for (int i = 0; i < files.Length; i++)
- {
- if (IsImageFile(files[i]))
- {
- img_info ii = ParsePhoto(files[i]);
- if (ii != null)
- {
- img_list.Add(ii);
- }
- Console.WriteLine("Обрабатываю: {0} [{1}/{2}]", Path.GetFileName(files[i]), i+1, files.Length);
- }
- }
- WriteDB(img_list, ".\\img_info.db");
- }
- static void WriteDB(List<img_info> img_list, string BaseName)
- {
- SQLiteConnection.CreateFile(BaseName);
- using (SQLiteConnection connection = new SQLiteConnection("data source=" + BaseName))
- {
- connection.Open();
- using (SQLiteCommand command = new SQLiteCommand(connection))
- {
- command.CommandText = @"CREATE TABLE [image_info] (
- [id] integer PRIMARY KEY AUTOINCREMENT NOT NULL,
- [hash] char(32) NOT NULL,
- [width] integer NOT NULL,
- [height] integer NOT NULL,
- [ratio] real NOT NULL
- );";
- command.CommandType = CommandType.Text;
- command.ExecuteNonQuery();
- }
- }
- //hguihuihihiu
- using (SQLiteConnection connection = new SQLiteConnection("data source=" + BaseName))
- {
- int count_file = 0;
- int all_files = img_list.Count;
- DateTime start = DateTime.Now;
- connection.Open();
- SQLiteTransaction transact = connection.BeginTransaction();
- foreach (img_info img in img_list)
- {
- count_file++;
- using (SQLiteCommand command = new SQLiteCommand(connection))
- {
- command.CommandText = "INSERT INTO image_info(hash, width, height, ratio) VALUES(@hash, @width, @height, @ratio);";
- command.Parameters.AddWithValue("hash", img.hash);
- command.Parameters.AddWithValue("width", img.Width);
- command.Parameters.AddWithValue("height", img.Height);
- command.Parameters.AddWithValue("ratio", img.Ratio);
- command.ExecuteNonQuery();
- }
- Console.WriteLine("Фаил {0} добавлен. [{1}/{2}]", Path.GetFileName(img.hash), count_file, all_files);
- }
- transact.Commit();
- DateTime finish = DateTime.Now;
- Console.WriteLine("Файлов проверено: {0} за: {1} секунд ({2} в секунду)", img_list.Count, (finish - start).TotalSeconds.ToString("0.00"), (img_list.Count / (finish - start).TotalSeconds));
- }
- }
- static bool IsImageFile(string s)
- {
- int t = s.LastIndexOf('.');
- if (t >= 0)
- {
- string ext = s.Substring(t).ToLower();
- switch (ext)
- {
- case ".jpg":
- return true;
- //break;
- case ".jpeg":
- return true;
- //break;
- case ".png":
- return true;
- //break;
- case ".bmp":
- return true;
- //break;
- case ".gif":
- return true;
- //break;
- case ".tif":
- return true;
- //break;
- case ".tiff":
- return true;
- //break;
- }
- }
- return false;
- }
- static img_info ParsePhoto(string path)
- {
- img_info ii = new img_info();
- TagLib.File file = null;
- try
- {
- file = TagLib.File.Create(path);
- }
- catch (TagLib.UnsupportedFormatException)
- {
- return null;
- }
- catch (TagLib.CorruptFileException)
- {
- return null;
- }
- var image = file as TagLib.Image.File;
- if (file == null)
- {
- return null;
- }
- if (image.Properties != null)
- {
- ii.Height = image.Properties.PhotoHeight;
- ii.Width = image.Properties.PhotoWidth;
- ii.Ratio = (float)ii.Width / (float)ii.Height;
- ii.hash = Path.GetFileNameWithoutExtension(path);
- }
- return null;
- }
- }
- class img_info
- {
- public string hash;
- public int Width;
- public int Height;
- public float Ratio;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement