如何检测图像是否只有错误的EXIF或图像应该旋转?

时间:2019-05-24 08:06:55

标签: c# sharepoint

我们有一些SharePoint环境,我检测到成千上万张具有错误EXIF或错误EXIF +方向的图像。

例如:
a)有许多图片具有正确的横向取向,但EXIF为“ 6”
b)也有图片,其图片化为纵向,但实际上具有横向和EXIF“ 6 “

我需要能够检测出是否只需要更改EXIF或同时需要更改EXIF和方向。

我有以下代码,但是如何检测图像是否仅需要更改EXIF或也需要旋转?

public static void SetImageProperties(System.Drawing.Image image, ImageFormat imageFormat, SPFile spFile, Enumerators.SPImageOrientations imageOrientation, bool rotate, RotateFlipType rotation)
{
    string loggingMessage = null;
    SPWeb spWeb = spFile.Item.ParentList.ParentWeb;
    string siteUrl = spWeb.Site.MakeFullUrl(spWeb.ServerRelativeUrl);
    List<PropertyItem> propertyItems = image.PropertyItems.OrderBy<PropertyItem, int>(pi => pi.Id).ToList<PropertyItem>();
    PropertyItem propertyItem = propertyItems.Where<PropertyItem>(property => property.Id == 274).FirstOrDefault<PropertyItem>();
    Int16 imageOrientationInt = Convert.ToInt16(imageOrientation);
    byte imageOrientationNumber = (byte)imageOrientationInt;
    if (propertyItem != null)
    {
        propertyItem.Value[0] = imageOrientationNumber;
        image.SetPropertyItem(propertyItem);
        Stream imageStream = DrawingMethods.ImageToStream(image, imageFormat);
        spFile.SaveBinary(imageStream);
        spWeb.Dispose();
        if (rotate == true)
        {
            SPSite spSite = new SPSite(siteUrl);
            spWeb = spSite.OpenWeb();
            SPList spList = spWeb.Lists[spFile.Item.ParentList.ID];
            SPListItem spListItem = spList.GetItemById(spFile.Item.ID);
            spFile = spListItem.File;
            image = DrawingMethods.ByteArrayToImage(spFile.OpenBinary());
            image.RotateFlip(rotation);
            imageFormat = MimeTypeHelper.GetImageFormat(spFile.OpenBinary());
            imageStream = DrawingMethods.ImageToStream(image, imageFormat);
            spFile.SaveBinary(imageStream);
            spWeb.Dispose();
            spSite.Dispose();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

除了读取EXIF数据并检查Orientation值外,没有其他简便的方法可以检测图像的方向。

在您的特定情况下,即使您具有有关图像(例如,它们是纵向/横向图像)的某些信息,也无法提出简单的解决方案。如果在其EXIF数据中未定义方向值,则需要某种图像识别和分析库 (例如OpenCV或类似的文件) < strong>猜测图像的方向:图像只是图像,方向更像是“人类关注的事物” 。

但是,如果图像包含方向EXIF数据,则可以轻松读取它们并根据需要旋转图像。

如何获取Orientation EXIF数据:

# Probability
A higher chance of winning than B

# Case1: A wins
A +small reward
B -small punishment

# Case2: B wins
A -large punishment
B +large reward

如何使用Orientation EXIF数据旋转图像:

// EXIF data: orientation address.
public const int ImageOrientationId = 0x0112;

// here we use an enum for readability purposes
public enum ExifOrientations
{
    Unknown = 0,
    TopLeft = 1,
    TopRight = 2,
    BottomRight = 3,
    BottomLeft = 4,
    LeftTop = 5,
    RightTop = 6,
    RightBottom = 7,
    LeftBottom = 8
}

public static ExifOrientations ImageExifOrientation(Image img)
{
    // check if there's a value for ImageOrientationId address
    int orientation_index =
    Array.IndexOf(img.PropertyIdList, ImageOrientationId);

    // that means there's no orientation value
    if (orientation_index < 0) return ExifOrientations.Unknown;

    // otherwise, we cast it in our enum and return it
    return (ExifOrientations)
    img.GetPropertyItem(ImageOrientationId).Value[0];
}