using System; using System.IO; using System.Text; using System.Collections.Generic; using System.Drawing; using System.Data.SQLite; using Newtonsoft.Json; using System.Security.Cryptography; using System.Windows; namespace ThyroidDescriptionUI { /// Canvas上绘制的目标有几种类型 public enum EnumCanvasItemType { Drawing, Choosed, Existed, } /// 线的种类 public enum EnumCanvasLineType { Horizontal, Vertical, } /// 病灶信息 public class LesionInfo { // 病灶轮廓 public List LesionContour { get; set; } = new List(); // 回声 public EchoPatternEnum EchoPattern { get; set; } = 0; // 病灶形状 public ShapeEnum Shape { get; set; } = 0; // 边缘 public MarginEnum Margin { get; set; } = 0; // 局灶性强回声 public EchogenicFociEnum EchogenicFoci { get; set; } = 0; // 横径起点 public MyPoint HorizontalPoint1 { get; set; } = null; /// 横径终点 public MyPoint HorizontalPoint2 { get; set; } = null; /// 纵径起点 public MyPoint VerticalPoint1 { get; set; } = null; /// 纵径终点 public MyPoint VerticalPoint2 { get; set; } = null; /// 内容完整 public bool IsCompleted { get { return LesionContour.Count > 0 /*&& ThyroidContour.Count > 0*/ && HorizontalPoint1 != null && HorizontalPoint2 != null && VerticalPoint1 != null && VerticalPoint2 != null; } } } /// 病灶描述GT图像信息 public class GTImgInfo { public string ImageId { get; set; } /// 该图上所有病灶的标准描述 public List Lesions { get; set; } /// 甲状腺轮廓 public List ThyroidContour { get; set; } } class DataBase { #region 数据库相关变量 private readonly string _dataFolder = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.IO.Path.GetDirectoryName( AppDomain.CurrentDomain.BaseDirectory)), "LesionDescriptionGTDatas"); public readonly string _dbPath; public readonly string _imageDataFolder; public List _allImages = new List(); private volatile int _imgIndex = 0; public List _lesions = new List(); private volatile int _lesionIndex = 0; private System.Windows.Point _mouseStartPos; // 绘制中的所有轮廓点的集合 private List _drawingPoints = new List(); private volatile bool _canAutoFinish = false; #endregion public DataBase(string dbpath, string imgPath) { _dbPath = dbpath; _imageDataFolder = imgPath; } public DataBase(string dbpath) { _dbPath = dbpath; } /// 读入数据库 public bool ReadDataBase() { try { if (!File.Exists(_dbPath)) { //throw new Exception("未找到存放待标注数据的文件:" + _dbPath); return false; } SQLiteConnection dbCon = new SQLiteConnection("Data Source=" + _dbPath); dbCon.Open(); SQLiteCommand dbCmd; dbCmd = dbCon.CreateCommand(); dbCmd.CommandText = "select * from GTDatas"; SQLiteDataReader dbReader; dbReader = dbCmd.ExecuteReader(); if (!dbReader.HasRows) { dbReader.Close(); dbCmd.Dispose(); dbCon.Close(); dbCon.Dispose(); throw new Exception("待标注的数据为空."); return false; } while (dbReader.Read()) { string imgId = ((string)dbReader[0]).TrimEnd(); string strLesions = ((string)dbReader[1]).TrimEnd(); List lesions = JsonConvert.DeserializeObject>(strLesions); string strThyroidCont = ((string)dbReader[2]).TrimEnd(); List thyroidContour = JsonConvert.DeserializeObject>(strThyroidCont); _allImages.Add(new GTImgInfo { ImageId = imgId, Lesions = lesions, ThyroidContour = thyroidContour }); } dbReader.Close(); dbCmd.Dispose(); dbCmd = dbCon.CreateCommand(); dbCmd.CommandText = "select * from LastModification"; dbReader = dbCmd.ExecuteReader(); if (!dbReader.HasRows) { _imgIndex = 0; } else { dbReader.Read(); string imgId = ((string)dbReader[0]).TrimEnd(); int imgIndex = _allImages.FindIndex(x => x.ImageId == imgId); //最后一个修改的图像索引 if (imgIndex == -1) { _imgIndex = 0; } else { _imgIndex = imgIndex; } } dbReader.Close(); dbCmd.Dispose(); dbCon.Close(); dbCon.Dispose(); return true; } catch (Exception excep) { throw new Exception("读入数据库时出错:" + excep); } } private string ComputeHashCode(byte[] input) { MD5 md5 = MD5.Create(); byte[] hash = md5.ComputeHash(input); StringBuilder sb = new StringBuilder(); foreach (var b in hash) { sb.Append(b.ToString("x2")); } string hashstr = sb.ToString(); sb.Clear(); return hashstr; } /// 创建数据库 public void CreateDataBase() { if (File.Exists(_dbPath)) { throw new Exception("数据库已存在,请检查."); } // 创建数据库文件 SQLiteConnection dbCon = new SQLiteConnection("Data Source=" + _dbPath); dbCon.Open(); SQLiteCommand dbCmd; dbCmd = dbCon.CreateCommand(); dbCmd.CommandText = "create table if not exists GTDatas(ImageId varchar(16),Lesions varchar(16),ThyroidContour varchar(16))"; dbCmd.ExecuteNonQuery(); dbCmd.Dispose(); dbCmd = dbCon.CreateCommand(); dbCmd.CommandText = "create table if not exists LastModification(ImageId varchar(16))"; dbCmd.ExecuteNonQuery(); dbCmd.Dispose(); dbCon.Close(); dbCon.Dispose(); } } }