Saving Image to Android Device - Qt6 for Android
Unsolved
Mobile and Embedded
-
Using Qt6 for Android, the following is a successful example.
Click the capture button, it will pop up a dialog to select the file location on Android, you just need to fill in the image name + image format suffix.
mainwindow.cpp
#include "mainwindow.h" #include <QVBoxLayout> #include <QHBoxLayout> #include <QCamera> #include <QMediaDevices> #include <QImageCapture> #include <QApplication> #include <QStandardPaths> #include <QFile> #include <QDir> #include <QDateTime> #include <QString> #include <QIODevice> #include <QLabel> #include <QFileDialog> #include <QImage> #include <QPainter> #include <QDebug> #if QT_CONFIG(permissions) #include <QPermission> #endif MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), camera(new QCamera(QMediaDevices::defaultVideoInput())), imageCapture(new QImageCapture), viewfinder(new QVideoWidget), buttonGroupBox(new QGroupBox), buttonGridlayout(new QGridLayout), captureButton(new QPushButton("Capture")), cameraSettingButton(new QPushButton("Setting")), styleSwitchButton(new QPushButton("Style")), centralWidget(new QWidget(this)), layout(new QVBoxLayout(centralWidget)) { setCentralWidget(centralWidget); buttonGridlayout->addWidget(captureButton, 0, 0); buttonGridlayout->addWidget(cameraSettingButton, 0, 1); buttonGridlayout->addWidget(styleSwitchButton, 0, 2); buttonGroupBox->setLayout(buttonGridlayout); layout->addWidget(viewfinder, 1); layout->addWidget(buttonGroupBox, 0); #if QT_CONFIG(permissions) // Camera permission QCameraPermission cameraPermission; switch (qApp->checkPermission(cameraPermission)) { case Qt::PermissionStatus::Undetermined: qApp->requestPermission(cameraPermission, this, &MainWindow::initPermissions); return; case Qt::PermissionStatus::Denied: qWarning("Camera permission is not granted!"); return; case Qt::PermissionStatus::Granted: break; } #endif init(); } void MainWindow::initPermissions() { #if QT_CONFIG(permissions) // Camera permission QCameraPermission cameraPermission; if (qApp->checkPermission(cameraPermission) != Qt::PermissionStatus::Granted) { qWarning("Camera permission is not granted!"); return; } #endif init(); } void MainWindow::init() { captureSession.setCamera(camera); captureSession.setVideoOutput(viewfinder); captureSession.setImageCapture(imageCapture); connect(captureButton, &QPushButton::clicked, this, &MainWindow::captureImage); connect(imageCapture, &QImageCapture::imageCaptured, this, &MainWindow::onImageCaptured); camera->start(); } void MainWindow::captureImage() { imageCapture->captureToFile(); } void MainWindow::onImageCaptured(int id, const QImage &image) { Q_UNUSED(id); capturedImage = image; fileName = QFileDialog::getSaveFileName(this, "Save Image", "", "Images (*.png *.jpg *.bmp)"); if (!fileName.isEmpty()) { int quality = 100; if (capturedImage.save(fileName, nullptr, quality)) { qDebug() << "Image saved successfully."; } else { qDebug() << "Failed to save image."; } } else { qDebug() << "No file selected."; } }
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QCamera> #include <QMediaDevices> #include <QImageCapture> #include <QMediaCaptureSession> #include <QVideoWidget> #include <QSlider> #include <QPushButton> #include <QVBoxLayout> #include <QGroupBox> #include <QGridLayout> class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private slots: void onImageCaptured(int id, const QImage &image); void captureImage(); private: void initPermissions(); void init(); QString filePath; QString fileName; QImage capturedImage; QCamera *camera; QImageCapture *imageCapture; QMediaCaptureSession captureSession; QVideoWidget *viewfinder; QGroupBox *buttonGroupBox; QGridLayout *buttonGridlayout; QPushButton *captureButton; QPushButton *cameraSettingButton; QPushButton *styleSwitchButton; QWidget *centralWidget; QVBoxLayout *layout; }; #endif // MAINWINDOW_H