Digital zoom for live stream
-
Hi.
I am live streaming (rtsp) from a camera and displaying the stream on a label with the following code:
capture >> frame; cvtColor(frame, frame, cv::COLOR_BGR2BGRA); QImage image = (const unsigned char*)(frame.data), frame.cols, frame.rows, QImage::Format_RGB888; ui->label->setPixmap(QPixmap::fromImage(image).scaled(ui->label->width(), ui->label->height(), Qt::KeepAspectRatio));
Now, I want to implement digital zoom for the stream with a button. Is there a way I can do that? I read that QCameraFocus might help with this. But QCameraFocus class is not available in Qt 6.3.0. Is there a solution for Qt 6.3.0? or using opencv maybe.
-
@BigBen said:
ui->label->setPixmap(QPixmap::fromImage(image).scaled(ui->label->width(), ui->label->height(), Qt::KeepAspectRatio));
This makes multiple copies of the data and is unnecessarily slow.
Replace the label with a plain QWidget and override its paintEvent. Use QPainter there to draw the image directly, without converting it to pixmap and then scaling. QPainter lets you draw the image with any transformation you want, so you can do zoom, pan, rotation, mirroring and more. -
@Chris-Kawa Thanks you for your answer.
How should I override the paintEvent function, and what to write in the overridden one? Do I need to make a new class? -
@BigBen
Yes, you always need to write a new class which derives from the base class if you need tooverride
avirtual
method, as QWidget::paintEvent().The Analog Clock Example illustrates overriding
paintEvent()
. There will be hundreds of other examples online.