使用 Java 执行反向图像搜索使开发人员能够根据图像的视觉内容在集合中查找相似的图像,而不是依赖关键字或元数据。此功能在图像检索、内容管理系统和数字资产管理等应用程序中尤其有价值。通过利用搜索库,Java 开发人员可以轻松实现反向图像搜索功能,以快速找到匹配的图像。在本文中,我们将介绍如何使用 Java 执行反向图像搜索并提供代码示例来帮助您入门。此功能对于处理各种图像格式(包括 PNG、JPEG 和 ZIP 存档)特别有用,可确保跨不同平台和文件类型的无缝兼容性。
使用 Java 执行反向图像搜索的步骤
- 将 GroupDocs.Search for Java 库集成到您的项目中以启用反向图像搜索功能
- 通过指定存储索引的文件夹来初始化 Index 对象
- 配置图像 IndexingOptions 以允许对容器项图像、嵌入图像和独立图像进行索引
- 使用配置的图像索引选项将文档添加到索引文件夹
- 设置 ImageSearchOptions,例如哈希差异、最大结果数以及要搜索的文档的过滤器
- 使用 SearchImage.create 方法定义图像文件的路径,为搜索创建参考图像
- 通过 Index.search 使用参考图像和定义的搜索选项在索引中执行图像搜索
- 遍历搜索结果并显示每个找到的图像的详细信息
要实现此功能,首先要创建一个索引,该索引存储图像及其相关元数据。IndexingOptions
类用于为各种图像类型(例如嵌入式图像、容器项图像和独立图像)启用索引。索引后,可以使用 SearchImage
类执行搜索,该类允许您指定参考图像并在索引文档中查找相似的匹配项。ImageSearchOptions
类为搜索提供了进一步的自定义,包括设置结果数量的限制以及指定要搜索的文档类型。此方法可在 Java 应用程序中实现高效的基于内容的图像检索。下面是 反向图像搜索的 Java 代码,它说明了如何在您的项目中应用这些功能。
使用 Java 执行反向图像搜索的代码
import com.groupdocs.search.Index; | |
import com.groupdocs.search.SearchDocumentFilter; | |
import com.groupdocs.search.common.SearchImage; | |
import com.groupdocs.search.licenses.License; | |
import com.groupdocs.search.options.ImageSearchOptions; | |
import com.groupdocs.search.options.IndexingOptions; | |
import com.groupdocs.search.results.FoundImageFrame; | |
import com.groupdocs.search.results.ImageSearchResult; | |
public class PerformReverseImageSearchusingJava { | |
public static void main(String[] args) throws Exception { | |
// Apply the license to remove the restrictions | |
// imposed by the Search library | |
License license = new License(); | |
license.setLicense("GroupDocs.Search.lic"); | |
// The path where the index will be stored | |
String indexFolder = "c:\\MyIndex\\"; | |
// The folder containing the documents you want to search | |
String documentsFolder = "c:\\MyDocuments\\"; | |
// Creating an index | |
Index index = new Index(indexFolder); | |
// Setting the image indexing options | |
IndexingOptions indexingOptions = new IndexingOptions(); | |
indexingOptions.getImageIndexingOptions().setEnabledForContainerItemImages(true); | |
indexingOptions.getImageIndexingOptions().setEnabledForEmbeddedImages(true); | |
indexingOptions.getImageIndexingOptions().setEnabledForSeparateImages(true); | |
// Indexing documents in a document folder | |
index.add(documentsFolder, indexingOptions); | |
// Setting the image search options | |
ImageSearchOptions imageSearchOptions = new ImageSearchOptions(); | |
imageSearchOptions.setHashDifferences(10); | |
imageSearchOptions.setMaxResultCount(10000); | |
imageSearchOptions.setSearchDocumentFilter(SearchDocumentFilter | |
.createFileExtension(".zip", ".png", ".jpg")); | |
// Creating a reference image for search | |
SearchImage searchImage = SearchImage.create("c:\\MyDocuments\\image.png"); | |
// Searching in the index | |
ImageSearchResult result = index.search(searchImage, imageSearchOptions); | |
System.out.print("Images found: " + result.getImageCount()); | |
for (int i = 0; i < result.getImageCount(); i++) { | |
FoundImageFrame image = result.getFoundImage(i); | |
System.out.print(image.getDocumentInfo().toString()); | |
} | |
} | |
} |
使用 Java 执行基于内容的图像检索是高效地根据内容定位相似图像的一项基本功能,它使应用程序能够在从桌面软件到基于云的服务等各种环境中快速准确地找到图像。包含用于索引和搜索图像的搜索库可确保平台独立性,使其与 Windows、macOS 和 Linux 兼容,同时还能够扩展以管理大型图像数据集。通过利用这些功能,开发人员可以构建强大的图像搜索系统,以改善用户体验并优化跨各种平台和应用程序的内容管理。这种方法不仅提高了搜索效率,还简化了对大量图像库的处理,确保顺利集成到现有工作流程中。
之前,我们发布了有关使用 Java 执行正则表达式搜索的详细指南。如需完整的分步说明,请阅读有关如何执行 使用 Java 的正则表达式搜索 的深入文章。