如何获取图像属性 (HTML)

[ 本文适用于编写 Windows 运行时应用的 Windows 8.x 和 Windows Phone 8.x 开发人员。如果你要针对 Windows 10 进行开发,请参阅 最新文档 ]

这将向你展示如何使用 ImageProperties 对象从图像文件中检索属性。

如果没有 BitmapDecoder 对象,或者仅需要常用的图像属性,则我们建议你使用 ImageProperties 类。ImageProperties 为你提供基础数据,例如标题和拍摄日期。它还提供对 Windows 属性系统的访问,其中包含多个常用属性。 如需更详细的信息,可在以下位置找到完整的 Windows 属性命名空间:Windows 属性

注意  图像格式和编解码器仅支持某些属性。有关详细信息,请参阅照片元数据策略

 

你需要了解的内容

技术

先决条件

说明

步骤 1: 获取文件对象

编写用于接收 StorageFile 对象的函数的开头,并声明在其中存储所检索属性的变量。

function GetImageProperties(file) {
      var title;
      var orientation;
      var aperture;

在此处声明变量以将其保留在作用域内。

步骤 2: 检索基础属性

StorageFile 具有成员 Properties,该属性提供对文件上内容相关的属性的访问。你可以使用 properties.getImagePropertiesAsync 获取图像属性。

file.properties.getImagePropertiesAsync().then(function (imageProperties) {

如果具有 ImageProperties 对象,则你可以立即获取常用属性,例如标题和分级。

title = imageProperties.title;

注意  如果图像不包含特定属性,或者如果图像格式不支持该属性,则返回 null。在检索每个属性前必须检查该属性的存在性。

 

步骤 3: 检索 Windows 属性

还可以通过将一列属性键名传递给 imageProperties.retrievePropertiesAsync 来异步请求支持的 Windows 属性。

return imageProperties.retrievePropertiesAsync(["System.Photo.Orientation", “System.Photo.Aperture”]);
}).done(function (retrievedProperties) {

retrievedProperties 对象为一组键值对,其中每个键都是请求的 Windows 属性名,而值为相应的数据。当调用 lookup 函数时,它是同步的。retrievePropertiesAsync 函数处理该处理过程。

  orientation = retrievedProperties.lookup("System.Photo.Orientation");
    aperture = retrievedProperties.lookup("System.Photo.Aperture");

注意  如果图像不包含特定属性,或者如果图像格式不支持该属性,则返回 null。在检索每个属性前必须检查该属性的存在性。

 

备注

你可以使用 BitmapPropertiesView 获取 Windows 属性,例如 ImageProperties。但该属性通过使用 WIC 元数据查询语言提供对文件内本机元数据结构的低级别访问。有关详细信息,请参阅如何读取图像元数据

相关主题

简单的图像处理示例

Windows.Storage.FileProperties.ImageProperties

如何解码图像

如何读取图像处理元数据

Windows 属性

支持的 Windows 属性