c++qcamera

How to flip image from QCamera?


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


Solution

  • 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.