如何从 JPEG 图像中删除所有 EXIF 数据?
我找到了很多关于如何使用各种库读取和编辑 EXIF 数据的示例,但我只需要一个关于如何删除它的简单示例。
它仅用于测试建议,因此即使是最丑陋和最骇人听闻的方法也会有所帮助:)
我已经尝试搜索 EXIF 开始/结束标记 0xFFE1 和 0xFFE2。最后一个在我的情况下不存在。
我第一次在我的博客中使用 WPF 库写过这个,但是由于 Windows 后端调用有点混乱,这种失败。
我的最终解决方案也更快,基本上是字节修补 jpeg 以删除 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();
}
}
}
}
}
我认为将文件读入位图对象并再次写入文件应该可以解决问题。
我记得在执行我的“图像旋转程序”时感到沮丧,因为它删除了 EXIF 数据。但在这种情况下,这正是你想要的!
您应该避免的是对图像进行解码和重新编码,因为这会损害质量。相反,您应该找到一种仅修改元数据的方法。我还没有尝试过,但我认为InPlaceBitmapMetadataWriter可以解决问题。
太简单了,从这里使用 jhead.exe:http ://www.sentex.net/~mwandel/jhead/
如果需要,请制作一个小批处理文件,例如:
jhead.exe -purejpg *.jpg
它将从同一文件夹中的所有 jpeg 中删除所有元数据。