嗨,我对 Android 比较陌生,最近几天一直在搜索直到头痛(是时候喝杯咖啡了)。 PC 程序(例如 digiKam (Linux))允许您使用关键字标记照片。您稍后可以在这些图像中搜索这些关键字的匹配项。
我看过Metadata-extractor并看到它可以将这些类型的标签读取到照片中的 XMP 或 IPTC 目录。
有人有一个关于如何执行此操作的简单示例吗?我只是对标签的读/写感兴趣,因为搜索相对简单。
提前致谢
最佳答案
好吧,喝完一杯好咖啡后,经过一番尝试和错误,我找到了答案。
要使用元数据提取器从隐藏在图像元数据中的 Xmp 目录中读取标签,我使用了以下代码....
private void metadataMetaEx (File jpegFile)
{
Metadata metadata = null;
String tagInfo = null;
XmpDirectory xmpDirectory = null;
Map<String, String> xmp = null;
// Get all the metadata of the file
try {
metadata = ImageMetadataReader.readMetadata(jpegFile);
} catch (ImageProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Read in the metadata of the xmp directory
try
{
xmpDirectory = metadata.getDirectory(XmpDirectory.class);
} catch (NullPointerException e)
{
e.printStackTrace();
}
// Look through the xmp metadata for keys containing the word "Subject" and if a match add the value to the variable tagInfo
if (xmpDirectory != null)
{
xmp = xmpDirectory.getXmpProperties();
Iterator tags = xmp.keySet().iterator();
tagInfo = "Image Tags: ";
while (tags.hasNext())
{
String key=(String)tags.next();
if (key.contains("Subject"))
{
String value=(String)xmp.get(key);
tagInfo += value
+= "; ";
}
}
} }
这会将所有标签放入字符串变量 infoTag 中,您可以在其他地方使用
关于java - 如何读取/写入用户定义的照片主题标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19899344/