1
0

ReadDataBase.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.Data.SQLite;
  7. using Newtonsoft.Json;
  8. using System.Security.Cryptography;
  9. using System.Windows;
  10. namespace ThyroidDescriptionUI
  11. {
  12. /// Canvas上绘制的目标有几种类型
  13. public enum EnumCanvasItemType
  14. {
  15. Drawing,
  16. Choosed,
  17. Existed,
  18. }
  19. /// 线的种类
  20. public enum EnumCanvasLineType
  21. {
  22. Horizontal,
  23. Vertical,
  24. }
  25. /// 病灶信息
  26. public class LesionInfo
  27. {
  28. // 病灶轮廓
  29. public List<MyPoint> LesionContour { get; set; } = new List<MyPoint>();
  30. // 回声
  31. public EchoPatternEnum EchoPattern { get; set; } = 0;
  32. // 病灶形状
  33. public ShapeEnum Shape { get; set; } = 0;
  34. // 边缘
  35. public MarginEnum Margin { get; set; } = 0;
  36. // 局灶性强回声
  37. public EchogenicFociEnum EchogenicFoci { get; set; } = 0;
  38. // 横径起点
  39. public MyPoint HorizontalPoint1 { get; set; } = null;
  40. /// 横径终点
  41. public MyPoint HorizontalPoint2 { get; set; } = null;
  42. /// 纵径起点
  43. public MyPoint VerticalPoint1 { get; set; } = null;
  44. /// 纵径终点
  45. public MyPoint VerticalPoint2 { get; set; } = null;
  46. /// 内容完整
  47. public bool IsCompleted
  48. {
  49. get
  50. {
  51. return LesionContour.Count > 0 /*&& ThyroidContour.Count > 0*/ && HorizontalPoint1 != null
  52. && HorizontalPoint2 != null && VerticalPoint1 != null && VerticalPoint2 != null;
  53. }
  54. }
  55. }
  56. /// 病灶描述GT图像信息
  57. public class GTImgInfo
  58. {
  59. public string ImageId { get; set; }
  60. /// 该图上所有病灶的标准描述
  61. public List<LesionInfo> Lesions { get; set; }
  62. /// 甲状腺轮廓
  63. public List<MyPoint> ThyroidContour { get; set; }
  64. }
  65. class DataBase
  66. {
  67. #region 数据库相关变量
  68. private readonly string _dataFolder = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.IO.Path.GetDirectoryName(
  69. AppDomain.CurrentDomain.BaseDirectory)), "LesionDescriptionGTDatas");
  70. public readonly string _dbPath;
  71. public readonly string _imageDataFolder;
  72. public List<GTImgInfo> _allImages = new List<GTImgInfo>();
  73. private volatile int _imgIndex = 0;
  74. public List<LesionInfo> _lesions = new List<LesionInfo>();
  75. private volatile int _lesionIndex = 0;
  76. private System.Windows.Point _mouseStartPos;
  77. // 绘制中的所有轮廓点的集合
  78. private List<System.Windows.Point> _drawingPoints = new List<System.Windows.Point>();
  79. private volatile bool _canAutoFinish = false;
  80. #endregion
  81. public DataBase(string dbpath, string imgPath)
  82. {
  83. _dbPath = dbpath;
  84. _imageDataFolder = imgPath;
  85. }
  86. public DataBase(string dbpath)
  87. {
  88. _dbPath = dbpath;
  89. }
  90. /// 读入数据库
  91. public bool ReadDataBase()
  92. {
  93. try
  94. {
  95. if (!File.Exists(_dbPath))
  96. {
  97. //throw new Exception("未找到存放待标注数据的文件:" + _dbPath);
  98. return false;
  99. }
  100. SQLiteConnection dbCon = new SQLiteConnection("Data Source=" + _dbPath);
  101. dbCon.Open();
  102. SQLiteCommand dbCmd;
  103. dbCmd = dbCon.CreateCommand();
  104. dbCmd.CommandText = "select * from GTDatas";
  105. SQLiteDataReader dbReader;
  106. dbReader = dbCmd.ExecuteReader();
  107. if (!dbReader.HasRows)
  108. {
  109. dbReader.Close();
  110. dbCmd.Dispose();
  111. dbCon.Close();
  112. dbCon.Dispose();
  113. throw new Exception("待标注的数据为空.");
  114. return false;
  115. }
  116. while (dbReader.Read())
  117. {
  118. string imgId = ((string)dbReader[0]).TrimEnd();
  119. string strLesions = ((string)dbReader[1]).TrimEnd();
  120. List<LesionInfo> lesions = JsonConvert.DeserializeObject<List<LesionInfo>>(strLesions);
  121. string strThyroidCont = ((string)dbReader[2]).TrimEnd();
  122. List<MyPoint> thyroidContour = JsonConvert.DeserializeObject<List<MyPoint>>(strThyroidCont);
  123. _allImages.Add(new GTImgInfo { ImageId = imgId, Lesions = lesions, ThyroidContour = thyroidContour });
  124. }
  125. dbReader.Close();
  126. dbCmd.Dispose();
  127. dbCmd = dbCon.CreateCommand();
  128. dbCmd.CommandText = "select * from LastModification";
  129. dbReader = dbCmd.ExecuteReader();
  130. if (!dbReader.HasRows)
  131. {
  132. _imgIndex = 0;
  133. }
  134. else
  135. {
  136. dbReader.Read();
  137. string imgId = ((string)dbReader[0]).TrimEnd();
  138. int imgIndex = _allImages.FindIndex(x => x.ImageId == imgId); //最后一个修改的图像索引
  139. if (imgIndex == -1)
  140. {
  141. _imgIndex = 0;
  142. }
  143. else
  144. {
  145. _imgIndex = imgIndex;
  146. }
  147. }
  148. dbReader.Close();
  149. dbCmd.Dispose();
  150. dbCon.Close();
  151. dbCon.Dispose();
  152. return true;
  153. }
  154. catch (Exception excep)
  155. {
  156. throw new Exception("读入数据库时出错:" + excep);
  157. }
  158. }
  159. private string ComputeHashCode(byte[] input)
  160. {
  161. MD5 md5 = MD5.Create();
  162. byte[] hash = md5.ComputeHash(input);
  163. StringBuilder sb = new StringBuilder();
  164. foreach (var b in hash)
  165. {
  166. sb.Append(b.ToString("x2"));
  167. }
  168. string hashstr = sb.ToString();
  169. sb.Clear();
  170. return hashstr;
  171. }
  172. /// 创建数据库
  173. public void CreateDataBase()
  174. {
  175. if (File.Exists(_dbPath))
  176. {
  177. throw new Exception("数据库已存在,请检查.");
  178. }
  179. // 创建数据库文件
  180. SQLiteConnection dbCon = new SQLiteConnection("Data Source=" + _dbPath);
  181. dbCon.Open();
  182. SQLiteCommand dbCmd;
  183. dbCmd = dbCon.CreateCommand();
  184. dbCmd.CommandText = "create table if not exists GTDatas(ImageId varchar(16),Lesions varchar(16),ThyroidContour varchar(16))";
  185. dbCmd.ExecuteNonQuery();
  186. dbCmd.Dispose();
  187. dbCmd = dbCon.CreateCommand();
  188. dbCmd.CommandText = "create table if not exists LastModification(ImageId varchar(16))";
  189. dbCmd.ExecuteNonQuery();
  190. dbCmd.Dispose();
  191. dbCon.Close();
  192. dbCon.Dispose();
  193. }
  194. }
  195. }