Some popular image formats (like jpg) may contain metadata. One of them is rotation. If you create the file in a graphics editor, then rotate it in the viewer, and finally load it into your application using the QImage, QPicture, or QPixmap classes, you will find that the image is loaded in its original orientation. With this orientation, the image is stored in the file. At the same time, any graphics editor opens this picture with the current rotation. The problem is easily corrected by turning to the desired angle. But the above classes do not allow it to be counted.
In an attempt to solve this problem, programmers turn to the multimedia module (in particular, the QMediaRecorder class and its metaData(const Qstring & key) method. This is a mistake. QImageReader and the transformation() method will help us solve this issue.
QImageReader reader("filePath"); int rotor(0); switch( reader.transformation() ) { case QImageIOHandler::TransformationRotate90: rotor = 90; break; case QImageIOHandler::TransformationRotate180: rotor = 180; break; case QImageIOHandler::TransformationRotate270: rotor = 270; break; } QImage image = reader.read(); if( rotor ) { QTransform tr; tr.rotate(rotor); image = image.transformed(tr); } QLabel l; l.resize( image.size() ); l.setPixmap( QPixmap::fromImage(image) ); l.show();
A detailed study of the Qt documentation helped me find this solution. You can save a file using metadata using the QImageWriter class.