我显示视频的方法如下:
当新图像准备好时,调用DrawImage,窗口也准备好显示。 我在调用此函数之前使用bool isReady设置为false 并在mainwindow paintEvent覆盖中设置为true。 (决定窗户是否准备好了)
void Mainview::DrawImage (char *pBuffer)
{
lockBuffer();
QImage image = getImageFromBuffer(); //1920x1080 RGB_888 image
paintLabel->setImage(image); //PaintLabel pointer
unlockBuffer();
}
void Mainview::paintEvent (QPaintEvent *e)
{
Q_UNUSED(e);
m_bReady = TRUE;
}
PaintLabel实现(重要部分):
void PaintLabel::setImage(QImage &image)
{
imageData = image.scaled(this->width(), this->height()); //private QImage
update();
}
void PaintLabel::paintEvent(QPaintEvent *ev)
{
Q_UNUSED(ev);
if (!m_Image.isNull())
{
if (isScale)
{
m_Image.scaled(this->width(), this->height());
}
QRectF target(0.0, 0.0, m_Image.width(), m_Image.height());
// Alignment draw
if (this->alignment() & Qt::AlignCenter)
{
qreal posX = this->width() / 2 - m_Image.width() / 2;
qreal posY = this->height() / 2 - m_Image.height() / 2;
target.setRect(posX, posY, m_Image.width(), m_Image.height());
}
// draw the image
QPainter painter(this);
painter.drawImage(target, imageData);
painter.end();
}
}
DrawImage调用花费了近100毫秒,这太过分了。任何想法,如何解决这个问题?