1

我正在尝试使用 QT 捕获图像,并将图像缓冲区写入 QBuffer 类的对象,而不是将其保存到文件中。

到目前为止,这是我的代码:

mBuffer->open(QBuffer::ReadWrite);
mCameraImageCapture->setCaptureDestination(QCameraImageCapture::CaptureToBuffer);
QImageEncoderSettings imageEncoderSettings;
imageEncoderSettings.setCodec("image/BMP");
mCameraImageCapture->setEncodingSettings(imageEncoderSettings);
mCamera->setCaptureMode(QCamera :: CaptureStillImage);
mCamera->start();
mCamera->searchAndLock();
mCamera->unlock();
mBuffer->close();

是否可以将图像捕获到缓冲区而不将其保存到文件中?

4

1 回答 1

6

也许是这样的?

QApplication app(argc, argv);
QCamera *cam = new QCamera;

cam->setCaptureMode(QCamera::CaptureStillImage);

QCameraViewfinder *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, "PNG");
});

QObject::connect(cap, &QCameraImageCapture::readyForCaptureChanged, [=] (bool state) {
   if(state == true) {
       cam->searchAndLock();
       cap->capture();
       cam->unlock();
   }
});

cam->start();

app.exec();
于 2017-10-30T08:52:42.797 回答