ImageCompareCellView.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. //
  2. // ImageCompareCellView.swift
  3. // KdanAuto
  4. //
  5. // Created by 朱东勇 on 2022/11/21.
  6. //
  7. import Foundation
  8. import AppKit
  9. class ImageCompareCellView : NSTableCellView {
  10. @IBOutlet var _titleLbl : NSTextField!
  11. @IBOutlet var _degreeInfoLbl : NSTextField!
  12. @IBOutlet var _activityView : NSProgressIndicator!
  13. @IBOutlet var _contentView : NSView!
  14. @IBOutlet var _imageView01 : NSImageView!
  15. @IBOutlet var _imageView02 : NSImageView!
  16. @IBOutlet var _imageView03 : NSImageView!
  17. @IBOutlet var _imageView04 : NSImageView!
  18. @IBOutlet var _sepLine : NSView!
  19. @IBOutlet var _segmentedControl : NSSegmentedControl! // 0 - Compare, 1 - Cover Compare
  20. @IBOutlet var _showCoverBtn : NSButton!
  21. var _title : String!
  22. var _fileInfo : NSMutableDictionary!
  23. class func shared() -> ImageCompareCellView? {
  24. var objects : NSArray!
  25. Bundle.main.loadNibNamed("ImageCompareCellView", owner: nil, topLevelObjects: &objects)
  26. for tView in objects {
  27. if let tv = tView as? ImageCompareCellView {
  28. return tv
  29. }
  30. }
  31. return nil
  32. }
  33. override func awakeFromNib() {
  34. _sepLine.wantsLayer = true;
  35. _sepLine.layer?.backgroundColor = NSColor.lightGray.cgColor
  36. _imageView01.wantsLayer = true;
  37. _imageView02.wantsLayer = true;
  38. _imageView01.layer?.borderColor = NSColor.lightGray.cgColor;
  39. _imageView02.layer?.borderColor = NSColor.lightGray.cgColor;
  40. _imageView01.layer?.borderWidth = 0.5;
  41. _imageView02.layer?.borderWidth = 0.5;
  42. }
  43. // Setter & Getter
  44. public func setTitle(_ title:String) {
  45. _title = title;
  46. _titleLbl.stringValue = _title ?? ""
  47. }
  48. public func title() -> String? {
  49. return _title
  50. }
  51. public func setFileInfo(_ fileInfo:NSMutableDictionary) {
  52. _fileInfo = fileInfo;
  53. _segmentedControl.selectedSegment = 0;
  54. if nil != _fileInfo {
  55. autoreleasepool {
  56. let comparePath = _fileInfo.comparePath()
  57. let resultPath = _fileInfo.resultPath()
  58. _imageView03.image = nil;
  59. _imageView04.image = nil;
  60. DispatchQueue.global().async {
  61. autoreleasepool {
  62. let image = NSImage.init(contentsOfFile: comparePath)
  63. let image2 = NSImage.init(contentsOfFile: resultPath)
  64. DispatchQueue.main.async {
  65. autoreleasepool {
  66. self._imageView01.image = image;
  67. self._imageView02.image = image2;
  68. }
  69. }
  70. }
  71. }
  72. if (nil == _fileInfo.refFilePath()) {
  73. setTitle(String("[\(_fileInfo.objc().fileType()):\(_fileInfo.objc().name())]\(_fileInfo.fileName())"))
  74. }else {
  75. let lastPathComponent = NSString(string: _fileInfo.refFilePath()!).lastPathComponent
  76. setTitle(String("[\(_fileInfo.objc().fileType()):\(_fileInfo.objc().name())]\(_fileInfo.fileName())/\(lastPathComponent)"))
  77. }
  78. let degree = _fileInfo.degree()
  79. if degree == -1 {
  80. _degreeInfoLbl.stringValue = "对照文件不存在"
  81. _degreeInfoLbl.textColor = NSColor.red
  82. } else {
  83. if abs(degree - 100) > 0 {
  84. _degreeInfoLbl.textColor = NSColor.red
  85. }else {
  86. _degreeInfoLbl.textColor = NSColor.blue
  87. }
  88. _degreeInfoLbl.stringValue = NSString(format: "相似度:%.2f%%", degree) as String
  89. }
  90. }
  91. }else {
  92. setTitle("")
  93. _degreeInfoLbl.stringValue = ""
  94. }
  95. _activityView.isHidden = true;
  96. _activityView.stopAnimation(nil)
  97. _showCoverBtn.isHidden = _segmentedControl.indexOfSelectedItem == 0
  98. }
  99. public func fileInfo() -> NSMutableDictionary {
  100. return _fileInfo
  101. }
  102. // IBAction
  103. @IBAction func segmentAction(_ sender: NSSegmentedControl) {
  104. if nil == _fileInfo {
  105. _imageView01.image = nil;
  106. _imageView02.image = nil;
  107. _imageView03.image = nil;
  108. _imageView04.image = nil;
  109. return
  110. }
  111. _showCoverBtn.isHidden = sender.indexOfSelectedItem == 0
  112. if sender.indexOfSelectedItem == 0 {
  113. autoreleasepool {
  114. _imageView03.image = nil;
  115. _imageView04.image = nil;
  116. let comparePath = _fileInfo.comparePath()
  117. let resultPath = _fileInfo.resultPath()
  118. let image = NSImage.init(contentsOfFile: comparePath)
  119. _imageView01.image = image;
  120. let image2 = NSImage.init(contentsOfFile: resultPath)
  121. _imageView02.image = image2;
  122. }
  123. }else {
  124. autoreleasepool {
  125. _imageView01.image = nil;
  126. _imageView02.image = nil;
  127. _showCoverBtn.state = .on
  128. let comparePath = _fileInfo.comparePath()
  129. let resultPath = _fileInfo.resultPath()
  130. let image = NSImage.init(contentsOfFile: comparePath)
  131. _imageView03.image = image;
  132. self._imageView04.image = nil;
  133. _imageView04.isHidden = _showCoverBtn.state == .off || _segmentedControl.indexOfSelectedItem != 1
  134. // let image2 = NSImage.init(contentsOfFile: resultPath)
  135. // _imageView04.image = image2;
  136. let degree = _fileInfo.degree()
  137. if (abs(degree - 100) > 0) {
  138. _activityView.isHidden = false
  139. _activityView.startAnimation(sender)
  140. DispatchQueue.global().async {
  141. autoreleasepool {
  142. var image2 = nil as NSImage?
  143. autoreleasepool {
  144. let coverPath = NSString(format: "%@_cover.png", NSString(string: resultPath).deletingPathExtension) as String
  145. if FileManager.default.fileExists(atPath: coverPath) {
  146. image2 = NSImage.init(contentsOfFile: coverPath)
  147. }else {
  148. image2 = ImageProcess.processImage(resultPath, checkPath: comparePath)
  149. if nil != image2 {
  150. let rep = NSBitmapImageRep.init(cgImage: image2!.cgImage(forProposedRect: nil, context: nil, hints: nil)!)
  151. let data = rep.representation(using: NSBitmapImageRep.FileType.png, properties: [:]);
  152. let url = URL.init(fileURLWithPath: coverPath, isDirectory: false)
  153. try? data!.write(to: url)
  154. }
  155. }
  156. }
  157. DispatchQueue.main.sync {
  158. autoreleasepool {
  159. self._imageView04.image = image2
  160. self._activityView.isHidden = true
  161. self._activityView.stopAnimation(sender)
  162. }
  163. }
  164. }
  165. }
  166. }
  167. }
  168. }
  169. }
  170. // 留意该方法在子线程调用
  171. func processCompareImage() -> NSImage? {
  172. return autoreleasepool {
  173. let comparePath = _fileInfo.comparePath()
  174. let resultPath = _fileInfo.resultPath()
  175. let image = NSImage.init(contentsOfFile: comparePath)
  176. let image2 = NSImage.init(contentsOfFile: resultPath)
  177. DispatchQueue.main.sync {
  178. autoreleasepool {
  179. _imageView01.image = image;
  180. _imageView02.image = image2;
  181. _imageView03.image = nil;
  182. _imageView04.image = nil;
  183. _showCoverBtn.isHidden = true;
  184. }
  185. }
  186. let nimage = self.imageFroView(self)
  187. return nimage
  188. }
  189. }
  190. // 留意该方法在子线程调用
  191. func processCoverImage() -> NSImage? {
  192. return autoreleasepool {
  193. let comparePath = _fileInfo.comparePath()
  194. let resultPath = _fileInfo.resultPath()
  195. let image = NSImage.init(contentsOfFile: comparePath)
  196. DispatchQueue.main.sync {
  197. autoreleasepool {
  198. _imageView01.image = nil;
  199. _imageView02.image = nil;
  200. self._imageView03.image = image;
  201. self._imageView03.isHidden = false
  202. self._imageView04.image = nil;
  203. self._imageView04.isHidden = false
  204. }
  205. }
  206. var image2 = nil as NSImage?
  207. let coverPath = NSString(format: "%@_cover.png", NSString(string: resultPath).deletingPathExtension) as String
  208. if FileManager.default.fileExists(atPath: coverPath) {
  209. image2 = NSImage.init(contentsOfFile: coverPath)
  210. }else {
  211. image2 = ImageProcess.processImage(resultPath, checkPath: comparePath)
  212. if nil != image2 {
  213. let rep = NSBitmapImageRep.init(cgImage: image2!.cgImage(forProposedRect: nil, context: nil, hints: nil)!)
  214. let data = rep.representation(using: NSBitmapImageRep.FileType.png, properties: [:]);
  215. let url = URL.init(fileURLWithPath: coverPath, isDirectory: false)
  216. try? data!.write(to: url)
  217. }
  218. }
  219. DispatchQueue.main.sync {
  220. autoreleasepool {
  221. self._imageView04.image = image2
  222. }
  223. }
  224. let nimage = self.imageFroView(self)
  225. return nimage
  226. }
  227. }
  228. func imageFroView(_ view:NSView) -> NSImage? {
  229. var image = nil as NSImage?;
  230. autoreleasepool {
  231. var data = nil as Data?
  232. if (Thread.isMainThread) {
  233. data = view.dataWithPDF(inside: view.frame)
  234. }else {
  235. DispatchQueue.main.sync {
  236. data = view.dataWithPDF(inside: view.frame)
  237. }
  238. }
  239. if data != nil {
  240. image = NSImage.init(data: data!);
  241. }
  242. }
  243. return image ?? nil;
  244. }
  245. @IBAction func showCoverAction(_ sender:NSButton) {
  246. if sender.state == .on {
  247. _showCoverBtn.title = "隐藏覆盖层"
  248. }else {
  249. _showCoverBtn.title = "显示覆盖层"
  250. }
  251. _imageView04.isHidden = _showCoverBtn.state == .off || _segmentedControl.indexOfSelectedItem != 1
  252. }
  253. }