Browse Source

图片对照 - 背景色过滤

innovator 2 years ago
parent
commit
7ba056b163

BIN
KdanAutoTest/KdanAuto.xcodeproj/project.xcworkspace/xcuserdata/zhudongyong.xcuserdatad/UserInterfaceState.xcuserstate


+ 2 - 2
KdanAutoTest/KdanAuto.xcodeproj/xcuserdata/zhudongyong.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

@@ -14,8 +14,8 @@
             filePath = "KdanAuto/Class/AutoTestCase/AutoTest.swift"
             startingColumnNumber = "9223372036854775807"
             endingColumnNumber = "9223372036854775807"
-            startingLineNumber = "506"
-            endingLineNumber = "506"
+            startingLineNumber = "565"
+            endingLineNumber = "565"
             landmarkName = "stringToImage(_:)"
             landmarkType = "7">
          </BreakpointContent>

+ 60 - 1
KdanAutoTest/KdanAuto/Class/AutoTestCase/AutoTest.swift

@@ -264,7 +264,61 @@ class AutoTest : NSObject, AutoTestProtocal {
         let maxWidth = min(rWidth, cWidth) - 1
         let maxHeight = min(rHeight, cHeight) - 1
         
+        // check background color
+        // 挑选图片 对角斜线 上的相素进行识别
+        var markInfo = NSMutableDictionary.init()
+        for w in 0...maxWidth {
+            let x = Int(w)
+            for mark in 0...1 {
+                let color = checkImageRep.colorAt(x: min(x, cWidth-1), y: min(mark == 0 ? x : (maxWidth - x), (cHeight-1))) as! NSColor
+                
+                let r = Int(color.redComponent*255)
+                let g = Int(color.greenComponent*255)
+                let b = Int(color.blueComponent*255)
+                
+                let key = String("\(r)-\(g)-\(b)")
+                
+                if (markInfo[key] != nil) {
+                    let count = (markInfo[key] as? NSNumber)!.intValue ?? 0
+                    
+                    markInfo[key] = NSNumber.init(value: count + 1)
+                }else {
+                    markInfo[key] = NSNumber.init(value: 1)
+                }
+            }
+        }
+        
+        var maxCount = Int(0);
+        var bgColorString : String? = nil
+        for key in markInfo.allKeys {
+            let count = (markInfo[key] as? NSNumber)!.intValue ?? 0
+                         
+            if count > maxCount {
+                maxCount = count
+                bgColorString = key as! String
+            }
+        }
+        
+        var bgColor : NSColor? = nil
+        if bgColorString != nil {
+            let coms = NSString(string: bgColorString!).components(separatedBy: "-")
+            
+            let r = NSString(string: coms[0]).intValue
+            let g = NSString(string: coms[1]).intValue
+            let b = NSString(string: coms[2]).intValue
+            bgColor = NSColor(red: CGFloat(CGFloat(r)/255.0),
+                              green: CGFloat(CGFloat(g)/255.0),
+                              blue: CGFloat(CGFloat(b)/255.0),
+                              alpha: 1)
+        }
+            
+        if nil != bgColor {
+            NSLog(String("识别到背景色\(bgColor)"))
+        }
+        
+        // Compare
         var equalCount = 0 as Double
+        var bgCount = 0 as Double
         for w in 0...maxWidth {
             let x = Int(w)
             
@@ -276,11 +330,16 @@ class AutoTest : NSObject, AutoTestProtocal {
                 
                 if (cColor.isEqual(to: rColor)) {
                     equalCount = equalCount + 1
+                    
+                    if bgColor != nil &&
+                        cColor.isEqual(to: bgColor) {
+                        bgCount += 1
+                    }
                 }
             }
         }
         
-        return Double(equalCount/(Double(cWidth) * Double(cHeight)) * 100.0)
+        return Double(max(equalCount-bgCount, 1)/(max(Double(cWidth) * Double(cHeight)-bgCount, 1)) * 100.0)
     }
     
     // Update Refrence image