QImage,QPixmap加載照片圖像,切記處理拍攝方向

現在數碼相機拍攝圖片都有拍攝方向,水平、垂直拍攝,拍攝後,圖像內容會保存相應的信息,我們可以獲取這些信息,對圖像做展示,不然你會發現,一張圖片,90度拍攝後,QImage加載會發生90度旋轉,我測試如果不做處理,html5的image標籤,python的CV2模塊加載圖片,都會發現圖片方向被旋轉90度。再Qt中 可以用如下方式來修正圖像方向
代碼:

QImageReader reader(path);
reader.setAutoTransform(true);
const QImage newImage = reader.read();
QImageIOHandler::Transformations transformation;
if (newImage.isNull())
 {
      QMessageBox::information(nullptr, QGuiApplication::applicationDisplayName(),
                               tr("Cannot load %1: %2")
                              .arg(QDir::toNativeSeparators(path), reader.errorString()));
            return;
}
else {
      transformation = reader.transformation();
      qDebug() << "transformation is " << transformation;
} 
if (transformation == QImageIOHandler::TransformationRotate90
            || transformation == QImageIOHandler::TransformationMirrorAndRotate90
            || transformation == QImageIOHandler::TransformationFlipAndRotate90
            || transformation == QImageIOHandler::TransformationRotate270)
{
       QTransform matrix;
       matrix.rotate(90);
       m_pixmap = m_pixmap.transformed(matrix);
       m_image = m_image.transformed(matrix);
}
else if (transformation == QImageIOHandler::TransformationRotate180)
{
       QTransform matrix;
       matrix.rotate(180);
       m_pixmap = m_pixmap.transformed(matrix);
       m_image = m_image.transformed(matrix);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章