gpt4 book ai didi

c# - 读取 JPEG 元数据时出现问题(方向)

转载 作者:IT王子 更新时间:2023-10-29 03:49:10 32 4
gpt4 key购买 nike

我有一张用 iphone 拍摄的 JPEG 图片。在我的台式电脑(Windows 照片查看器、谷歌浏览器等)上,方向不正确。

我正在开发一个需要上传照片的 ASP.NET MVC 3 Web 应用程序(目前正在使用 plupload)。

我有一些服务器端代码来处理图像,包括读取 EXIF 数据。

我尝试读取 EXIF 元数据中的 PropertyTagOrientation 字段(使用 GDI - Image.PropertyItems),但该字段不存在。

所以它要么是一些特定的 iphone 元数据,要么是一些其他元数据。

我使用了另一个工具,例如 Aurigma Photo Uploader,它可以正确读取元数据并旋转图像。它是如何做到这一点的?

有谁知道 Aurigma 使用哪些其他 JPEG 元数据可以包含需要旋转所需的信息?

这是我用来读取 EXIF 数据的代码:

var image = Image.FromStream(fileStream);

foreach (var prop in image.PropertyItems)
{
if (prop.Id == 112 || prop.Id == 5029)
{
// do my rotate code - e.g "RotateFlip"
// Never get's in here - can't find these properties.
}
}

有什么想法吗?

最佳答案

这是一个解决 8 个方向值的片段。

首先注意几点:

EXIF id 0x0112 用于定位。这是一个有用的 EXIF id 引用 http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/EXIF.html

0x0112274 的十六进制等价物。 PropertyItem.Id 的数据类型是 int,这意味着 274 在这里很有用。

此外,5029 可能应该是 0x502920521,这与 ThumbnailOrientation 相关,尽管这里可能不是我们想要的。

东方形象:

注意:img 是一个System.Drawing.Image 或继承自它,如System.Drawing.Bitmap

if (Array.IndexOf(img.PropertyIdList, 274) > -1)
{
var orientation = (int)img.GetPropertyItem(274).Value[0];
switch (orientation)
{
case 1:
// No rotation required.
break;
case 2:
img.RotateFlip(RotateFlipType.RotateNoneFlipX);
break;
case 3:
img.RotateFlip(RotateFlipType.Rotate180FlipNone);
break;
case 4:
img.RotateFlip(RotateFlipType.Rotate180FlipX);
break;
case 5:
img.RotateFlip(RotateFlipType.Rotate90FlipX);
break;
case 6:
img.RotateFlip(RotateFlipType.Rotate90FlipNone);
break;
case 7:
img.RotateFlip(RotateFlipType.Rotate270FlipX);
break;
case 8:
img.RotateFlip(RotateFlipType.Rotate270FlipNone);
break;
}
// This EXIF data is now invalid and should be removed.
img.RemovePropertyItem(274);
}

关于c# - 读取 JPEG 元数据时出现问题(方向),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6222053/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com