Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to bring live view of camera in C++ inside QT.
I have tried the following code.
In the code below, what happens is that all the cameras connected to the laptop are shown in the combo-box, and then when we select a camera from the combo-box and then click on the Start button, the live view of the selected camera appears. But black and white live view comes. But I do not want black and white live view. What is to be done is that the original colors should appear in the live view. How do I bring original colors in live view???

What I have tried:

C++
// This is my code from main.cpp file
#include <QApplication>
#include <QWidget>
#include <QComboBox>
#include <QPushButton>
#include <QCamera>
#include <QCameraInfo>
#include <QCameraViewfinder>
#include <QVBoxLayout>
#include <QMessageBox>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QWidget window;

    QComboBox cameraDropdown;
    QPushButton startButton("Start");
    QPushButton stopButton("Stop");
    QCamera *camera = nullptr;
    QCameraViewfinder *cameraViewfinder = new QCameraViewfinder();

    // Layout for widgets
    QVBoxLayout layout(&window);
    layout.addWidget(&cameraDropdown);
    layout.addWidget(&startButton);
    layout.addWidget(&stopButton);
    layout.addWidget(cameraViewfinder);

    // Populate the camera dropdown with available cameras
    QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
    foreach (const QCameraInfo &cameraInfo, cameras) {
        cameraDropdown.addItem(cameraInfo.description());
    }

    QObject::connect(&startButton, &QPushButton::clicked, 
           [&]() {
        // Get the selected camera
        int selectedCameraIndex = cameraDropdown.currentIndex();
        if (selectedCameraIndex >= 0) {
            QCameraInfo selectedCameraInfo = cameras[selectedCameraIndex];
            if (camera) {
                // Stop the current camera if it's running
                camera->stop();
                delete camera;
            }
            camera = new QCamera(selectedCameraInfo);

            // Set the viewfinder to the camera viewfinder widget
            camera->setViewfinder(cameraViewfinder);

            // Connect camera stateChanged signal
            QObject::connect(camera, &QCamera::stateChanged, 
                     [&](QCamera::State state) {
                if (state == QCamera::ActiveState) {
                    // Camera is successfully started
                } else if (state == QCamera::UnloadedState || 
                  state == QCamera::LoadedState) {
                    // Camera has stopped
                } else {
                    // Handle other camera states (e.g., ErrorState)
                    QMessageBox::critical(&window, 
                    "Camera Error", "Failed to start the camera.");
                }
            });

            // Start the camera
            camera->start();
        }
    });

    QObject::connect(&stopButton, &QPushButton::clicked, [&]() {
        if (camera) {
            // Stop the camera
            camera->stop();
            delete camera;
            camera = nullptr;
        }
    });

    window.show();

    return app.exec();
}
Posted
Updated 2-Oct-23 5:07am
v3
Comments
jeron1 26-Sep-23 16:25pm    
Can any other application display this in color?
Member 15746979 26-Sep-23 23:04pm    
yes, A laptop's default camera can display in color.

1 solution

Sounds like you must configure the camera settings or output correct. Take a look at this camera example code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900