UWP 写入图片 Exif 信息
本文告诉大家如何在 UWP 中,保存图片的时候,写入 Exif 信息,也就是如照片的 相机型号 制造商 光圈值等信息的写入
在 UWP 中,保存图片或照片需要用到图片编码器,在使用编码器写入前可以设置编码器写入图片的属性,此时就可以包含了 Exif 信息。关于啥是 Exif 信息,还请自行百度
不同的图片格式可以支持的 Exif 信息范围不相同,咱以下使用 jpg 图片作为例子。如果大家切换为其他图片格式,还请自行测试一下
在创建编码器可以在构造函数传入参数,通过参数设置一些 Exif 信息,如质量信息。下面代码在创建时传入质量信息
BitmapPropertySet propertySet = new BitmapPropertySet();
BitmapTypedValue qualityValue = new BitmapTypedValue(0.77, PropertyType.Single);
propertySet.Add("ImageQuality", qualityValue);
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, pngStream.AsRandomAccessStream(), propertySet);
上面代码的 pngStream 是一个文件,用于写入图片,这部分代码不是本文重点,如果要获取全部的代码,还请到本文最后获取代码
在创建完成编码器之后,依然可以再次设置图片信息,通过调用 encoder.BitmapProperties.SetPropertiesAsync 方法进行设置
如以下代码,设置作者信息
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, pngStream.AsRandomAccessStream(), propertySet);
propertySet = new BitmapPropertySet();
// 作者
propertySet.Add("System.Author", new BitmapTypedValue("lindexi", PropertyType.String));
await encoder.BitmapProperties.SetPropertiesAsync(propertySet);
写入之后,可以右击图片文件的属性,进入详细信息。在详细信息里面可以看到图片的信息
以上有一个问题是,能写入属性有哪些,写入的类型是什么?这些可以从 官方文档 获取
如官方文档里面说写入相机型号的描述如下
propertyDescription
name = System.Photo.CameraManufacturer
shellPKey = PKEY_Photo_CameraManufacturer
formatID = 14B81DA1-0135-4D31-96D9-6CBFC9671A99
propID = 271
SearchInfo
InInvertedIndex = true
IsColumn = true
typeInfo
type = String
以上的含义就是写入的 Key 是 System.Photo.CameraManufacturer 要求传入的类型是 PropertyType.String 字符串,根据这个即可了解如何写以上的代码。如写入相机型号的描述等代码如下
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, pngStream.AsRandomAccessStream(), propertySet);
// https://docs.microsoft.com/en-us/windows/win32/properties/windows-properties-system?WT.mc_id=WD-MVP-5003260
propertySet = new BitmapPropertySet();
// 作者
propertySet.Add("System.Author", new BitmapTypedValue("lindexi", PropertyType.String));
// 相机型号
propertySet.Add("System.Photo.CameraModel", new BitmapTypedValue("lindexi", PropertyType.String));
// 制造商
propertySet.Add("System.Photo.CameraManufacturer", new BitmapTypedValue("lindexi manufacturer", PropertyType.String));
// 光圈值 System.Photo.FNumberNumerator/System.Photo.FNumberDenominator
propertySet.Add("System.Photo.FNumberNumerator", new BitmapTypedValue(1, PropertyType.UInt32));
propertySet.Add("System.Photo.FNumberDenominator", new BitmapTypedValue(10, PropertyType.UInt32));
await encoder.BitmapProperties.SetPropertiesAsync(propertySet);
下面代码是在加载页面,然后进行截图,保存截图到本地文件的代码
public MainPage()
{
this.InitializeComponent();
Loaded += MainPage_Loaded;
}
private async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
await Task.Delay(TimeSpan.FromSeconds(1));
var renderTargetBitmap = new RenderTargetBitmap();
await renderTargetBitmap.RenderAsync(Grid);
var pngFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync(Path.GetRandomFileName() + ".jpg");
using (var pngStream = await pngFile.OpenStreamForWriteAsync())
{
BitmapPropertySet propertySet = new BitmapPropertySet();
BitmapTypedValue qualityValue = new BitmapTypedValue(0.77, PropertyType.Single);
propertySet.Add("ImageQuality", qualityValue);
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, pngStream.AsRandomAccessStream(), propertySet);
// https://docs.microsoft.com/en-us/windows/win32/properties/windows-properties-system?WT.mc_id=WD-MVP-5003260
propertySet = new BitmapPropertySet();
// 作者
propertySet.Add("System.Author", new BitmapTypedValue("lindexi", PropertyType.String));
// 相机型号
propertySet.Add("System.Photo.CameraModel", new BitmapTypedValue("lindexi", PropertyType.String));
// 制造商
propertySet.Add("System.Photo.CameraManufacturer", new BitmapTypedValue("lindexi manufacturer", PropertyType.String));
// 光圈值 System.Photo.FNumberNumerator/System.Photo.FNumberDenominator
propertySet.Add("System.Photo.FNumberNumerator", new BitmapTypedValue(1, PropertyType.UInt32));
propertySet.Add("System.Photo.FNumberDenominator", new BitmapTypedValue(10, PropertyType.UInt32));
await encoder.BitmapProperties.SetPropertiesAsync(propertySet);
var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();
var softwareBitmap = SoftwareBitmap.CreateCopyFromBuffer(pixelBuffer, BitmapPixelFormat.Bgra8, renderTargetBitmap.PixelWidth, renderTargetBitmap.PixelHeight);
encoder.SetSoftwareBitmap(softwareBitmap);
await encoder.FlushAsync();
softwareBitmap.Dispose();
}
await Launcher.LaunchFolderAsync(ApplicationData.Current.TemporaryFolder);
}
本文代码可以到 写入图片Exif信息.7z-CSDN 下载
可以通过如下方式获取本文的源代码,先创建一个空文件夹,接着使用命令行 cd 命令进入此空文件夹,在命令行里面输入以下代码,即可获取到本文的代码
git init
git remote add origin https://gitee.com/lindexi/lindexi_gd.git
git pull origin acdca3ea99682d6549cf2622fb96685531ab9ded
以上使用的是 gitee 的源,如果 gitee 不能访问,请替换为 github 的源
git remote remove origin
git remote add origin https://github.com/lindexi/lindexi_gd.git
获取代码之后,进入 KechinabeleenalLechefahar 文件夹
UWP 写入图片 Exif 信息的更多相关文章
- Android 图片Exif信息相关的获取与修改
1 Exif是什么 Exif是一种图像文件格式,它的数据存储于JPEG格式是完全相同的,实际上Exif格式就是JPEG格式头插入了 数码照片的信息,包括拍摄的光圈.快门.平衡白.ISO.焦距.日期时间 ...
- Android--操作图片Exif信息
前言 在Android系统中,图片文件在内存中以像素点的二维数组加载,存放像素信息,还会在开头加上一些额外的照片拍摄参数信息,这些信息就是Exif.Android2.0之后,媒体库加入了操作图片Exi ...
- Android -- 加载大图片到内存,从gallery获取图片,获取图片exif信息
1. 加载大图片到内存,从gallery获取图片 android默认的最大堆栈只有16M, 图片像素太高会导致内存不足的异常, 需要将图片等比例缩小到适合手机屏幕分辨率, 再加载. 从gallery ...
- 改动图片exif信息
我们先了解一下EXIF: EXIF能够附加于JPEG.TIFF.RIFF等文件之中.为其添加有关数码相机拍摄信息的内容和索引图或图像处理软件的版本号信息. 全部的JPEG文件以字符串"0xF ...
- 七牛--关于图片上传方向不统一的问题--主要关于图片EXIF信息中旋转参数Orientation的理解
[图片引用方向纠正]直接在图片后面添加 ?imageMogr/auto-orient eg:http://data.upfitapp.com/data/2016/10/18/1629114767606 ...
- 图片Exif 信息中Orientation的理解和对此的处理
这个问题是在用七牛上传图片后获取宽高时发现的,一张图片,用图片浏览器打开始终是竖图,但是查看属性或者用七牛获取宽高,却发现宽大于高,也就是在属性中这是个横图.这样导致客户端用该宽高来展示图片会出现问题 ...
- Android 获取图片exif信息
使用android api读取图片的exif信息 布局代码: <LinearLayout xmlns:android="http://schemas.android.com/apk/r ...
- 七牛:关于图片 EXIF 信息中旋转参数 Orientation 的理解
EXIF(Exchangeable Image File)是 “可交换图像文件” 的缩写,当中包含了专门为数码相机的照片而定制的元数据,可以记录数码照片的拍摄参数.缩略图及其他属性信息,简单来说,Ex ...
- 图片Exif信息
Exif文件格式简述链接:https://www.zhihu.com/question/23727439/answer/25467748 可交换图像文件常被简称为Exif(Exchangeable i ...
- Java读取图片exif信息实现图片方向自动纠正
起因 一个对试卷进行OCR识别需求,需要实现一个功能,一个章节下的题目图片需要上下拼接合成一张大图,起初写了一个工具实现图片的合并,程序一直很稳定的运行着,有一反馈合成的图片方向不对,起初怀疑是本身图 ...
随机推荐
- 使用 NocoDB 一键将各种数据库转换为智能表格
NocoDB 是一款开源的无代码数据库平台,可以进行数据管理和应用开发.它的灵感来自 Airtable,支持与 Airtable 类似的电子表格式交互.关系型数据库 Schema 设计.API 自动生 ...
- Windows上部署spring boot jar项目
1.下载地址:https://github.com/winsw/winsw/releases 下载红色框内三个文件就够了. sample-allOptions.xml 所有配置参考 sample-mi ...
- KingbaseES 最老事务阻止vacuum freeze
前言 最近生产环境发生几次由于长事务导致表.库年龄没法回收的情况.我们要规避这种情况的发生,不要等发生了再去强制中断会话连接.当数据库中存在最老事务版本xmin,那么早于他的快照可以被标记为froze ...
- arch xfce启用自动挂载usb设备,自动访问usb设备,自动连接usb设备
1.安装gvfs sudo pacman -S gvfs GVFS(Gnome Virtual File System)是一个用于 GNOME 桌面环境的虚拟文件系统,它提供了一种统一的方式来访问和管 ...
- C++设计模式 - 建造者模式(Builder)
对象创建模式 通过"对象创建" 模式绕开new,来避免对象创建(new)过程中所导致的紧耦合(依赖具体类),从而支持对象创建的稳定.它是接口抽象之后的第一步工作. 典型模式 Fac ...
- AtCoder Beginner Contest 240
前言 考场把前六题切了,但是 E 题和 F 题罚时了,所以也写一写. ABC240 E - Ranges on Tree 题目传送门 分析 \(r\) 的最大值就是叶子的个数,如果将叶子按顺序编号, ...
- 深入解析 Java 面向对象编程与类属性应用
Java 面向对象编程 面向对象编程 (OOP) 是一种编程范式,它将程序组织成对象.对象包含数据和操作数据的方法. OOP 的优势: 更快.更易于执行 提供清晰的结构 代码更易于维护.修改和调试 提 ...
- 【FAQ】调用应用内支付SDK时报错,如何用tag对问题进行排查和分析
华为应用内支付服务(In-App Purchases,IAP)为开发者提供便捷的应用内支付体验和简便的接入流程.开发者的应用集成IAP SDK后,调用IAP SDK接口,启动IAP收银台,即可实现应用 ...
- java集合源码详解
一 Collection接口 1.List 1.1ArrayList 特点 1.底层实现基于动态数组,数组特点根据下表查找元素速度所以查找速度较快.继承自接口 Collection ->Lis ...
- openGauss每日一练第6天
学习地址 https://www.modb.pro/course/133 学习目标 学习 openGauss 创建模式.修改模式属性和删除模式 模式是一组数据库对象的集合,主要用于控制对数据库对象的访 ...