如何从JPEG图像中删除所有EXIF数据?
我发现很多例子都是使用不同的库来读取和编辑EXIF数据,但我只需要一个简单的例子来删除它。
这只是为了测试目的,所以即使是最丑陋和破解的方法也会有帮助 :)
我已经尝试搜索EXIF的起始/结束标记0xFFE1和0xFFE2。在我的情况下,最后一个不存在。
我最初在我的博客中使用WPF库写过这个,但由于Windows后端调用有点混乱,所以失败了。
我的最终解决方案也更快捷,基本上是通过字节修补来删除exif。快速而简单 :)
[编辑:博客文章有更新的代码]
namespace ExifRemover
{
public class JpegPatcher
{
public Stream PatchAwayExif(Stream inStream, Stream outStream)
{
byte[] jpegHeader = new byte[2];
jpegHeader[0] = (byte) inStream.ReadByte();
jpegHeader[1] = (byte) inStream.ReadByte();
if (jpegHeader[0] == 0xff && jpegHeader[1] == 0xd8)
{
SkipExifSection(inStream);
}
outStream.Write(jpegHeader,0,2);
int readCount;
byte[] readBuffer = new byte[4096];
while ((readCount = inStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
outStream.Write(readBuffer, 0, readCount);
return outStream;
}
private void SkipExifSection(Stream inStream)
{
byte[] header = new byte[2];
header[0] = (byte) inStream.ReadByte();
header[1] = (byte) inStream.ReadByte();
if (header[0] == 0xff && header[1] == 0xe1)
{
int exifLength = inStream.ReadByte();
exifLength = exifLength << 8;
exifLength |= inStream.ReadByte();
for (int i = 0; i < exifLength - 2; i++)
{
inStream.ReadByte();
}
}
}
}
}
SkipAppHeaderSection
(来自您的博客,而不是此处发布的代码)中,您正在成对迭代字节,并始终检查第一个是否为0xff
,第二个是否在0xe0
和0xef
之间。这如何指示JPG头的开始和结束?最好奇的是为什么成对中的第一个总是0xe0
。 - oscilatingcretin你应该避免对图像进行解码和重新编码,因为这会损害图像的质量。相反,你应该找到一种只修改元数据的方法。我没有尝试过,但我认为InPlaceBitmapMetadataWriter可以解决问题。
这太容易了,从这里使用jhead.exe:http://www.sentex.net/~mwandel/jhead/
如果您想要的话,可以制作一个小批处理文件,例如:
jhead.exe -purejpg *.jpg
它将从同一文件夹中的所有jpeg文件中删除所有元数据。