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);
}

你可能感兴趣的:(QT)