【问题标题】:How to read/write photo subject tags defined by user如何读取/写入用户定义的照片主题标签
【发布时间】:2013-11-11 05:18:43
【问题描述】:
您好,我对 Android 比较陌生,最近几天一直在搜索直到头疼(该喝咖啡了)。诸如digiKam (Linux) 之类的PC 程序允许您用关键字标记照片。您可以稍后在这些图像中搜索这些关键字中的匹配项。
我查看了Metadata-extractor,发现它可以将这些类型的标签读取到照片中的 XMP 或 IPTC 目录中。
有没有人有一个很好的简单例子来说明如何做到这一点?我只对标签的读/写感兴趣,因为搜索相对简单。
提前致谢
【问题讨论】:
标签:
java
android
image
metadata
jpeg
【解决方案1】:
好的,在喝了一杯好咖啡后,我经过反复试验找到了答案。
为了使用 Metadata-extractor 从隐藏在图像元数据中的 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 中,您可以在其他地方使用