在Android中用java写一个利用pHash算法,用来找出目录下所有具有相同图片的图片的方法
时间: 2024-01-21 19:15:51 浏览: 236
以下是一个利用pHash算法,在Android中用Java编写的查找目录下所有具有相同图片的图片的方法的示例代码:
```java
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.github.kilianB.hash.Hash;
import com.github.kilianB.hashAlgorithms.PerceptiveHash;
public class SimilarImageFinder {
private PerceptiveHash hashAlgo;
private Map<Hash, List<File>> hashMap;
public SimilarImageFinder() {
hashAlgo = new PerceptiveHash(32);
hashMap = new HashMap<>();
}
public void findSimilarImages(File dir) {
File[] files = dir.listFiles();
for (File file : files) {
if (file.isDirectory()) {
findSimilarImages(file);
} else {
Hash hash = hashAlgo.hash(file);
if (hashMap.containsKey(hash)) {
hashMap.get(hash).add(file);
} else {
List<File> fileList = new ArrayList<>();
fileList.add(file);
hashMap.put(hash, fileList);
}
}
}
}
public void printDuplicates() {
for (List<File> fileList : hashMap.values()) {
if (fileList.size() > 1) {
System.out.println("Duplicates found:");
for (File file : fileList) {
System.out.println(file.getAbsolutePath());
}
System.out.println();
}
}
}
}
```
这个类使用了开源的Java库“kilianB/pHash”,它提供了PerceptiveHash算法的实现。在构造函数中,我们初始化了哈希算法和哈希表。在"findSimilarImages()"方法中,我们递归遍历文件夹中的所有图片,计算每个文件的哈希值,并将它们添加到哈希表中。在"printDuplicates()"方法中,我们遍历哈希表中的所有哈希值,如果有两个及以上的文件具有相同的哈希值,则输出这些文件的路径。您可以将这个类实例化并调用它的"findSimilarImages()"方法和"printDuplicates()"方法来查找目录下所有具有相同图片的图片。
阅读全文
相关推荐
![exe](https://img-home.csdnimg.cn/images/20241231044909.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20210720083447.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)