How to flip image from QCamera?

697 Views Asked by At

In QT Creator how we can filp the images from camera. I googled a lot but I didn't get a proper solution. Following is my code.

mCamera = new QCamera;
    mViewfinder = new QCameraViewfinder;
    mLayout = new QVBoxLayout(ui->graphicsView);
    mLayout->addWidget(mViewfinder);
    mCamera->setViewfinder(mViewfinder);
    mViewfinder->show();
    mCamera->start();

I tried QCamera::FrontFace and QCamera::BackFace in constructor argument in QCamera like below

mCamera = new QCamera(QCamera::FrontFace);

and

mCamera = new QCamera(QCamera::BackFace );

But both have no difference. In Python

video=cv2.flip(self.frame,1)

will solve the problem, Any idea how to solve this .. I am using Windows 10

2

There are 2 best solutions below

7
On BEST ANSWER

QCamera::FrontFace and QCamera::BackFace are just positions of the camera. To achieve what you want, you should flip every image.

Create QCameraImageCapture and connect to its imageCaptured() signal.

auto imageCapture = new QCameraImageCapture( mCamera );
connect(imageCapture, &QCameraImageCapture::imageCaptured, [&](int id, const QImage &preview){
    QImage flipped = preview.mirrored();
    // do what you want with flipped image
})

Documentation says that mirrored(bool horizontal = false, bool vertical = true)

Returns a mirror of the image, mirrored in the horizontal and/or the vertical direction depending on whether horizontal and vertical are set to true or false.

UPDATE:

I found the camera and tested the code and realized that I forgot one important thing. You need to use a timer by which QCameraImageCapture will capture the image.

Create QTimer and connect to QTimer::timeout() signal:

connect (&timer, &QTimer::timeout, [&](){
    camera->searchAndLock();
    imageCapture->capture();
    camera->unlock();
});

And after that start the timer. To show flipped image you can use just QLabel class with label->setPixmap(QPixmap::fromImage(flipped)) method.

0
On

Hai I changed my code as per the answer of @Allocse and its work for me My full code will be

mCamera = new QCamera;
    mCamera->start();
    imageCapture = new QCameraImageCapture( mCamera );
    connect (&timer, &QTimer::timeout, [&](){
    mCamera->searchAndLock();
    imageCapture->capture();
    mCamera->unlock();
    });
    connect(imageCapture, &QCameraImageCapture::imageCaptured, [&](int id, const QImage &preview){
    QImage flipped = preview.mirrored(true,false);

    ui->videoFrame->setPixmap(QPixmap::fromImage(flipped));
    });
     timer.start();

Note :-mCamera and imageCapture should be declared in class decleration