123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- //
- // ImageCompareCellView.swift
- // KdanAuto
- //
- // Created by 朱东勇 on 2022/11/21.
- //
- import Foundation
- import AppKit
- class ImageCompareCellView : NSTableCellView {
- @IBOutlet var _titleLbl : NSTextField!
- @IBOutlet var _degreeInfoLbl : NSTextField!
-
- @IBOutlet var _activityView : NSProgressIndicator!
-
- @IBOutlet var _contentView : NSView!
- @IBOutlet var _imageView01 : NSImageView!
- @IBOutlet var _imageView02 : NSImageView!
-
- @IBOutlet var _imageView03 : NSImageView!
- @IBOutlet var _imageView04 : NSImageView!
-
- @IBOutlet var _sepLine : NSView!
-
- @IBOutlet var _segmentedControl : NSSegmentedControl! // 0 - Compare, 1 - Cover Compare
-
- @IBOutlet var _showCoverBtn : NSButton!
-
- var _title : String!
- var _fileInfo : NSMutableDictionary!
-
- class func shared() -> ImageCompareCellView? {
- var objects : NSArray!
-
- Bundle.main.loadNibNamed("ImageCompareCellView", owner: nil, topLevelObjects: &objects)
-
- for tView in objects {
- if let tv = tView as? ImageCompareCellView {
- return tv
- }
- }
-
- return nil
- }
-
- override func awakeFromNib() {
- _sepLine.wantsLayer = true;
- _sepLine.layer?.backgroundColor = NSColor.lightGray.cgColor
-
- _imageView01.wantsLayer = true;
- _imageView02.wantsLayer = true;
- _imageView01.layer?.borderColor = NSColor.lightGray.cgColor;
- _imageView02.layer?.borderColor = NSColor.lightGray.cgColor;
- _imageView01.layer?.borderWidth = 0.5;
- _imageView02.layer?.borderWidth = 0.5;
- }
-
- // Setter & Getter
- public func setTitle(_ title:String) {
- _title = title;
-
- _titleLbl.stringValue = _title ?? ""
- }
-
- public func title() -> String? {
- return _title
- }
-
- public func setFileInfo(_ fileInfo:NSMutableDictionary) {
- _fileInfo = fileInfo;
-
- _segmentedControl.selectedSegment = 0;
-
- if nil != _fileInfo {
- autoreleasepool {
- let comparePath = _fileInfo.comparePath()
- let resultPath = _fileInfo.resultPath()
- _imageView03.image = nil;
- _imageView04.image = nil;
-
- DispatchQueue.global().async {
- autoreleasepool {
- let image = NSImage.init(contentsOfFile: comparePath)
-
- let image2 = NSImage.init(contentsOfFile: resultPath)
-
- DispatchQueue.main.async {
- autoreleasepool {
- self._imageView01.image = image;
- self._imageView02.image = image2;
- }
- }
- }
- }
-
- if (nil == _fileInfo.refFilePath()) {
- setTitle(String("[\(_fileInfo.objc().fileType()):\(_fileInfo.objc().name())]\(_fileInfo.fileName())"))
- }else {
- let lastPathComponent = NSString(string: _fileInfo.refFilePath()!).lastPathComponent
- setTitle(String("[\(_fileInfo.objc().fileType()):\(_fileInfo.objc().name())]\(_fileInfo.fileName())/\(lastPathComponent)"))
- }
-
- let degree = _fileInfo.degree()
- if degree == -1 {
- _degreeInfoLbl.stringValue = "对照文件不存在"
- _degreeInfoLbl.textColor = NSColor.red
- } else {
- if abs(degree - 100) > 0 {
- _degreeInfoLbl.textColor = NSColor.red
- }else {
- _degreeInfoLbl.textColor = NSColor.blue
- }
- _degreeInfoLbl.stringValue = NSString(format: "相似度:%.2f%%", degree) as String
- }
- }
- }else {
- setTitle("")
- _degreeInfoLbl.stringValue = ""
- }
-
-
- _activityView.isHidden = true;
- _activityView.stopAnimation(nil)
-
- _showCoverBtn.isHidden = _segmentedControl.indexOfSelectedItem == 0
- }
-
- public func fileInfo() -> NSMutableDictionary {
- return _fileInfo
- }
-
- // IBAction
- @IBAction func segmentAction(_ sender: NSSegmentedControl) {
- if nil == _fileInfo {
- _imageView01.image = nil;
- _imageView02.image = nil;
- _imageView03.image = nil;
- _imageView04.image = nil;
-
- return
- }
-
- _showCoverBtn.isHidden = sender.indexOfSelectedItem == 0
- if sender.indexOfSelectedItem == 0 {
- autoreleasepool {
- _imageView03.image = nil;
- _imageView04.image = nil;
-
- let comparePath = _fileInfo.comparePath()
- let resultPath = _fileInfo.resultPath()
-
- let image = NSImage.init(contentsOfFile: comparePath)
- _imageView01.image = image;
-
- let image2 = NSImage.init(contentsOfFile: resultPath)
- _imageView02.image = image2;
- }
- }else {
- autoreleasepool {
- _imageView01.image = nil;
- _imageView02.image = nil;
- _showCoverBtn.state = .on
-
- let comparePath = _fileInfo.comparePath()
- let resultPath = _fileInfo.resultPath()
-
-
- let image = NSImage.init(contentsOfFile: comparePath)
- _imageView03.image = image;
- self._imageView04.image = nil;
- _imageView04.isHidden = _showCoverBtn.state == .off || _segmentedControl.indexOfSelectedItem != 1
-
- // let image2 = NSImage.init(contentsOfFile: resultPath)
- // _imageView04.image = image2;
- let degree = _fileInfo.degree()
- if (abs(degree - 100) > 0) {
- _activityView.isHidden = false
- _activityView.startAnimation(sender)
- DispatchQueue.global().async {
- autoreleasepool {
- var image2 = nil as NSImage?
-
- autoreleasepool {
- let coverPath = NSString(format: "%@_cover.png", NSString(string: resultPath).deletingPathExtension) as String
-
- if FileManager.default.fileExists(atPath: coverPath) {
- image2 = NSImage.init(contentsOfFile: coverPath)
- }else {
- image2 = ImageProcess.processImage(resultPath, checkPath: comparePath)
-
- if nil != image2 {
- let rep = NSBitmapImageRep.init(cgImage: image2!.cgImage(forProposedRect: nil, context: nil, hints: nil)!)
- let data = rep.representation(using: NSBitmapImageRep.FileType.png, properties: [:]);
-
- let url = URL.init(fileURLWithPath: coverPath, isDirectory: false)
- try? data!.write(to: url)
- }
- }
- }
-
- DispatchQueue.main.sync {
- autoreleasepool {
- self._imageView04.image = image2
- self._activityView.isHidden = true
- self._activityView.stopAnimation(sender)
- }
- }
- }
- }
- }
- }
- }
- }
-
- // 留意该方法在子线程调用
- func processCompareImage() -> NSImage? {
-
- return autoreleasepool {
- let comparePath = _fileInfo.comparePath()
- let resultPath = _fileInfo.resultPath()
-
- let image = NSImage.init(contentsOfFile: comparePath)
-
- let image2 = NSImage.init(contentsOfFile: resultPath)
- DispatchQueue.main.sync {
- autoreleasepool {
- _imageView01.image = image;
- _imageView02.image = image2;
- _imageView03.image = nil;
- _imageView04.image = nil;
-
- _showCoverBtn.isHidden = true;
- }
- }
-
- let nimage = self.imageFroView(self)
- return nimage
- }
- }
-
- // 留意该方法在子线程调用
- func processCoverImage() -> NSImage? {
- return autoreleasepool {
- let comparePath = _fileInfo.comparePath()
- let resultPath = _fileInfo.resultPath()
-
- let image = NSImage.init(contentsOfFile: comparePath)
- DispatchQueue.main.sync {
- autoreleasepool {
- _imageView01.image = nil;
- _imageView02.image = nil;
-
- self._imageView03.image = image;
- self._imageView03.isHidden = false
- self._imageView04.image = nil;
- self._imageView04.isHidden = false
- }
- }
-
- var image2 = nil as NSImage?
-
- let coverPath = NSString(format: "%@_cover.png", NSString(string: resultPath).deletingPathExtension) as String
-
- if FileManager.default.fileExists(atPath: coverPath) {
- image2 = NSImage.init(contentsOfFile: coverPath)
- }else {
- image2 = ImageProcess.processImage(resultPath, checkPath: comparePath)
-
- if nil != image2 {
- let rep = NSBitmapImageRep.init(cgImage: image2!.cgImage(forProposedRect: nil, context: nil, hints: nil)!)
- let data = rep.representation(using: NSBitmapImageRep.FileType.png, properties: [:]);
-
- let url = URL.init(fileURLWithPath: coverPath, isDirectory: false)
- try? data!.write(to: url)
- }
- }
-
- DispatchQueue.main.sync {
- autoreleasepool {
- self._imageView04.image = image2
- }
- }
-
- let nimage = self.imageFroView(self)
- return nimage
- }
- }
-
- func imageFroView(_ view:NSView) -> NSImage? {
- var image = nil as NSImage?;
-
- autoreleasepool {
- var data = nil as Data?
- if (Thread.isMainThread) {
- data = view.dataWithPDF(inside: view.frame)
- }else {
- DispatchQueue.main.sync {
- data = view.dataWithPDF(inside: view.frame)
- }
- }
- if data != nil {
- image = NSImage.init(data: data!);
- }
- }
-
- return image ?? nil;
- }
-
- @IBAction func showCoverAction(_ sender:NSButton) {
- if sender.state == .on {
- _showCoverBtn.title = "隐藏覆盖层"
- }else {
- _showCoverBtn.title = "显示覆盖层"
- }
-
- _imageView04.isHidden = _showCoverBtn.state == .off || _segmentedControl.indexOfSelectedItem != 1
- }
-
- }
|