使用Qcameraimagecapture :: capture()以png格式保存捕获的图像

发布于 2025-01-23 17:16:55 字数 908 浏览 0 评论 0原文

这实际上是我在论坛中的第一个问题。 所以我是QT新手,我陷入了这个小细节。 我正在创建该应用程序来拍摄图片并保存它们,但是问题是它以“ JPEG”格式保存,我需要在“ PNG”或“ GIF”或“ TIFF”中保存它们,并且我已经尝试过很多东西,但没有任何效果,所以这是我的代码:

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
_camera_view = new QCameraViewfinder();
_take_image_button = new QPushButton("Take Image");
_turn_camera_off = new QPushButton("Turn Off");
_turn_camera_on= new QPushButton("Turn On");

_central_widget = new QWidget();
setCentralWidget(_central_widget);

_setup_ui();
_setup_camera_devices();
set_camera(QCameraInfo::defaultCamera());
connect(_take_image_button, &QPushButton::clicked, [this]{
      _image_capture.data()->capture();});

connect(_turn_camera_off, &QPushButton::clicked, [this]{_camera.data()->stop();});
connect(_turn_camera_on, &QPushButton::clicked, [this]{_camera.data()->start();});}

this is literally my first question in a forum.
So I'm a Qt newbie and I'm stuck at this little detail.
I'm creating this application that takes pictures and saves them, but the issue is that it saves then in a "JPEG" format and i need them in "PNG" or "GIF" or "tiff" and i I've tried a lot of stuff but nothing worked, so here's my code :

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
_camera_view = new QCameraViewfinder();
_take_image_button = new QPushButton("Take Image");
_turn_camera_off = new QPushButton("Turn Off");
_turn_camera_on= new QPushButton("Turn On");

_central_widget = new QWidget();
setCentralWidget(_central_widget);

_setup_ui();
_setup_camera_devices();
set_camera(QCameraInfo::defaultCamera());
connect(_take_image_button, &QPushButton::clicked, [this]{
      _image_capture.data()->capture();});

connect(_turn_camera_off, &QPushButton::clicked, [this]{_camera.data()->stop();});
connect(_turn_camera_on, &QPushButton::clicked, [this]{_camera.data()->start();});}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丘比特射中我 2025-01-30 17:16:56

您应该在工作的某个点上获得qimage。它具有 save 成员。引用文档的示例:

QImage image;
QByteArray ba;
QBuffer buffer(&ba);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "PNG"); // writes image into ba in PNG format

因此,qcameraimagecapture的用法是这样的:

QObject::connect(cap, &QCameraImageCapture::imageCaptured, [=] (int id, QImage img) {
    QByteArray ba;
    QBuffer buffer(&ba);
    buffer.open(QIODevice::WriteOnly);
    img.save(&buffer, "PNG");
});

You should get QImage in some point of your work. It has save member. Example from cited documentation:

QImage image;
QByteArray ba;
QBuffer buffer(&ba);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "PNG"); // writes image into ba in PNG format

So the usage in context of QCameraImageCapture goes something like that:

QObject::connect(cap, &QCameraImageCapture::imageCaptured, [=] (int id, QImage img) {
    QByteArray ba;
    QBuffer buffer(&ba);
    buffer.open(QIODevice::WriteOnly);
    img.save(&buffer, "PNG");
});
手心的海 2025-01-30 17:16:56

对于可能将来可能遇到此问题的任何人,这是我发现的解决方案:

    _image_capture->setCaptureDestination(QCameraImageCapture::CaptureToBuffer);

 QObject::connect(_image_capture.data(), &QCameraImageCapture::imageCaptured, [=] (int id, QImage img) {

           fileName = "image.png";
           path = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation) + "/" + fileName;
           img.save(path, "PNG");
             
            });

For anyone that might encounter this problem in the future, here's the solution i've found :

    _image_capture->setCaptureDestination(QCameraImageCapture::CaptureToBuffer);

 QObject::connect(_image_capture.data(), &QCameraImageCapture::imageCaptured, [=] (int id, QImage img) {

           fileName = "image.png";
           path = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation) + "/" + fileName;
           img.save(path, "PNG");
             
            });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文