checks-parser/adjustpicturedialog.cpp

106 lines
3.1 KiB
C++
Raw Normal View History

2024-11-22 23:26:42 +03:00
#include "adjustpicturedialog.h"
#include "ui_adjustpicturedialog.h"
#include "utils/utils.h"
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <string>
#include <opencv2/core/mat.hpp>
2024-11-25 04:07:56 +03:00
#include <QColorTransform>
2024-11-22 23:26:42 +03:00
#include <QFileDialog>
#include <QMessageBox>
#include <zbar.h>
AdjustPictureDialog::AdjustPictureDialog(QWidget *parent, std::string imagePath)
: QDialog(parent)
2024-11-25 04:07:56 +03:00
, ui(new Ui::AdjustPictureDialog)
, pixmap(QString::fromStdString(imagePath))
, img(pixmap.toImage()){
2024-11-22 23:26:42 +03:00
ui->setupUi(this);
2024-11-25 04:07:56 +03:00
computeContrastLookupTable();
2024-11-22 23:26:42 +03:00
scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
scene->addPixmap(pixmap);
}
AdjustPictureDialog::~AdjustPictureDialog() {
delete ui;
}
void AdjustPictureDialog::accept() {
QPixmap pixMap = ui->graphicsView->grab();
pixMap.save(QString::fromStdString(get_path_relative_to_home(".local/share/checks_parser/temp.png")));
std::string result = decode();
if (result == "") {
QMessageBox infoDialog;
infoDialog.setText("QR code was not detected on that image. Please edit it again or enter data manually");
infoDialog.setIcon(QMessageBox::Warning);
infoDialog.setWindowTitle("No QR code");
infoDialog.exec();
} else {
emit decodedData(result);
QDialog::accept();
}
}
std::string AdjustPictureDialog::decode() {
cv::Mat im = cv::imread(get_path_relative_to_home(".local/share/checks_parser/temp.png"));
zbar::ImageScanner scanner;
scanner.set_config(zbar::ZBAR_QRCODE, zbar::ZBAR_CFG_ENABLE, 1);
scanner.set_config(zbar::ZBAR_QRCODE, zbar::ZBAR_CFG_TEST_INVERTED, 1);
cv::Mat imGray;
cv::cvtColor(im, imGray, cv::COLOR_BGR2GRAY);
zbar::Image image(im.cols, im.rows, "Y800", (uchar *) imGray.data, im.cols * im.rows);
2024-11-24 19:07:28 +03:00
scanner.scan(image);
2024-11-22 23:26:42 +03:00
std::string result = "";
for (zbar::Image::SymbolIterator symbol = image.symbol_begin(); symbol != image.symbol_end(); ++symbol) {
result = symbol->get_data();
}
2024-11-25 04:07:56 +03:00
return result;
}
2024-11-22 23:26:42 +03:00
2024-11-25 04:07:56 +03:00
void AdjustPictureDialog::computeContrastLookupTable() {
2024-11-22 23:26:42 +03:00
2024-11-25 04:07:56 +03:00
for (int contrastValue = 0; contrastValue < 100; ++contrastValue) {
double contrast = contrastValue / 50.0;
for (int i = 0; i < 256; ++i) {
unsigned short correctedValue = std::clamp(static_cast<int>(128 + contrast * (i - 128)), 0, 255);
contrastLUT[contrastValue].push_back(correctedValue);
}
}
2024-11-22 23:26:42 +03:00
}
2024-11-24 19:07:28 +03:00
void AdjustPictureDialog::on_contrastSlider_sliderMoved(int position) {
2024-11-25 04:07:56 +03:00
QImage image = img.copy();
uint32_t* pixels = reinterpret_cast<uint32_t*>(image.bits());
int width = image.width();
int height = image.height();
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
QRgb rgb = pixels[y * width + x];
pixels[y * width + x] = qRgba(
contrastLUT[position][qRed(rgb)],
contrastLUT[position][qGreen(rgb)],
contrastLUT[position][qBlue(rgb)],
qAlpha(rgb));
}
}
2024-11-24 19:07:28 +03:00
2024-11-25 04:07:56 +03:00
scene->clear();
scene->addPixmap(QPixmap::fromImage(image));
2024-11-24 19:07:28 +03:00
}