Дмитрий
Dec. 11, 2022, 12:50 a.m.

How to open image with correct orientation with Qt tools

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.

  1. QImageReader reader("filePath");
  2. int rotor(0);
  3. switch( reader.transformation() ) {
  4. case QImageIOHandler::TransformationRotate90: rotor = 90;
  5. break;
  6. case QImageIOHandler::TransformationRotate180: rotor = 180;
  7. break;
  8. case QImageIOHandler::TransformationRotate270: rotor = 270;
  9. break;
  10. }
  11. QImage image = reader.read();
  12. if( rotor ) {
  13. QTransform tr;
  14. tr.rotate(rotor);
  15. image = image.transformed(tr);
  16. }
  17. QLabel l;
  18. l.resize( image.size() );
  19. l.setPixmap( QPixmap::fromImage(image) );
  20. 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.

By article asked0question(s)

2

Comments

Only authorized users can post comments.
Please, Log in or Sign up
  • Last comments
  • Evgenii Legotckoi
    March 9, 2025, 9:02 p.m.
    К сожалению, я этого подсказать не могу, поскольку у меня нет необходимости в обходе блокировок и т.д. Поэтому я и не задавался решением этой проблемы. Ну выглядит так, что вам действитель…
  • VP
    March 9, 2025, 4:14 p.m.
    Здравствуйте! Я устанавливал Qt6 из исходников а также Qt Creator по отдельности. Все компоненты, связанные с разработкой для Android, установлены. Кроме одного... Когда пытаюсь скомпилиров…
  • ИМ
    Nov. 22, 2024, 9:51 p.m.
    Добрый вечер Евгений! Я сделал себе авторизацию аналогичную вашей, все работает, кроме возврата к предидущей странице. Редеректит всегда на главную, хотя в логах сервера вижу запросы на правильн…
  • Evgenii Legotckoi
    Oct. 31, 2024, 11:37 p.m.
    Добрый день. Да, можно. Либо через такие же плагины, либо с постобработкой через python библиотеку Beautiful Soup
  • A
    Oct. 19, 2024, 5:19 p.m.
    Подскажите как это запустить? Я не шарю в программировании и кодинге. Скачал и установаил Qt, но куча ошибок выдается и не запустить. А очень надо fb3 переконвертировать в html