Upload system with rotate image automatically as in windows 8.1 or facebook

Asked

Viewed 485 times

3

Today when viewing an image in windows 8.1 even if the image is lying down it automatically understands and shows correctly, in windows 7 appears the picture lying down. When sending this image to websites like facebook it appears correct. But when sending to a normal upload it appears lying down.

I thought the system read the EXIF photo but the image was made by a camera that apparently had no system to know whether it was lying or standing.

How to make a class/component that understands this photo and automatically turn it?

1 answer

2


Using the class EXIFExtractor implemented and explained here, thus:

var bmp = new Bitmap(pathToImageFile);
var exif = new EXIFextractor(ref bmp, "n");

if (exif["Orientation"] != null)
{

    RotateFlipType flip = OrientationToFlipType(exif["Orientation"].ToString());

    if (flip != RotateFlipType.RotateNoneFlipNone) // Se a orientação já está correta
    {
        bmp.RotateFlip(flip);
        exif.setTag(0x112, "1");
        bmp.Save(pathToImageFile, ImageFormat.Jpeg);
    }
}


private static RotateFlipType OrientationToFlipType(string orientation)
{

    switch (int.Parse(orientation))
    {
        case 1:    
            return RotateFlipType.RotateNoneFlipNone;
            break;
        case 2:    
            return RotateFlipType.RotateNoneFlipX;
            break;
        case 3:    
            return RotateFlipType.Rotate180FlipNone;
            break;
        case 4:    
            return RotateFlipType.Rotate180FlipX;
            break;
        case 5:    
            return RotateFlipType.Rotate90FlipX;
            break;
        case 6:    
            return RotateFlipType.Rotate90FlipNone;
            break;
        case 7:    
            return RotateFlipType.Rotate270FlipX;
            break;
        case 8:    
            return RotateFlipType.Rotate270FlipNone;
            break;        
        default:    
            return RotateFlipType.RotateNoneFlipNone;    
    }

}

I took the example from here.

Without EXIF I think there’s no way.

  • Good answer. : ) Indeed has as do without EXIF, but it’s not very trivial. An option that is certainly simpler and maybe works well enough (I’m not sure, the AP needs to test) should be to use Opencv to estimate the pose (the example is in Python, but should work in the size of C#) of some object in the image, and then decide the orientation from it.

  • 1

    @Luizvieira I once tried to use Opencv for image recognition. It was so complicated that I gave up.

  • 1

    Hahahaha I totally understand you. But don’t worry, it happens to anyone who uses Opencv. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.