Commit 96cf42d9 authored by leichao.gao's avatar leichao.gao

相似度对比

parent 1f9a8191
package com.base.filerecoveryrecyclebin.utils
import android.graphics.BitmapFactory
import android.provider.MediaStore.Audio.Radio
import java.io.File
import kotlin.random.Random
......@@ -46,9 +47,39 @@ object SimilarHelper {
}
private fun similarPercent(srcFile: File, compareFile: File): Int {
return Random.nextInt(60, 100)
var similarityScore:Double = 0.00
try {
val bitmap1 = BitmapFactory.decodeFile(srcFile.absolutePath)
val bitmap2 = BitmapFactory.decodeFile(compareFile.absolutePath)
if (bitmap1.width != bitmap2.width || bitmap1.height != bitmap2.height) {
return 0 // 如果尺寸不同,则直接返回0
}
var totalDifference = 0L
for (x in 0 until bitmap1.width) {
for (y in 0 until bitmap1.height) {
val pixel1 = bitmap1.getPixel(x, y)
val pixel2 = bitmap2.getPixel(x, y)
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 = bitmap1.width * bitmap1.height * 3 * 255 * 255
similarityScore = (1 - totalDifference.toDouble() / maxDifference) * 100
} catch (e: Exception) {
e.printStackTrace()
}
// 将相似度得分四舍五入到整数
return similarityScore.toInt().coerceIn(0, 100)
}
}
\ No newline at end of file
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