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

本文关键字:问题 方向 JPEG 元数据 读取 | 更新日期: 2023-09-27 17:58:27

我有一张在iPhone上拍摄的JPEG图像。在我的台式电脑(Windows Photo Viewer,Google Chrome等(上,方向不正确。

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

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

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

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

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

有谁知道还有哪些JPEG元数据可以包含所需的信息,以便知道它需要旋转,这是Aurigma使用的?

这是我用来读取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.
   }
}

有什么想法吗?

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

下面是一个解决 8 个方向值的代码片段。

首先注意几点:

EXIF ID 0x0112用于定向。这是一个有用的 EXIF ID 参考 http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/EXIF.html

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

此外,5029 可能应该是 0x502920521 与缩略图方向相关,尽管这里可能不是想要的。

东方形象:

注意:imgSystem.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);
}

您似乎忘记了您查找的方向 id 值是十六进制的。在你使用 112 的地方,你应该使用0x112。

本文解释了Windows如何进行定向处理,而这篇文章似乎与您正在做的事情非常相关。

从这篇文章来看,你需要检查ID 274

foreach (PropertyItem p in properties) {
      if (p.Id == 274) {
            Orientation = (int)p.Value[0];
         if (Orientation == 6)
            oldImage.RotateFlip(RotateFlipType.Rotate90FlipNone);
         if (Orientation == 8)
            oldImage.RotateFlip(RotateFlipType.Rotate270FlipNone);
      break;
      }
}

我结合了给定的答案和评论,得出了这个:

    MemoryStream stream = new MemoryStream(data);
    Image image = Image.FromStream(stream);
    foreach (var prop in image.PropertyItems) {
        if ((prop.Id == 0x0112 || prop.Id == 5029 || prop.Id == 274)) {
            var value = (int)prop.Value[0];
            if (value == 6) {
                image.RotateFlip(RotateFlipType.Rotate90FlipNone);
                break;
            } else if (value == 8) {
                image.RotateFlip(RotateFlipType.Rotate270FlipNone);
                break;
            } else if (value == 3) {
                image.RotateFlip(RotateFlipType.Rotate180FlipNone);
                break;
            } 
        }
    }

在这里发帖以防万一有人有同样的问题。我在生产中使用 WFP 和 GDI 阅读方向时遇到了问题。唯一有效的方法是使用:https://github.com/dlemstra/Magick.NET

代码相当简单:

var img = new MagickImage(inputStream);
img.AutoOrient();   // Fix orientation
img.Strip();        // remove all EXIF information
img.Write(outputPath);