Commit 9d4f3cf1 authored by wanglei's avatar wanglei

...

parent 4e7f8c4d
......@@ -96,38 +96,37 @@ object SimilarHelper {
// // 将相似度得分四舍五入到整数
// return similarityScore.toInt().coerceIn(0, 100)
// }
private fun similarPercent(srcFile: File, compareFile: File): Int {
fun similarPercent(srcFile: File, compareFile: File): Int {
val bitmap1 = BitmapFactory.decodeFile(srcFile.absolutePath)
val bitmap2 = BitmapFactory.decodeFile(compareFile.absolutePath)
if (bitmap1.width != bitmap2.width || bitmap1.height != bitmap2.height) {
return 0 // 如果尺寸不同,则直接返回0
}
val width = bitmap1.width
val height = bitmap1.height
val maxDifference = width * height * 3 * 255 * 255
val pixels1 = IntArray(width * height)
val pixels2 = IntArray(width * height)
bitmap1.getPixels(pixels1, 0, width, 0, 0, width, height)
bitmap2.getPixels(pixels2, 0, width, 0, 0, width, height)
var totalDifference = 0L
val pixel1 = IntArray(1)
val pixel2 = IntArray(1)
for (x in 0 until width) {
for (y in 0 until height) {
bitmap1.getPixel(x, y)
bitmap2.getPixel(x, y)
val red1 = pixel1[0] shr 16 and 0xff
val green1 = pixel1[0] shr 8 and 0xff
val blue1 = pixel1[0] and 0xff
val red2 = pixel2[0] shr 16 and 0xff
val green2 = pixel2[0] shr 8 and 0xff
val blue2 = pixel2[0] and 0xff
val redDiff = (red1 - red2) * (red1 - red2)
val greenDiff = (green1 - green2) * (green1 - green2)
val blueDiff = (blue1 - blue2) * (blue1 - blue2)
totalDifference += (redDiff + greenDiff + blueDiff)
for (i in pixels1.indices) {
val pixel1 = pixels1[i]
val pixel2 = pixels2[i]
if (pixel1 != pixel2) {
val redDifference = (pixel1 shr 16 and 0xff) - (pixel2 shr 16 and 0xff)
val greenDifference = (pixel1 shr 8 and 0xff) - (pixel2 shr 8 and 0xff)
val blueDifference = (pixel1 and 0xff) - (pixel2 and 0xff)
totalDifference += (redDifference * redDifference + greenDifference * greenDifference + blueDifference * blueDifference)
}
}
val maxDifference = width * height * 3 * 255 * 255
val similarityScore = (1 - totalDifference.toDouble() / maxDifference) * 100
return similarityScore.toInt().coerceIn(0, 100)
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment