使用 QCameraImageCapture::capture() 以 PNG 格式保存捕获的图像
Save a captured image using QCameraImageCapture::capture() in PNG Format
这实际上是我在论坛中提出的第一个问题。
所以我是一个 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();});}
你应该在工作的某些方面得到 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");
});
对于将来可能遇到此问题的任何人,这是我找到的解决方案:
_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");
});
这实际上是我在论坛中提出的第一个问题。 所以我是一个 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();});}
你应该在工作的某些方面得到 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");
});
对于将来可能遇到此问题的任何人,这是我找到的解决方案:
_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");
});