阅读Windows Live照片库插入的“人物标签”

时间:2009-09-25 02:44:30

标签: c# image metadata xmp

照片库可让您标记人脸并为其应用标签。我知道它会将标签直接插入到文件中,而不是将其存储在数据库或随附的元文件中。

如果这是真的,它插入的是什么数据以及它是如何格式化的?

2 个答案:

答案 0 :(得分:4)

这是我想要的代码。它在C#中。

        public void ReadWLPGRegions(string sourceFile)
    {
        string microsoftRegions = @"/xmp/RegionInfo/Regions";
        string microsoftPersonDisplayName = @"/PersonDisplayName";
        string microsoftRectangle = @"/Rectangle";
        BitmapCreateOptions createOptions = BitmapCreateOptions.PreservePixelFormat | BitmapCreateOptions.IgnoreColorProfile;

        using (Stream sourceStream = File.Open(sourceFile, FileMode.Open, FileAccess.Read))
        {
            BitmapDecoder sourceDecoder = BitmapDecoder.Create(sourceStream, createOptions, BitmapCacheOption.None);

            // Check source has valid frames
            if (sourceDecoder.Frames[0] != null && sourceDecoder.Frames[0].Metadata != null)
            {
                BitmapMetadata sourceMetadata = sourceDecoder.Frames[0].Metadata as BitmapMetadata;

                // Check there is a RegionInfo
                if (sourceMetadata.ContainsQuery(microsoftRegions))
                {
                    BitmapMetadata regionsMetadata = sourceMetadata.GetQuery(microsoftRegions) as BitmapMetadata;

                    // Loop through each Region
                    foreach (string regionQuery in regionsMetadata)
                    {
                        string regionFullQuery = microsoftRegions + regionQuery;

                        // Query for all the data for this region
                        BitmapMetadata regionMetadata = sourceMetadata.GetQuery(regionFullQuery) as BitmapMetadata;

                        if (regionMetadata != null)
                        {
                            if (regionMetadata.ContainsQuery(microsoftPersonDisplayName) &&
                                regionMetadata.ContainsQuery(microsoftRectangle))
                            {
                                Console.Writeline( regionMetadata.GetQuery(microsoftRectangle).ToString()));
                                 Console.WriteLine(regionMetadata.GetQuery(microsoftPersonDisplayName).ToString()));
                            }

                        }
                    }
                }
            }
        }
    }

答案 1 :(得分:1)

如果可能,Windows Live照片库使用XMP将元数据写入图片文件。有关详细信息,请参阅Metadata and the Windows Vista Photo Gallery