阅读JPEG元数据的问题(方向)

RPM*_*984 51 c# jpeg metadata gdi image-processing

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

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

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

我已经尝试PropertyTagOrientation在EXIF元数据中读取字段(使用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.
   }
}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

Ree*_*gnE 118

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

先说几点:

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

0x0112是等效于274的十六进制.的数据类型PropertyItem.Idint,这意味着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);
}
Run Code Online (Sandbox Code Playgroud)

  • 更完整的答案:对方向案例的完整描述,以及删除EXIF数据的注释(如果以后处理的话) (12认同)
  • 很有帮助.这应该是接受的答案恕我直言. (6认同)
  • 在这里可以找到一些用于测试的定向图像示例:https://github.com/recurser/exif-orientation-examples (2认同)

spe*_*der 17

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

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

  • "EPIC FAIL".你的权利.`0x0112`奏效了.谢谢! (2认同)

Bal*_*a R 14

这篇文章看起来你需要检查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;
      }
}
Run Code Online (Sandbox Code Playgroud)


小智 11

我结合了给定的答案和评论,并提出了这个问题:

    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;
            } 
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • 0x112 = 274,它们相等,因此您可以在if语句中忽略两者之一。 (2认同)
  • 我试过这个,但仍然面临移动浏览器的问题.在Web浏览器中它工作正常.但在移动设备中,图像向右转90度.救命? (2认同)