首先是對圖片進行處理:
//1.縮小圖片尺寸
func scaleToSize(img: UIImage, size: CGSize) -> UIImage {
UIGraphicsBeginImageContext(size)
img.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return scaledImage!
}
//2.簡化色彩 將圖片轉換成灰度圖片
func getGrayImage(sourceImage: UIImage) -> UIImage {
let imageRef: CGImage = sourceImage.cgImage!
let width: Int = imageRef.width
let height: Int = imageRef.height
let colorSpace = CGColorSpaceCreateDeviceGray()
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue)
let context: CGContext = CGContext(data: nil, width: width, height: height, bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)!
let rect: CGRect = CGRect.init(x: 0, y: 0, width: width, height: height)
context.draw(imageRef, in: rect)
let outPutImage: CGImage = context.makeImage()!
let newImage: UIImage = UIImage.init(cgImage: outPutImage)
return newImage
}
//3. 計算平均值, 比較像素的灰度
func pHashValueWithImage(image: UIImage) -> NSString {
let pHashString = NSMutableString()
let imageRef = image.cgImage!
let width = imageRef.width
let height = imageRef.height
let pixelData = imageRef.dataProvider!.data
let data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData)
var sum: Int = 0
for i in 0..<width * height {
if data[i] != 0 {
sum = sum + Int(data[i])
}
}
let avr = sum / (width * height)
for i in 0..<width * height {
if Int(data[i]) >= avr {
pHashString.append("1")
} else {
pHashString.append("0")
}
}
return pHashString
}
//4.計算哈希值 如果不相同的數據位不超過5,就說明兩張圖片很相似;如果大於10,就說明這是兩張不同的圖片。
func getDifferentValueCountWithString(str1: NSString, str2: NSString) -> NSInteger {
var diff: NSInteger = 0
let s1 = str1.utf8String!
let s2 = str2.utf8String!
for i in 0..<str1.length {
if s1[i] != s2[i] {
diff += 1
}
}
return diff
}
寫個共用方法來比較兩個圖片是否相同
/// 比較兩個圖片是否相同, 這裏比較尺寸爲20*20
///
/// - Parameters:
/// - imageOne: 圖片1
/// - imageTwo: 圖片2
/// - Returns: 是否相同的布爾值
func isEqualImage(imageOne: UIImage, imageTwo: UIImage) -> Bool {
var equalResult = false
let mImageOne = self.getGrayImage(sourceImage: self.scaleToSize(img: imageOne, size: CGSize(width: 20, height: 20)))
let mImageTwo = self.getGrayImage(sourceImage: self.scaleToSize(img: imageTwo, size: CGSize(width: 20, height: 20)))
let diff = self.getDifferentValueCountWithString(str1: self.pHashValueWithImage(image: mImageOne), str2: self.pHashValueWithImage(image: mImageTwo))
print(diff)
if diff > 10 {
equalResult = false
} else {
equalResult = true
}
return equalResult
}
通過調用上面的共用方法,傳入兩個待比較的UIImage對象, 得到返回的Bool, 根據該布爾值進行相應的操作