How to read image orientation in Qt (Using stored EXIF)

Posted on Posted in Qt Tutorials, Tutorials

Updated: QImageReader class in Qt has a new method (introduced in Qt 5.5) named transformation() which you can use for this purpose but this article still applies if you are not using 5.5 or having problems with QImageRaader. Also it is a useful example on how to use GDI in Qt.

You might have also noticed that Qt reads jpg, tif and other types of images that it supports without paying any attention to EXIF information stored in them. EXIF meta data contains highly valuable information about the characteristics of a photograph. Let’s say you want to read an image file into QImage and do some process over it. It is crucial to know what type of orientation the image needs to go through right after reading it.





Windows has its own way of doing this using a library known as GDI (The Microsoft Windows graphics device interface). So if you are using Qt on Windows you can use GDI to handle image orientation. In this post I’m sharing a simple class that allows reading the orientation from EXIF meta-data stored in the image. You can then use simple methods already existing in QImage (such as mirrored and transformed) to transform image to whatever is needed according to the orientation type.

Following should be noted while using this class:

You MUST add following line to your Qt project file (.PRO) in order to be able to use this class:

LIBS += -lGdiplus

GDI is available in all Windows versions but if you face any problems you can start a conversation in the comments section.

Pay attention to the method definition and comments regarding the return values:

// 1 – The 0th row is at the top of the visual image, and the 0th column is the visual left side. (Do Nothing)
// 2 – The 0th row is at the visual top of the image, and the 0th column is the visual right side. (Hor flip)
// 3 – The 0th row is at the visual bottom of the image, and the 0th column is the visual right side. (+180)
// 4 – The 0th row is at the visual bottom of the image, and the 0th column is the visual left side. (ver flip)
// 5 – The 0th row is the visual left side of the image, and the 0th column is the visual top. (-90 and flip ver)
// 6 – The 0th row is the visual right side of the image, and the 0th column is the visual top. (+90)
// 7 – The 0th row is the visual right side of the image, and the 0th column is the visual bottom. (+90 – flip ver)
// 8 – The 0th row is the visual left side of the image, and the 0th column is the visual bottom. (-90)
SHORT QImgOrient::orientation(QString fname)

You can download this class using the button below:

Download





Leave a Reply

Your email address will not be published. Required fields are marked *