QrScanner: macOS support

This commit is contained in:
tobtoht
2021-06-27 02:41:30 +02:00
parent 3db3e330d5
commit a1db9ade6b
8 changed files with 83 additions and 152 deletions
+61 -14
View File
@@ -11,7 +11,6 @@
QrCodeScanDialog::QrCodeScanDialog(QWidget *parent)
: QDialog(parent)
, ui(new Ui::QrCodeScanDialog)
, m_scanner(new QrCodeScanner(this))
{
ui->setupUi(this);
this->setWindowTitle("Scan QR Code");
@@ -31,10 +30,16 @@ QrCodeScanDialog::QrCodeScanDialog(QWidget *parent)
connect(ui->combo_camera, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &QrCodeScanDialog::onCameraSwitched);
connect(m_scanner, &QrCodeScanner::decoded, this, &QrCodeScanDialog::onDecoded);
connect(m_scanner, &QrCodeScanner::notifyError, this, &QrCodeScanDialog::notifyError);
this->onCameraSwitched(0);
m_thread = new QrScanThread(this);
m_thread->start();
connect(m_thread, &QrScanThread::decoded, this, &QrCodeScanDialog::onDecoded);
connect(m_thread, &QrScanThread::notifyError, this, &QrCodeScanDialog::notifyError);
connect(&m_imageTimer, &QTimer::timeout, this, &QrCodeScanDialog::takeImage);
m_imageTimer.start(500);
}
void QrCodeScanDialog::onCameraSwitched(int index) {
@@ -42,24 +47,60 @@ void QrCodeScanDialog::onCameraSwitched(int index) {
return;
}
m_scanner->setSource(nullptr);
delete m_camera;
m_camera.reset(new QCamera(m_cameras.at(index)));
m_camera = new QCamera(m_cameras.at(index), this);
connect(m_camera, &QCamera::statusChanged, [this](QCamera::Status status){
auto captureMode = QCamera::CaptureStillImage;
if (m_camera->isCaptureModeSupported(captureMode)) {
m_camera->setCaptureMode(captureMode);
}
connect(m_camera.data(), QOverload<QCamera::Error>::of(&QCamera::error), this, &QrCodeScanDialog::displayCameraError);
connect(m_camera.data(), &QCamera::statusChanged, [this](QCamera::Status status){
bool unloaded = (status == QCamera::Status::UnloadedStatus);
ui->frame_unavailable->setVisible(unloaded);
});
m_camera->setCaptureMode(QCamera::CaptureViewfinder);
m_imageCapture.reset(new QCameraImageCapture(m_camera.data()));
if (!m_imageCapture->isCaptureDestinationSupported(QCameraImageCapture::CaptureToBuffer)) {
qDebug() << "Capture to buffer is NOT supported";
}
m_imageCapture->setCaptureDestination(QCameraImageCapture::CaptureToBuffer);
connect(m_imageCapture.data(), &QCameraImageCapture::imageAvailable, this, &QrCodeScanDialog::processAvailableImage);
connect(m_imageCapture.data(), QOverload<int, QCameraImageCapture::Error, const QString &>::of(&QCameraImageCapture::error),
this, &QrCodeScanDialog::displayCaptureError);
m_camera->setViewfinder(ui->viewfinder);
m_scanner->setSource(m_camera);
m_scanner->setEnabled(true);
m_camera->start();
}
void QrCodeScanDialog::displayCaptureError(int id, const QCameraImageCapture::Error error, const QString &errorString)
{
Q_UNUSED(id);
Q_UNUSED(error);
QMessageBox::warning(this, "Image Capture Error", errorString);
}
void QrCodeScanDialog::displayCameraError()
{
QMessageBox::warning(this, "Camera Error", m_camera->errorString());
}
void QrCodeScanDialog::processAvailableImage(int id, const QVideoFrame &frame) {
Q_UNUSED(id);
QImage img = frame.image();
img.convertTo(QImage::Format_RGB32);
m_thread->addImage(img);
}
void QrCodeScanDialog::takeImage()
{
if (m_imageCapture->isReadyForCapture()) {
m_imageCapture->capture();
}
}
void QrCodeScanDialog::onDecoded(int type, const QString &data) {
decodedString = data;
this->accept();
@@ -71,6 +112,12 @@ void QrCodeScanDialog::notifyError(const QString &msg) {
QrCodeScanDialog::~QrCodeScanDialog()
{
delete m_camera;
m_thread->stop();
m_thread->quit();
if (!m_thread->wait(5000))
{
m_thread->terminate();
m_thread->wait();
}
delete ui;
}