【问题标题】:Store array in XMP dc:Subject field将数组存储在 XMP dc:Subject 字段中
【发布时间】:2015-10-27 09:52:27
【问题描述】:

我正在尝试复制 Adob​​e Lightroom 如何在图像 (JPEG) 结构中存储“关键字”。

到目前为止,我发现关键字存储在字段中的元数据中

/xmp/dc:主题

Light room 将每个标签存储为 xml 结构,格式为:

<dc:subject>
    <rdf:Bag>
        <rdf:li>Bianca</rdf:li>
        <rdf:li>KEYWORDS -LR</rdf:li>
        <rdf:li>Laura</rdf:li>
        <rdf:li>Lyndel</rdf:li>
        <rdf:li>T-ALL</rdf:li>
    </rdf:Bag>
</dc:subject>

我的问题是,当我用微软的元数据查询语言编写关键字时,关键字存储为字符串,因此图像中的关键字以以下格式存储:

<dc:subject>Bianca; KEYWORDS -LR; Laura; Lyndel; T-ALL</dc:subject>

所以,我的问题是如何存储和数组,所以结果类似于上面的xml结构。

下面是我的代码:

// Read the image stream
using (var originalFile = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.None))
{
    // Create the bitmap decoder
    decoder = BitmapDecoder.Create(originalFile, createOptions, BitmapCacheOption.None);

    // The the first frame contains the image metadata
    var bitmapFrame = decoder.Frames[0];

    if (bitmapFrame != null && bitmapFrame.Metadata != null)
    {
        // To be able to modify the metadata, first we need to clone it
        BitmapMetadata metadata = bitmapFrame.Metadata.Clone() as BitmapMetadata;

        // Remove the XP Subject field
        metadata.RemoveQuery("System.Subject");

        // Remove the XP keyword field
        metadata.RemoveQuery("System.Keywords");
        
        // Write Tags (Lightroom keywording)
        string keywords = "K1; K2; K3";
        metadata.SetQuery("/xmp/dc:Subject", keywords);
        
        // Create a bitmap encoder
        var encoder = CreateBitmapEncoder(imageFormat);

        // Create new frame with the updated metadata
        // Keep everything the same except the updated metadata
        encoder.Frames.Add(BitmapFrame.Create(
            bitmapFrame, bitmapFrame.Thumbnail, metadata, bitmapFrame.ColorContexts));

        // Attemp to save the update image
        using (Stream outputFile = File.Open(path + tempSufix, FileMode.Create, FileAccess.ReadWrite))
        {
            encoder.Save(outputFile);
        }
    }
}

编辑:

在此链接https://msdn.microsoft.com/en-us/library/ee719963%28v=vs.85%29.aspx 中显示,写作应在以下查询中:

/xmp/&lt;xmpbag&gt;dc:subject

但是&lt;xmpbag&gt; 是什么?

编辑2:

原来写'位置'如下:

/xmp/&lt;xmpbag&gt;dc:subject/{ulong=0}

/xmp/&lt;xmpbag&gt;dc:subject/{ulong=1}

【问题讨论】:

    标签: c# xmp


    【解决方案1】:
    // Read the image stream
    using (var originalFile = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.None))
    {
        // Create the bitmap decoder
        decoder = BitmapDecoder.Create(originalFile, createOptions, BitmapCacheOption.None);
    
        // The the first frame contains the image metadata
        var bitmapFrame = decoder.Frames[0];
    
        if (bitmapFrame != null && bitmapFrame.Metadata != null)
        {
            // To be able to modify the metadata, first we need to clone it
            BitmapMetadata metadata = bitmapFrame.Metadata.Clone() as BitmapMetadata;
    
            // Remove the XP Subject field
            metadata.RemoveQuery("System.Subject");
    
            // Remove the XP keyword field
            metadata.RemoveQuery("System.Keywords");
    
            // Write Tags (Lightroom keywording)
            var keywords = "K1; K2; K3";
            var keywordArray = keywords.Split(new[] { "; " }, StringSplitOptions.RemoveEmptyEntries);
    
            // Write in the XMP section
            metadata.SetQuery("/xmp/dc:Subject", new BitmapMetadata("xmpbag"));
            for (var i = 0; i < keywordArray.Length; i++)
            {
                var order = "/{ulong=" + i + "}";
                var keyword = keywordArray[i];
                metadata.SetQuery("/xmp/<xmpbag>dc:Subject" + order, keyword);
            }
    
            // Write in the IPTC section
            metadata.SetQuery("/app13/irb/8bimiptc/iptc/{str=Keywords}", keywordArray);
    
            // Create a bitmap encoder
            var encoder = CreateBitmapEncoder(imageFormat);
    
            // Create new frame with the updated metadata
            // Keep everything the same except the updated metadata
            encoder.Frames.Add(BitmapFrame.Create(
                bitmapFrame, bitmapFrame.Thumbnail, metadata, bitmapFrame.ColorContexts));
    
            // Attemp to save the update image
            using (Stream outputFile = File.Open(path + tempSufix, FileMode.Create, FileAccess.ReadWrite))
            {
                encoder.Save(outputFile);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-17
      • 2012-06-09
      • 2013-02-21
      • 1970-01-01
      相关资源
      最近更新 更多