在我的项目中,我使用QCameraImageCapture::CaptureToBuffer
. 但它也会将图像保存到文件中。由于我计划连续捕获图像,因此我想阻止它将图像保存到文件中。这是我的代码:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
cam = new QCamera;
cam->setCaptureMode(QCamera::CaptureStillImage);
viewfinder = new QCameraViewfinder;
viewfinder->show();
QCameraImageCapture *cap = new QCameraImageCapture(cam);
cap->setCaptureDestination(QCameraImageCapture::CaptureToBuffer);
cam->setViewfinder(viewfinder);
QObject::connect(cap, &QCameraImageCapture::imageCaptured, [=] (int id, QImage img) {
QByteArray buf;
QBuffer buffer(&buf);
buffer.open(QIODevice::WriteOnly);
img.save(&buffer, "BMP");
});
QObject::connect(cap, &QCameraImageCapture::readyForCaptureChanged, [=] (bool state) {
if(state == true) {
cam->searchAndLock();
cap->capture();
cam->unlock();
}
});
cam->start();
}