2147 lines
71 KiB
C++
Raw Permalink Normal View History

2025-07-17 08:39:15 +08:00
#include <Python.h>
#include "bacteria.h"
#include "ui_bacteria.h"
#include<QFileDialog>
#include "IMVDefines.h"
#include<QMessageBox>
#include <QApplication>
#include <QWidget>
#include <QPainter>
#include <QImage>
#include <QFile>
#include <QTextDocument>
#include <QAxObject>
#include <QAxWidget>
#include<QGraphicsDropShadowEffect>
#include<QScrollBar>
#include <QtConcurrent/QtConcurrent>
#include <QCoreApplication>
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <unistd.h>
#include <psapi.h>
#include <QFileInfo>
#include <windows.h>
#include <tlhelp32.h>
bool save=true;//菌落计数手动矫正的判断
int count;
QString id2;
QString savelujing;
QString savePath3;//系统命名--前
QString savePath4;//系统命名--处理后
QString xtshoudong;//系统命名--处理后
QString savePath;//自命名--前
QString savePath2;//自命名--处理后
QString zmshoudong;//自命名--处理后
QSqlDatabase db;
// extern int shang;
// extern int xia;
// extern int banjing;
bacteria::bacteria(QWidget *parent) :
QWidget(parent),
ui(new Ui::bacteria)
{
ui->setupUi(this);
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(updateCount()));
connect(&m_cammer,SIGNAL(xinhao(QString)),this, SLOT(getxinhao(QString)));
list_cameras = QMediaDevices::videoInputs();
//判断设备数(设置最多两个设备,没有则不会读取设备信息)
ui->tableWidget_3->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->tableWidget_3->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_bachistoricalrecords=new bachistoricalrecords;
connect( m_bachistoricalrecords, &bachistoricalrecords::fanhui, this, [=](){this->showMaximized();m_bachistoricalrecords->hide();});
ui->pushButton->setFixedSize(100,30);
ui->pushButton_2->setFixedSize(120,35);
ui->pushButton_13->setFixedSize(120,35);
ui->pushButton_14->setFixedSize(120,35);
ui->pushButton->setFixedSize(92,37);
ui->pushButton_6->setFixedSize(92,37);
ui->pushButton_4->setFixedSize(92,37);
ui->pushButton_5->setFixedSize(92,37);
ui->pushButton_4->setHidden(true);
ui->label_5->setHidden(true);
//ui->widget_3->setHidden(true);
ui->pushButton->setEnabled(false);
//ui->label_4->setAlignment(Qt::AlignTop | Qt::AlignLeft);
connect(this,&bacteria::shuaxin,m_bachistoricalrecords,&bachistoricalrecords::getshuaxin);
connect(m_bachistoricalrecords, SIGNAL(guan()), this, SLOT(getguan()));
connect(m_bachistoricalrecords, &bachistoricalrecords::tuichu2, this, [=](){emit tuichu();});
ui->tableWidget_3->setColumnCount(3);
// 设置第一列宽度示例为100像素
ui->tableWidget_3->setColumnWidth(1, 0); // 将第二列列索引为1的宽度设置为0实现隐藏效果
ui->tableWidget_3->setColumnWidth(2, 0);
ui->tableWidget_3->horizontalHeader()->setVisible(false);
ui->tableWidget_3->verticalHeader()->setVisible(false);
connect(ui->tableWidget_3, &QTableWidget::itemClicked, this, &bacteria::on_tableWidget_3_itemClicked);
ui->horizontalSlider->setMaximum(255);
ui->horizontalSlider->setValue(76);
ui->horizontalSlider_2->setValue(0);
ui->horizontalSlider_2->setMaximum(255);
ui->horizontalSlider_5->setMaximum(255);
ui->horizontalSlider_6->setMaximum(255);
ui->horizontalSlider_2->setFixedWidth(150);
ui->horizontalSlider->setFixedWidth(150);
ui->horizontalSlider_5->setFixedWidth(150);
ui->horizontalSlider_6->setFixedWidth(150);
ui->horizontalSlider_7->setFixedWidth(150);
ui->frame->setFixedSize(1550,907);
ui->frame_2->setFixedSize(180,907);
ui->widget_2->setFixedHeight(40);
ui->widget_3->setFixedSize(750,750);
ui->label_5->setFixedSize(750,750);
ui->label_4->setFixedSize(750,750);
ui->frame_3->setFixedSize(750,750);
ui->tableWidget_3->setFixedSize(170,886);
ui->tableWidget_3->setColumnWidth(0, 170);
QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
//设置阴影距离
shadow->setOffset(0, 0);
//设置阴影颜色
shadow->setColor(QColor(214, 214, 214));
//设置阴影圆角
shadow->setBlurRadius(30);
//给嵌套QWidget设置阴影
ui->frame_2->setGraphicsEffect(shadow);
QGraphicsDropShadowEffect *shadow2 = new QGraphicsDropShadowEffect(this);
//设置阴影距离
shadow2->setOffset(0, 0);
//设置阴影颜色
shadow2->setColor(QColor(214, 214, 214));
//设置阴影圆角
shadow2->setBlurRadius(30);
//给嵌套QWidget设置阴影
ui->frame->setGraphicsEffect(shadow2);
ui->horizontalSlider->setEnabled(false);
ui->horizontalSlider_2->setEnabled(false);
ui->horizontalSlider_5->setEnabled(false);
ui->horizontalSlider_6->setEnabled(false);
ui->horizontalSlider_7->setEnabled(false);
ui->horizontalSlider->setHidden(true);
ui->horizontalSlider_2->setHidden(true);
ui->horizontalSlider_5->setHidden(true);
ui->horizontalSlider_6->setHidden(true);
ui->horizontalSlider_7->setHidden(true);
ui->label_7->setHidden(true);
ui->label_8->setHidden(true);
ui->label_14->setHidden(true);
ui->label_16->setHidden(true);
ui->label_18->setHidden(true);
ui->label_5->setHidden(false);
ui->label_4->setHidden(true);
// QString filePath="C:/Users/20486/Desktop/danna/use/xijun/2025-03-04/10-21-00_Y.png";
// QPixmap pixmap(filePath);
// if (!pixmap.isNull()) {
// ui->label_4->setPixmap(pixmap);
// ui->label_4->setScaledContents(true); // 设置图片自动缩放以适应QLabel大小
// }else{
// qDebug()<<"zhaopianweikong11111111111111111111111";
// }
ui->label_3->setHidden(true);
ui->pushButton_3->setHidden(true);
ui->pushButton_5->setHidden(true);
m_msgBox=new msgBox;
m_zidingyi=new zidingyi;
connect(m_zidingyi, SIGNAL(ming(QString)),this, SLOT(getming(QString)));
connect(m_zidingyi,&zidingyi::quxiao,this,[=](){jixu=false;});
ui->tableWidget_3->setRowCount(100);
int rowCount = ui->tableWidget_3->rowCount();
for (int i = 0; i < rowCount; ++i) {
ui->tableWidget_3->setRowHeight(i, 170);
}
connect(this,&bacteria::show,this,&bacteria::showtu);
ui->pushButton_10->setHidden(true);
ui->pushButton_9->setHidden(true);
ui->pushButton_11->setHidden(true);
ui->pushButton_12->setHidden(true);
installEventFilter(this);
ui->pushButton_14->setHidden(true);
}
bacteria::~bacteria()
{
delete ui;
}
void bacteria::getming(QString s)
{
jixu=true;
ui->label_3->setText(s);
m_msgBox->show();
}
void bacteria::on_tableWidget_3_itemClicked(QTableWidgetItem *item)
{
if(save==false)
{
QMessageBox::information(NULL,"保存","请先保存照片", QMessageBox::Ok | QMessageBox::Cancel);
}else{
// 获取点击的单元格所在的行索引
int row = item->row();
// 获取该行第一列对应的QTableWidgetItem假设第一列存放图片路径相关信息
QTableWidgetItem *pixmapItem = ui->tableWidget_3->item(row, 1);
QTableWidgetItem *Item2 = ui->tableWidget_3->item(row, 2);
ui->label_2->setText(Item2->text());
qDebug()<<Item2->text();
qDebug()<<pixmapItem->text();
if (pixmapItem) {
// 获取之前设置的图片路径
QString filePath = pixmapItem->text();
qDebug()<<filePath;
QPixmap pixmap(filePath);
if (!pixmap.isNull()) {
ui->label_4->setHidden(false);
ui->label_5->setHidden(true);;
ui->label_4->setPixmap(pixmap);
ui->label_4->setScaledContents(true); // 设置图片自动缩放以适应QLabel大小
}
}
}
}
void bacteria::mousePressEvent(QMouseEvent *event) {
qDebug() << "点击了" ;
if (ui->label_4->geometry().adjusted(-50, -50, 50, 50).contains(event->pos())) {
if (event->button() == Qt::LeftButton) {
count++;
QPixmap pixmap = ui->label_4->pixmap();
int pixmapWidth = pixmap.width();
int pixmapHeight = pixmap.height();
int labelWidth = ui->label_4->width();
int labelHeight = ui->label_4->height();
double scaleX = (double)labelWidth / pixmapWidth;
double scaleY = (double)labelHeight / pixmapHeight;
QPoint posInLabel = event->pos() - ui->label_4->pos();
int adjustedX = (int)(posInLabel.x() / scaleX);
int adjustedY = (int)(posInLabel.y() / scaleY);
QPoint adjustedPos(adjustedX-60, adjustedY-60);
qDebug() << adjustedPos;
drawOnLabelPixmap(ui->label_4, adjustedPos);
ui->label_2->setText(QString::number(count));
leftClickPoints.append(event->pos());
} else if (event->button() == Qt::RightButton) {
count--;
QPixmap pixmap = ui->label_4->pixmap();
int pixmapWidth = pixmap.width();
int pixmapHeight = pixmap.height();
int labelWidth = ui->label_4->width();
int labelHeight = ui->label_4->height();
double scaleX = (double)labelWidth / pixmapWidth;
double scaleY = (double)labelHeight / pixmapHeight;
QPoint posInLabel = event->pos() - ui->label_4->pos();
int adjustedX = (int)(posInLabel.x() / scaleX);
int adjustedY = (int)(posInLabel.y() / scaleY);
QPoint adjustedPos(adjustedX-60, adjustedY-60);
drawOnLabelPixmap2(ui->label_4, adjustedPos);
ui->label_2->setText(QString::number(count));
rightClickPoints.append(event->pos());
}
}
}
void bacteria::drawOnLabelPixmap(QLabel* label, QPoint adjustedPos)
{
QPixmap pixmap = label->pixmap();
if (!pixmap.isNull()) {
QImage image = pixmap.toImage();
QPainter painter(&image);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::NoPen);
painter.setBrush(Qt::blue);
int yuan=radius;
qDebug() << "yuan" << yuan;
painter.drawEllipse(adjustedPos.x(), adjustedPos.y(), 50,50);
painter.end();
label->setPixmap(QPixmap::fromImage(image));
} else {
// 如果QLabel没有设置Pixmap进行相应处理
qDebug() << "QLabel does not have a Pixmap set.";
}
}
void bacteria::drawOnLabelPixmap2(QLabel* label, QPoint adjustedPos)
{
QPixmap pixmap = label->pixmap();
if (!pixmap.isNull()) {
QImage image = pixmap.toImage();
QPainter painter(&image);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::NoPen);
painter.setBrush(Qt::white);
int yuan=radius;
qDebug() << "yuan" << yuan;
painter.drawEllipse(adjustedPos.x(), adjustedPos.y(), 50,50);
painter.end();
label->setPixmap(QPixmap::fromImage(image));
} else {
// 如果QLabel没有设置Pixmap进行相应处理
qDebug() << "QLabel does not have a Pixmap set.";
}
}
void bacteria::wheelEvent(QWheelEvent *event) {
if (event->angleDelta().y() > 0) {
if (radius < 20) {
radius++;
}
} else {
if (radius > 1) {
radius--;
}
}
qDebug() << "当前值为:" << radius;
update();
}
void bacteria::getxinhao(QString p)
{
qDebug()<<"getxinhao";
// a=1;
// QString aFile=QString("use/1.png");
// QPixmap p;
// p.load(aFile);
ui->label_5->setPixmap(p);
ui->label_5->setScaledContents(true);
// usepython();
}//线程函数,定义为普通全局函数
DWORD WINAPI MyThreadFunction(LPVOID lpParam) {
// 设置Python环境
Py_SetPythonHome(L"C:/ProgramData/Anaconda3");
Py_Initialize();
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.argv = ['python.py']");
PyRun_SimpleString("sys.path.append('./')");
// 导入模块
PyObject* pModule = PyImport_ImportModule("cell_count");
if (!pModule) {
PyErr_Print();
qDebug() << "Can't open python file!\n";
// return; // 如果模块加载失败,提前返回
} else {
qDebug() << "Python file has been opened";
}
// 获取函数
PyObject* pFunhello = PyObject_GetAttrString(pModule, "main");
if (!pFunhello || !PyCallable_Check(pFunhello)) {
PyErr_Print();
qDebug() << "Get function 'main' failed or is not callable";
Py_XDECREF(pFunhello);
Py_DECREF(pModule);
//return; // 确保在失败情况下提前返回
}
// 传参
PyObject* args = PyTuple_New(2);
if (!args) {
PyErr_Print();
qDebug() << "Failed to create argument tuple";
Py_DECREF(pFunhello);
Py_DECREF(pModule);
// return;
}
PyObject* arg1 = PyUnicode_FromString(savelujing.toUtf8().constData());
PyObject* arg2 = PyUnicode_FromString(savePath4.toUtf8().constData());
PyTuple_SetItem(args, 0, arg1); // arg1
PyTuple_SetItem(args, 1, arg2); // arg2 - 这里仍需注意,将直接把 arg1 和 arg2 的引用设置到args中
// 调用函数
PyObject* result = PyObject_CallObject(pFunhello, args);
Py_DECREF(args); // 因为在PyTuple_SetItem中会管理内存调用完要释放
if (!result) {
PyErr_Print();
qDebug() << "Call function failed";
} else {
// 处理返回值
if (PyLong_Check(result)) {
long returned_value = PyLong_AsLong(result);
qDebug() << "Function returned an integer value: " << returned_value;
count = returned_value;
// emit show();
} else {
qDebug() << "Function did not return an integer as expected";
}
Py_DECREF(result); // 释放返回值
}
Py_DECREF(pFunhello); // 释放函数引用
Py_DECREF(pModule); // 释放模块引用
return 0;
}
void bacteria::usepython() {
//设置Python环境
Py_SetPythonHome(L"C:/ProgramData/Anaconda3");
Py_Initialize();
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.argv = ['python.py']");
PyRun_SimpleString("sys.path.append('./')");
// 导入模块
PyObject* pModule = PyImport_ImportModule("cell_count");
if (!pModule) {
PyErr_Print();
qDebug() << "Can't open python file!\n";
QMessageBox::information(NULL, "提示", "图片识别有误,请重试");
m_msgBox->close();
return; // 提前返回避免继续执行
} else {
qDebug() << "Python file has been opened";
}
// 获取函数
PyObject* pFunhello = PyObject_GetAttrString(pModule, "main");
if (!pFunhello || !PyCallable_Check(pFunhello)) {
PyErr_Print();
qDebug() << "Get function 'main' failed or is not callable";
Py_XDECREF(pFunhello);
Py_DECREF(pModule);
QMessageBox::information(NULL, "提示", "图片识别有误,请重试");
m_msgBox->close();
return; // 提前返回避免继续执行
}
// 传参
PyObject* args = PyTuple_New(2);
if (!args) {
PyErr_Print();
qDebug() << "Failed to create argument tuple";
Py_DECREF(pFunhello);
Py_DECREF(pModule);
QMessageBox::information(NULL, "提示", "图片识别有误,请重试");
m_msgBox->close();
return; // 提前返回避免继续执行
}
QString pythontu;
if(!eventFilterEnabled){
//如果进入审查模式
pythontu= ui->label_3->text().isEmpty() ?xtshoudong:zmshoudong;
}else{
pythontu= ui->label_3->text().isEmpty() ?savePath4:savePath2;
}
PyObject* arg1 = PyUnicode_FromString(savelujing.toUtf8().constData());
PyObject* arg2 = PyUnicode_FromString(pythontu.toUtf8().constData());
QString param;
if (ui->label_3->text().isEmpty()) {
param = savePath4;
} else {
param = savePath2;
}
// 打印传入的参数
qDebug() << "传入 PyUnicode_FromString 的参数是: " << param;
PyTuple_SetItem(args, 0, arg1); // arg1
PyTuple_SetItem(args, 1, arg2); // arg2 - 这里仍需注意,将直接把 arg1 和 arg2 的引用设置到args中
// 调用函数
PyObject* result = PyObject_CallObject(pFunhello, args);
Py_DECREF(args); // 因为在PyTuple_SetItem中会管理内存调用完要释放
if (!result) {
PyErr_Print();
qDebug() << "Call function failed";
QMessageBox::information(NULL, "提示", "图片识别有误,请重试");
m_msgBox->close();
return; // 提前返回避免继续执行
} else {
// 处理返回值
if (PyLong_Check(result)) {
long returned_value = PyLong_AsLong(result);
qDebug() << "返回数字 " << returned_value;
int value=returned_value*1.15;
count = value;
qDebug() << "显示数字 " << value;
ui->label_2->setText(QString::number(value));
emit show();
} else {
qDebug() << "Function did not return an integer as expected";
}
Py_DECREF(result); // 释放返回值
}
Py_DECREF(pFunhello); // 释放函数引用
Py_DECREF(pModule); // 释放模块引用
}
void bacteria::usepython_slow(QString yuan,QString jie) {
//设置Python环境
Py_SetPythonHome(L"C:/ProgramData/Anaconda3");
Py_Initialize();
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.argv = ['python.py']");
PyRun_SimpleString("sys.path.append('./')");
// 导入模块
PyObject* pModule = PyImport_ImportModule("main_script2");
if (!pModule) {
PyErr_Print();
qDebug() << "Can't open python file!\n";
QMessageBox::information(NULL, "提示", "图片识别有误,请重试");
m_msgBox->close();
return; // 提前返回避免继续执行
} else {
qDebug() << "Python file has been opened";
}
// 获取函数
PyObject* pFunhello = PyObject_GetAttrString(pModule, "main");
if (!pFunhello || !PyCallable_Check(pFunhello)) {
PyErr_Print();
qDebug() << "Get function 'main' failed or is not callable";
Py_XDECREF(pFunhello);
Py_DECREF(pModule);
QMessageBox::information(NULL, "提示", "图片识别有误,请重试");
m_msgBox->close();
return; // 提前返回避免继续执行
}
// 传参
PyObject* args = PyTuple_New(2);
if (!args) {
PyErr_Print();
qDebug() << "Failed to create argument tuple";
Py_DECREF(pFunhello);
Py_DECREF(pModule);
QMessageBox::information(NULL, "提示", "图片识别有误,请重试");
m_msgBox->close();
return; // 提前返回避免继续执行
}
// PyObject* arg1 = PyUnicode_FromString("use/xijun/q.jpg");
// PyObject* arg2 = PyUnicode_FromString("use/xijun/y.jpg");
//实际运用
PyObject* arg1 = PyUnicode_FromString(yuan.toUtf8().constData());
PyObject* arg2 = PyUnicode_FromString(jie.toUtf8().constData());
PyTuple_SetItem(args, 0, arg1);
PyTuple_SetItem(args, 1, arg2);
QString param;
if (ui->label_3->text().isEmpty()) {
param = savePath4;
} else {
param = savePath2;
}
// 打印传入的参数
qDebug() << "传入 PyUnicode_FromString 的参数是: " << param;
PyTuple_SetItem(args, 0, arg1); // arg1
PyTuple_SetItem(args, 1, arg2); // arg2 - 这里仍需注意,将直接把 arg1 和 arg2 的引用设置到args中
// 调用函数
PyObject* result = PyObject_CallObject(pFunhello, args);
Py_DECREF(args); // 因为在PyTuple_SetItem中会管理内存调用完要释放
if (!result) {
PyErr_Print();
qDebug() << "Call function failed";
QMessageBox::information(NULL, "提示", "图片识别有误,请重试");
m_msgBox->close();
return; // 提前返回避免继续执行
} else {
// 处理返回值
if (PyLong_Check(result)) {
long returned_value = PyLong_AsLong(result);
qDebug() << "返回数字 " << returned_value;
int value=returned_value*1.15;
count = value;
qDebug() << "显示数字 " << value;
ui->label_2->setText(QString::number(value));
emit show();
} else {
qDebug() << "Function did not return an integer as expected";
}
Py_DECREF(result); // 释放返回值
}
Py_DECREF(pFunhello); // 释放函数引用
Py_DECREF(pModule); // 释放模块引用
}
void bacteria::usepython_quick(QString yuan,QString jie) {
//设置Python环境
Py_SetPythonHome(L"C:/ProgramData/Anaconda3");
Py_Initialize();
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.argv = ['python.py']");
PyRun_SimpleString("sys.path.append('./')");
// 导入模块
PyObject* pModule = PyImport_ImportModule("cell_count");
if (!pModule) {
PyErr_Print();
qDebug() << "Can't open python file!\n";
QMessageBox::information(NULL, "提示", "图片识别有误,请重试");
m_msgBox->close();
return; // 提前返回避免继续执行
} else {
qDebug() << "Python file has been opened";
}
// 获取函数
PyObject* pFunhello = PyObject_GetAttrString(pModule, "main");
if (!pFunhello || !PyCallable_Check(pFunhello)) {
PyErr_Print();
qDebug() << "Get function 'main' failed or is not callable";
Py_XDECREF(pFunhello);
Py_DECREF(pModule);
QMessageBox::information(NULL, "提示", "图片识别有误,请重试");
m_msgBox->close();
return; // 提前返回避免继续执行
}
// 传参
PyObject* args = PyTuple_New(2);
if (!args) {
PyErr_Print();
qDebug() << "Failed to create argument tuple";
Py_DECREF(pFunhello);
Py_DECREF(pModule);
QMessageBox::information(NULL, "提示", "图片识别有误,请重试");
m_msgBox->close();
return; // 提前返回避免继续执行
}
// PyObject* arg1 = PyUnicode_FromString("use/1.png");
// PyObject* arg2 = PyUnicode_FromString("use/2.png");
//实际运用
PyObject* arg1 = PyUnicode_FromString(yuan.toUtf8().constData());
PyObject* arg2 = PyUnicode_FromString(jie.toUtf8().constData());
PyTuple_SetItem(args, 0, arg1);
PyTuple_SetItem(args, 1, arg2);
// 打印传入的参数
qDebug() << "传入 PyUnicode_FromString 的参数是: " <<yuan<<jie;
PyTuple_SetItem(args, 0, arg1); // arg1
PyTuple_SetItem(args, 1, arg2); // arg2 - 这里仍需注意,将直接把 arg1 和 arg2 的引用设置到args中
// 调用函数
PyObject* result = PyObject_CallObject(pFunhello, args);
Py_DECREF(args); // 因为在PyTuple_SetItem中会管理内存调用完要释放
if (!result) {
PyErr_Print();
qDebug() << "Call function failed";
QMessageBox::information(NULL, "提示", "图片识别有误,请重试");
m_msgBox->close();
return; // 提前返回避免继续执行
} else {
// 处理返回值
if (PyLong_Check(result)) {
long returned_value = PyLong_AsLong(result);
qDebug() << "返回数字 " << returned_value;
int value=returned_value*1.15;
count = value;
qDebug() << "显示数字 " << value;
ui->label_2->setText(QString::number(value));
emit show();
} else {
qDebug() << "Function did not return an integer as expected";
}
Py_DECREF(result); // 释放返回值
}
Py_DECREF(pFunhello); // 释放函数引用
Py_DECREF(pModule); // 释放模块引用
}
//拍照
void bacteria::on_pushButton_clicked()
{
if(save==false)
{
int ok=QMessageBox::information(NULL,"保存","请先保存照片", QMessageBox::Ok | QMessageBox::Cancel);
if (ok == QMessageBox::Ok) {
}
}else{
if(ui->pushButton_2->text()=="已连接")
{
if(!f.isRunning()){
ui->label_4->setHidden(true);
ui->label_5->clear();
ui->label_2->clear();
ui->label_5->setHidden(false);
m_msgBox->close();
m_zidingyi->exec();
m_zidingyi->focus();
QString folderName;
QString timeStamp = QDate::currentDate().toString("yyyy-MM-dd");
folderName = "use/xijun/" + timeStamp;
QString timeStampFull = QTime::currentTime().toString("hh-mm-ss");
savePath3 = folderName + "/" + timeStampFull + "_Y" + ".png";
savePath4 = folderName + "/" + timeStampFull + "_J" + ".png";
xtshoudong=folderName + "/" + timeStampFull + "_C" + ".png";
QString zidingyi=ui->label_3->text();
savePath = folderName + "/" + zidingyi + "_Y" + ".png";
savePath2 = folderName + "/" + zidingyi + "_J" + ".png";
zmshoudong=folderName + "/" + zidingyi + "_C" + ".png";
if(jixu==true){
QTimer::singleShot(1,this,[=](){m_cammer.zhuapai();});
QTimer::singleShot(500,this,[=](){
qDebug() << "定时器触发"; // 调试信息
a=1;
QPixmap p;
// if(p.load("use/1.png"))
if(p.load(savelujing))
{
ui->label_5->setScaledContents(true);
ui->label_5->setPixmap(p);
QTimer::singleShot(1000,this,[=](){
qDebug() << "开始尝试调用算法";
// DWORD result = WaitForSingleObject(hthread, 0);
// if (result == WAIT_TIMEOUT) {
// qDebug() << "Thread is running.";
// }
// else{//hthread = CreateThread(NULL, 0, MyThreadFunction, NULL, 0, NULL);
// }
// usepython_quick(savelujing,ui->label_3->text().isEmpty() ?savePath4 :savePath2);
usepython();
// f = QtConcurrent::run(&bacteria::usepython, this);
qDebug()<<"开启线程";
}
);
}
else
{
qDebug() << "图片加载失败: " ;
} });
};
}
else{ QMessageBox::information(NULL, "提示", "图片正在处理中,请稍后" );qDebug()<<"线程还没有结束";}
}else{
QMessageBox::information(NULL, "提示", "请先连接相机" );
}
}
}
void bacteria::showtu()
{
qDebug()<<"已经得到返回值"
"开始显示图片";
// 关闭线程句柄
//CloseHandle(hthread);
ui->label_5->setHidden(true);
ui->label_4->setHidden(false);
if(ui->label_3->text().isEmpty()){
ui->label_4->setPixmap(savePath4);
ui->label_4->setScaledContents(true);
QPixmap pixmap2 = ui->label_4->pixmap();
// QString kupath1 = "C:/Users/20486/Desktop/xijun1.3/build/Desktop_Qt_6_5_3_MinGW_64_bit-Debug/"+savePath3;
// QString kupath2 = "C:/Users/20486/Desktop/xijun1.3/build/Desktop_Qt_6_5_3_MinGW_64_bit-Debug/"+savePath4;
// 开启事务,确保操作的原子性
db.transaction();
// 第一步:更新现有行的 xuhao使其序号依次加 1
// 从最大的 xuhao 开始递减更新,避免 xuhao 冲突
QSqlQuery updateQuery(db);
updateQuery.prepare("SELECT MAX(xuhao) FROM xijun");
if (updateQuery.exec() && updateQuery.next()) {
int maxXuhao = updateQuery.value(0).toInt();
for (int i = maxXuhao; i >= 1; --i) {
QSqlQuery updateXuhaoQuery(db);
updateXuhaoQuery.prepare("UPDATE xijun SET xuhao = :newXuhao WHERE xuhao = :oldXuhao");
updateXuhaoQuery.bindValue(":newXuhao", i + 1);
updateXuhaoQuery.bindValue(":oldXuhao", i);
if (!updateXuhaoQuery.exec()) {
qDebug() << "更新 xuhao 失败:" << updateXuhaoQuery.lastError().text();
// 回滚事务
db.rollback();
return;
}
}
} else {
qDebug() << "获取最大 xuhao 失败:" << updateQuery.lastError().text();
// 回滚事务
db.rollback();
return;
}
// 新增:插入多列数据
QSqlQuery query(db);
query.prepare("INSERT INTO xijun (xuhao,name, time, yuanshi, jieguo, jishu) VALUES (:xuhao,:name, :time, :yuanshi, :jieguo, :jishu)");
query.bindValue(":xuhao", 1);
query.bindValue(":name", id2);
query.bindValue(":time", gettime());
query.bindValue(":yuanshi", savePath3);
query.bindValue(":jieguo", savePath4);
query.bindValue(":jishu", ui->label_2->text());
if (!query.exec()) {
qDebug() << "插入多列数据失败:" << query.lastError().text();
// 回滚事务
db.rollback();
return;
}
// 提交事务
if (db.commit()) {
qDebug() << "数据插入并重新排序成功";
} else {
qDebug() << "事务提交失败:" << db.lastError().text();
}
m_msgBox->close();
emit shuaxin();
a=0;
ui->horizontalSlider_5->setEnabled(false);
ui->horizontalSlider_6->setEnabled(false);
ui->horizontalSlider_7->setEnabled(false);
ui->tableWidget_3->insertRow(row);
QTableWidgetItem *filePathItem = new QTableWidgetItem();
filePathItem->setText(savePath4);
QTableWidgetItem *filePathItem2 = new QTableWidgetItem();
filePathItem2->setText(QString::number(count));
QTableWidgetItem *pixmapItem = new QTableWidgetItem();
pixmap2 = pixmap2.scaled(175,175, Qt::KeepAspectRatio, Qt::SmoothTransformation);
pixmapItem->setIcon(QIcon(pixmap2));
ui->tableWidget_3->setIconSize(QSize(175,175));
ui->tableWidget_3->setRowHeight(row,170);
ui->tableWidget_3->setItem(row, 1, filePathItem);
ui->tableWidget_3->setItem(row, 0, pixmapItem);
ui->tableWidget_3->setItem(row, 2, filePathItem2);
QtConcurrent::run(&bacteria::savezhao, this);
}
else{
ui->label_4->setPixmap(savePath2);
ui->label_4->setScaledContents(true);
QString zidingyi=ui->label_3->text();
QPixmap pixmap = ui->label_5->pixmap();
QPixmap pixmap2 = ui->label_4->pixmap();
// QString kupath1 = "C:/Users/20486/Desktop/xijun1.3/build/Desktop_Qt_6_5_3_MinGW_64_bit-Debug/"+savePath;
// QString kupath2 = "C:/Users/20486/Desktop/xijun1.3/build/Desktop_Qt_6_5_3_MinGW_64_bit-Debug/"+savePath2;
QSqlQuery query;
query.prepare("INSERT INTO xijun (name, time, yuanshi, jieguo, jishu) VALUES (:name, :time, :yuanshi, :jieguo, :jishu)");
query.bindValue(":name", id2);
query.bindValue(":time", gettime());
query.bindValue(":yuanshi", savePath);
query.bindValue(":jieguo", savePath2);
query.bindValue(":jishu", ui->label_2->text());
m_msgBox->close();
emit shuaxin();
// ui->label_4->clear();
// ui->label_5->clear();
// ui->listWidget->clear();
// ui->label_2->clear();
a=0;
ui->horizontalSlider_5->setEnabled(false);
ui->horizontalSlider_6->setEnabled(false);
ui->horizontalSlider_7->setEnabled(false);
if (!query.exec()) {
// 处理错误
qDebug() << query.lastError().text();
}
ui->tableWidget_3->insertRow(row);
QTableWidgetItem *filePathItem = new QTableWidgetItem();
filePathItem->setText(savePath2);
QTableWidgetItem *filePathItem2 = new QTableWidgetItem();
filePathItem2->setText(QString::number(count));
QTableWidgetItem *pixmapItem = new QTableWidgetItem();
pixmap2 = pixmap2.scaled(175,175, Qt::KeepAspectRatio, Qt::SmoothTransformation);
pixmapItem->setIcon(QIcon(pixmap2));
ui->tableWidget_3->setIconSize(QSize(175,175));
ui->tableWidget_3->setItem(row, 1, filePathItem);
ui->tableWidget_3->setItem(row, 0, pixmapItem);
ui->tableWidget_3->setItem(row, 2, filePathItem2);
ui->tableWidget_3->setRowHeight(0,170);
ui->tableWidget_3->setRowHeight(1,170);
row++;
QtConcurrent::run(&bacteria::savezhao, this);
}
}
void bacteria::savezhao()
{
qDebug()<<"开始保存照片";
QString folderName;
QString timeStamp = QDate::currentDate().toString("yyyy-MM-dd");
folderName = "use/xijun/" + timeStamp;
QDir dir(folderName);
if (!dir.exists()) {
if (!dir.mkpath(folderName)) {
qDebug() << "Failed to create directory: " << folderName;
return;
}
}
if (ui->label_3->text().isEmpty()) {
// QString timeStampFull = QTime::currentTime().toString("hh-mm-ss");
QPixmap pixmap = ui->label_5->pixmap();
// QString savePath = folderName + "/" + timeStampFull + "_Y" + ".png";
QPixmap pixmap2 = ui->label_4->pixmap();
//QString savePath2 = folderName + "/" + timeStampFull + "_J" + ".png";
if (pixmap.save(savePath3)) {
qDebug() << "Saved pixmap to: " << savePath3;
} else {
qDebug() << "Failed to save pixmap to: " << savePath3;
QMessageBox::information(NULL, "保存", "保存失败", QMessageBox::Ok | QMessageBox::Cancel);
}
} else {
QPixmap pixmap = ui->label_5->pixmap();
QPixmap pixmap2 = ui->label_4->pixmap();
if (pixmap.save(savePath)) {
qDebug() << "Saved pixmap to: " << savePath;
} else {
qDebug() << "Failed to save pixmap to: " << savePath;
QMessageBox::information(NULL, "保存", "保存失败", QMessageBox::Ok | QMessageBox::Cancel);
}
}
}
QString bacteria::getname()
{
QString sql=QString("select usersname from users where id='%1'").arg(id2);
qDebug()<<sql;
QSqlQuery query;
if(query.exec(sql))
{
if(query.first()){
QString str1=query.value(0).toString();
qDebug()<<str1;
return str1;
}
}
else
{
qDebug()<<"没查到名字";
}
}
QString bacteria::gettime()
{
QDateTime dateTime= QDateTime::currentDateTime();//获取系统当前的时间
QString str = dateTime .toString("yyyy-MM-dd hh:mm:ss");//格式化时间
return str;
}
//保存
void bacteria::baocun()
{
QString aFile=QString("use/1.png");
QString currentTime = QDateTime::currentDateTime().toString("yyyyMMddhhmmss");
QString fileName = QString("image_%1.png").arg(currentTime);
QPixmap pixmap = ui->label_4->pixmap();
// QPixmap pixmap(aFile);
QString savePath = "use/unusexijun";
QDir dir(savePath);
if (!dir.exists()) {
dir.mkpath(savePath);
}
QString filePath = dir.filePath(fileName);
pixmap.save(filePath);
// 创建一个QTableWidgetItem用于存放文件路径第二列展示文件路径
QTableWidgetItem *filePathItem = new QTableWidgetItem();
filePathItem->setText(filePath);
QTableWidgetItem *filePathItem2 = new QTableWidgetItem();
filePathItem2->setText(QString::number(count));
// 创建一个QTableWidgetItem用于存放Pixmap第一列展示图片
QTableWidgetItem *pixmapItem = new QTableWidgetItem();
if (pixmap.isNull()) {
qDebug() << "图片加载失败,请检查文件路径和图片格式是否正确";
return;
}
// 对图片进行缩放保持长宽比使其适应指定大小这里设置为100x100
pixmap = pixmap.scaled(175,175, Qt::KeepAspectRatio, Qt::SmoothTransformation);
pixmapItem->setIcon(QIcon(pixmap));
ui->tableWidget_3->setIconSize(QSize(175,175));
// 插入新行
ui->tableWidget_3->insertRow(row);
// 将Pixmap对应的Item添加到第一列
ui->tableWidget_3->setItem(row, 0, pixmapItem);
// 将文件路径对应的Item添加到第二列
ui->tableWidget_3->setItem(row, 1, filePathItem);
ui->tableWidget_3->setItem(row, 2, filePathItem2);
ui->tableWidget_3->setRowHeight(row,170);
row++;
}
//计数
void bacteria::on_pushButton_3_clicked()
{
}
//重新拍照
void bacteria::on_pushButton_4_clicked()
{
}
//连接相机
void bacteria::on_pushButton_2_clicked()
{
ui->widget_3->setHidden(false);
ui->widget_3->setxijun();
QTimer::singleShot(100,this,[=](){
if (!ui->widget_3->CameraOpen())
{
qDebug()<<"shibai";
QMessageBox::information(NULL, "提示", "连接相机失败" );
}
else{qDebug()<<"chenggog";
ui->widget_3->CameraStart();
ui->widget_3->resetStatistic();
m_staticTimer.start(100);
ui->pushButton->setEnabled(true);
ui->pushButton_2->setText("已连接");
ui->pushButton_2->setEnabled(false);
ui->horizontalSlider->setEnabled(true);
ui->horizontalSlider_2->setEnabled(true);
}
foreach (const QSerialPortInfo &info,QSerialPortInfo::availablePorts())
{
QSerialPort serial;
serial.setPort(info);
if(serial.open(QIODevice::ReadWrite))
{
SerialPort.setPortName("COM3");
qDebug()<<info.portName();
}
}
// 打开串口
if(SerialPort.open(QIODevice::ReadWrite))
{
SerialPort.setBaudRate(QSerialPort::Baud9600);
SerialPort.setDataBits(QSerialPort::Data8);
SerialPort.setStopBits(QSerialPort::OneStop);
SerialPort.setParity(QSerialPort::NoParity);
SerialPort.setFlowControl(QSerialPort::NoFlowControl);
qDebug()<<"成功打开串口";
QTimer::singleShot(10,this,[=](){
SerialPort.write("$1204C60");
});
QTimer::singleShot(20,this,[=](){
SerialPort.write("$2101E63");});
ui->horizontalSlider->setEnabled(true);
ui->horizontalSlider_2->setEnabled(true);
}
});
}
//历史记录
void bacteria::on_pushButton_5_clicked()
{
m_bachistoricalrecords->showFullScreen();
}
//当点击菌落计数时
void bacteria::getkai()
{
qDebug()<<"菌落getkai";
ui->pushButton->setHidden(false);
ui->pushButton->setEnabled(false);
ui->pushButton_4->setHidden(true);
ui->pushButton_2->setText("连接相机");
ui->pushButton_2->setEnabled(true);
ui->label_4->clear();
ui->label_5->clear();
ui->label_2->clear();
}
//当点击MIC判读时
void bacteria::getguan()
{
qDebug()<<"菌落getguan";
ui->widget_3->CameraStop();
ui->widget_3->CameraClose();
QTimer::singleShot(500,this,[=](){ SerialPort.close();
qDebug()<<"串口关闭"; } );
// //关闭上光源
// QTimer::singleShot(100,this,[=](){
// int num = ui->label_6->text().toInt();
// QString hexValue = QString("%1").arg(num, 0, 16).toUpper();
// if (hexValue.length() == 1) {
// hexValue.prepend("0");
// }
// char char1 = hexValue.at(0).toLatin1();
// char char2 = hexValue.at(1).toLatin1();
// // qDebug() << "转换后的第一个char型字符为: " << char1;
// //qDebug() << "转换后的第二个char型字符为: " << char2;
// char fChar1 = char1;
// int asciiValue1 = (int)fChar1;
// QString hexValue1 = QString("%1").arg(asciiValue1, 0, 16).toUpper();
// // qDebug() << "字符 '3' 对应的ASCII码十进制为: " << asciiValue1;
// // qDebug() << "字符 '3' 对应的ASCII码十六进制为: " << hexValue1;
// char fChar2 =char2;
// int asciiValue2 = (int)fChar2;
// QString hexValue2 = QString("%1").arg(asciiValue2, 0, 16).toUpper();
// //qDebug() << "字符 '2' 对应的ASCII码十进制为: " << asciiValue2;
// //qDebug() << "字符 '2' 对应的ASCII码十六进制为: " << hexValue2;
// // 计算字符 '$' 的相关信息
// char fChar3 = '$';
// int asciiValue3 = (int)fChar3;
// QString hexValue3 = QString("%1").arg(asciiValue3, 0, 16).toUpper();
// // qDebug() << "字符 '$' 对应的ASCII码十进制为: " << asciiValue3;
// //qDebug() << "字符 '$' 对应的ASCII码十六进制为: " << hexValue3;
// // 计算字符 '1' 的相关信息
// char fChar4 = '2';
// int asciiValue4 = (int)fChar4;
// QString hexValue4 = QString("%1").arg(asciiValue4, 0, 16).toUpper();
// // qDebug() << "字符 '1' 对应的ASCII码十进制为: " << asciiValue4;
// //qDebug() << "字符 '1' 对应的ASCII码十六进制为: " << hexValue4;
// // 计算字符 '0' 的相关信息
// char fChar5 = '1';
// int asciiValue5 = (int)fChar5;
// QString hexValue5 = QString("%1").arg(asciiValue5, 0, 16).toUpper();
// //qDebug() << "字符 '0' 对应的ASCII码十进制为: " << asciiValue5;
// //qDebug() << "字符 '0' 对应的ASCII码十六进制为: " << hexValue5;
// char fChar6 = '0';
// int asciiValue6= (int)fChar6;
// QString hexValue6= QString("%1").arg(asciiValue6, 0, 16).toUpper();
// //qDebug() << "字符 '1' 对应的ASCII码十进制为: " << asciiValue6;
// // qDebug() << "字符 '1' 对应的ASCII码十六进制为: " << hexValue6;
// bool ok1, ok2, ok3, ok4, ok5, ok6;
// int decimalValue1 = hexValue1.toInt(&ok1, 16);
// int decimalValue2 = hexValue2.toInt(&ok2, 16);
// int decimalValue3 = hexValue3.toInt(&ok3, 16);
// int decimalValue4 = hexValue4.toInt(&ok4, 16);
// int decimalValue5 = hexValue5.toInt(&ok5, 16);
// int decimalValue6 = hexValue6.toInt(&ok6, 16);
// if (ok1 && ok2 && ok3 && ok4 && ok5 && ok6) {
// int xorResult = decimalValue1 ^ decimalValue2 ^ decimalValue3 ^ decimalValue4 ^ decimalValue5 ^ decimalValue6;
// QString xorHexResult = QString("%1").arg(xorResult, 0, 16).toUpper();
// // qDebug() << "hexValue1、hexValue2、hexValue3、hexValue4、hexValue5、hexValue6异或运算结果十六进制为: " << xorHexResult;
// QString hexString = QString("%1").arg(num, 0, 16).toUpper();
// qDebug() << "整数50转换后的十六进制字符为: " << hexString;
// QString kai = "$210" + QString(char1) + QString(char2) + xorHexResult;
// // qDebug()<<kai;
// QByteArray byteArray = kai.toUtf8();
// SerialPort.write(byteArray);
// // qDebug()<<"上光源关了";
// } else {
// qDebug() << "十六进制字符串转换为整数失败,无法进行异或运算";
// }
// }
// );
// //关闭下光源
// QTimer::singleShot(200,this,[=](){
// int num = ui->label_9->text().toInt();
// QString hexValue = QString("%1").arg(num, 0, 16).toUpper();
// //qDebug() << "整数 " << num << " 转换后的十六进制表示为: " << hexValue;
// // 如果hexValue长度为1在前面补0
// if (hexValue.length() == 1) {
// hexValue.prepend("0");
// }
// char char1 = hexValue.at(0).toLatin1();
// char char2 = hexValue.at(1).toLatin1();
// // qDebug() << "转换后的第一个char型字符为: " << char1;
// // qDebug() << "转换后的第二个char型字符为: " << char2;
// char fChar1 = char1;
// int asciiValue1 = (int)fChar1;
// QString hexValue1 = QString("%1").arg(asciiValue1, 0, 16).toUpper();
// // qDebug() << "字符 '3' 对应的ASCII码十进制为: " << asciiValue1;
// //qDebug() << "字符 '3' 对应的ASCII码十六进制为: " << hexValue1;
// char fChar2 =char2;
// int asciiValue2 = (int)fChar2;
// QString hexValue2 = QString("%1").arg(asciiValue2, 0, 16).toUpper();
// // qDebug() << "字符 '2' 对应的ASCII码十进制为: " << asciiValue2;
// // qDebug() << "字符 '2' 对应的ASCII码十六进制为: " << hexValue2;
// // 计算字符 '$' 的相关信息
// char fChar3 = '$';
// int asciiValue3 = (int)fChar3;
// QString hexValue3 = QString("%1").arg(asciiValue3, 0, 16).toUpper();
// //qDebug() << "字符 '$' 对应的ASCII码十进制为: " << asciiValue3;
// // qDebug() << "字符 '$' 对应的ASCII码十六进制为: " << hexValue3;
// // 计算字符 '1' 的相关信息
// char fChar4 = '2';
// int asciiValue4 = (int)fChar4;
// QString hexValue4 = QString("%1").arg(asciiValue4, 0, 16).toUpper();
// //qDebug() << "字符 '1' 对应的ASCII码十进制为: " << asciiValue4;
// //qDebug() << "字符 '1' 对应的ASCII码十六进制为: " << hexValue4;
// // 计算字符 '0' 的相关信息
// char fChar5 = '2';
// int asciiValue5 = (int)fChar5;
// QString hexValue5 = QString("%1").arg(asciiValue5, 0, 16).toUpper();
// //qDebug() << "字符 '0' 对应的ASCII码十进制为: " << asciiValue5;
// //qDebug() << "字符 '0' 对应的ASCII码十六进制为: " << hexValue5;
// char fChar6 = '0';
// int asciiValue6= (int)fChar6;
// QString hexValue6= QString("%1").arg(asciiValue6, 0, 16).toUpper();
// //qDebug() << "字符 '1' 对应的ASCII码十进制为: " << asciiValue6;
// //qDebug() << "字符 '1' 对应的ASCII码十六进制为: " << hexValue6;
// bool ok1, ok2, ok3, ok4, ok5, ok6;
// int decimalValue1 = hexValue1.toInt(&ok1, 16);
// int decimalValue2 = hexValue2.toInt(&ok2, 16);
// int decimalValue3 = hexValue3.toInt(&ok3, 16);
// int decimalValue4 = hexValue4.toInt(&ok4, 16);
// int decimalValue5 = hexValue5.toInt(&ok5, 16);
// int decimalValue6 = hexValue6.toInt(&ok6, 16);
// if (ok1 && ok2 && ok3 && ok4 && ok5 && ok6) {
// int xorResult = decimalValue1 ^ decimalValue2 ^ decimalValue3 ^ decimalValue4 ^ decimalValue5 ^ decimalValue6;
// QString xorHexResult = QString("%1").arg(xorResult, 0, 16).toUpper();
// //qDebug() << "hexValue1、hexValue2、hexValue3、hexValue4、hexValue5、hexValue6异或运算结果十六进制为: " << xorHexResult;
// QString hexString = QString("%1").arg(num, 0, 16).toUpper();
// // qDebug() << "整数50转换后的十六进制字符为: " << hexString;
// QString kai = "$220" + QString(char1) + QString(char2) + xorHexResult;
// // qDebug()<<kai;
// QByteArray byteArray = kai.toUtf8();
// SerialPort.write(byteArray);
// // qDebug()<<"下光源关了";
// } else {
// //qDebug() << "十六进制字符串转换为整数失败,无法进行异或运算";
// }
// }
// );
}
// void bacteria::on_checkBox_clicked()
// {
// if(ui->checkBox->isChecked()){
// SerialPort.write("$110281E");
// }
// else {
// SerialPort.write("$210281D");
// }
// }
// void bacteria::on_checkBox_2_clicked()
// {
// if(ui->checkBox_2->isChecked()){SerialPort.write("$1201711");}
// else{SerialPort.write("$2201712");}
// }
//通道1上光源
void bacteria::on_horizontalSlider_valueChanged(int value)
{
ui->label_6->setText(QString::number(value));
int num = value;
QString hexValue = QString("%1").arg(num, 0, 16).toUpper();
qDebug() << "整数 " << num << " 转换后的十六进制表示为: " << hexValue;
// 如果hexValue长度为1在前面补0
if (hexValue.length() == 1) {
hexValue.prepend("0");
}
char char1 = hexValue.at(0).toLatin1();
char char2 = hexValue.at(1).toLatin1();
qDebug() << "转换后的第一个char型字符为: " << char1;
qDebug() << "转换后的第二个char型字符为: " << char2;
char fChar1 = char1;
int asciiValue1 = (int)fChar1;
QString hexValue1 = QString("%1").arg(asciiValue1, 0, 16).toUpper();
qDebug() << "字符 '3' 对应的ASCII码十进制为: " << asciiValue1;
qDebug() << "字符 '3' 对应的ASCII码十六进制为: " << hexValue1;
char fChar2 =char2;
int asciiValue2 = (int)fChar2;
QString hexValue2 = QString("%1").arg(asciiValue2, 0, 16).toUpper();
qDebug() << "字符 '2' 对应的ASCII码十进制为: " << asciiValue2;
qDebug() << "字符 '2' 对应的ASCII码十六进制为: " << hexValue2;
// 计算字符 '$' 的相关信息
char fChar3 = '$';
int asciiValue3 = (int)fChar3;
QString hexValue3 = QString("%1").arg(asciiValue3, 0, 16).toUpper();
qDebug() << "字符 '$' 对应的ASCII码十进制为: " << asciiValue3;
qDebug() << "字符 '$' 对应的ASCII码十六进制为: " << hexValue3;
// 计算字符 '1' 的相关信息
char fChar4 = '1';
int asciiValue4 = (int)fChar4;
QString hexValue4 = QString("%1").arg(asciiValue4, 0, 16).toUpper();
qDebug() << "字符 '1' 对应的ASCII码十进制为: " << asciiValue4;
qDebug() << "字符 '1' 对应的ASCII码十六进制为: " << hexValue4;
// 计算字符 '0' 的相关信息
char fChar5 = '1';
int asciiValue5 = (int)fChar5;
QString hexValue5 = QString("%1").arg(asciiValue5, 0, 16).toUpper();
qDebug() << "字符 '0' 对应的ASCII码十进制为: " << asciiValue5;
qDebug() << "字符 '0' 对应的ASCII码十六进制为: " << hexValue5;
char fChar6 = '0';
int asciiValue6= (int)fChar6;
QString hexValue6= QString("%1").arg(asciiValue6, 0, 16).toUpper();
qDebug() << "字符 '1' 对应的ASCII码十进制为: " << asciiValue6;
qDebug() << "字符 '1' 对应的ASCII码十六进制为: " << hexValue6;
bool ok1, ok2, ok3, ok4, ok5, ok6;
int decimalValue1 = hexValue1.toInt(&ok1, 16);
int decimalValue2 = hexValue2.toInt(&ok2, 16);
int decimalValue3 = hexValue3.toInt(&ok3, 16);
int decimalValue4 = hexValue4.toInt(&ok4, 16);
int decimalValue5 = hexValue5.toInt(&ok5, 16);
int decimalValue6 = hexValue6.toInt(&ok6, 16);
if (ok1 && ok2 && ok3 && ok4 && ok5 && ok6) {
int xorResult = decimalValue1 ^ decimalValue2 ^ decimalValue3 ^ decimalValue4 ^ decimalValue5 ^ decimalValue6;
QString xorHexResult = QString("%1").arg(xorResult, 0, 16).toUpper();
qDebug() << "hexValue1、hexValue2、hexValue3、hexValue4、hexValue5、hexValue6异或运算结果十六进制为: " << xorHexResult;
QString hexString = QString("%1").arg(num, 0, 16).toUpper();
qDebug() << "整数50转换后的十六进制字符为: " << hexString;
QString kai = "$110" + QString(char1) + QString(char2) + xorHexResult;
qDebug()<<kai;
QByteArray byteArray = kai.toUtf8();
SerialPort.write(byteArray);
} else {
qDebug() << "十六进制字符串转换为整数失败,无法进行异或运算";
}
}
//下光源
void bacteria::on_horizontalSlider_2_valueChanged(int value)
{
int num =value;
ui->label_9->setText(QString::number(value));
QString hexValue = QString("%1").arg(num, 0, 16).toUpper();
qDebug() << "整数 " << num << " 转换后的十六进制表示为: " << hexValue;
// 如果hexValue长度为1在前面补0
if (hexValue.length() == 1) {
hexValue.prepend("0");
}
char char1 = hexValue.at(0).toLatin1();
char char2 = hexValue.at(1).toLatin1();
qDebug() << "转换后的第一个char型字符为: " << char1;
qDebug() << "转换后的第二个char型字符为: " << char2;
char fChar1 = char1;
int asciiValue1 = (int)fChar1;
QString hexValue1 = QString("%1").arg(asciiValue1, 0, 16).toUpper();
qDebug() << "字符 '3' 对应的ASCII码十进制为: " << asciiValue1;
qDebug() << "字符 '3' 对应的ASCII码十六进制为: " << hexValue1;
char fChar2 =char2;
int asciiValue2 = (int)fChar2;
QString hexValue2 = QString("%1").arg(asciiValue2, 0, 16).toUpper();
qDebug() << "字符 '2' 对应的ASCII码十进制为: " << asciiValue2;
qDebug() << "字符 '2' 对应的ASCII码十六进制为: " << hexValue2;
// 计算字符 '$' 的相关信息
char fChar3 = '$';
int asciiValue3 = (int)fChar3;
QString hexValue3 = QString("%1").arg(asciiValue3, 0, 16).toUpper();
qDebug() << "字符 '$' 对应的ASCII码十进制为: " << asciiValue3;
qDebug() << "字符 '$' 对应的ASCII码十六进制为: " << hexValue3;
// 计算字符 '1' 的相关信息
char fChar4 = '1';
int asciiValue4 = (int)fChar4;
QString hexValue4 = QString("%1").arg(asciiValue4, 0, 16).toUpper();
qDebug() << "字符 '1' 对应的ASCII码十进制为: " << asciiValue4;
qDebug() << "字符 '1' 对应的ASCII码十六进制为: " << hexValue4;
// 计算字符 '0' 的相关信息
char fChar5 = '2';
int asciiValue5 = (int)fChar5;
QString hexValue5 = QString("%1").arg(asciiValue5, 0, 16).toUpper();
qDebug() << "字符 '0' 对应的ASCII码十进制为: " << asciiValue5;
qDebug() << "字符 '0' 对应的ASCII码十六进制为: " << hexValue5;
char fChar6 = '0';
int asciiValue6= (int)fChar6;
QString hexValue6= QString("%1").arg(asciiValue6, 0, 16).toUpper();
qDebug() << "字符 '1' 对应的ASCII码十进制为: " << asciiValue6;
qDebug() << "字符 '1' 对应的ASCII码十六进制为: " << hexValue6;
bool ok1, ok2, ok3, ok4, ok5, ok6;
int decimalValue1 = hexValue1.toInt(&ok1, 16);
int decimalValue2 = hexValue2.toInt(&ok2, 16);
int decimalValue3 = hexValue3.toInt(&ok3, 16);
int decimalValue4 = hexValue4.toInt(&ok4, 16);
int decimalValue5 = hexValue5.toInt(&ok5, 16);
int decimalValue6 = hexValue6.toInt(&ok6, 16);
if (ok1 && ok2 && ok3 && ok4 && ok5 && ok6) {
int xorResult = decimalValue1 ^ decimalValue2 ^ decimalValue3 ^ decimalValue4 ^ decimalValue5 ^ decimalValue6;
QString xorHexResult = QString("%1").arg(xorResult, 0, 16).toUpper();
qDebug() << "hexValue1、hexValue2、hexValue3、hexValue4、hexValue5、hexValue6异或运算结果十六进制为: " << xorHexResult;
QString hexString = QString("%1").arg(num, 0, 16).toUpper();
qDebug() << "整数50转换后的十六进制字符为: " << hexString;
QString kai = "$120" + QString(char1) + QString(char2) + xorHexResult;
qDebug()<<kai;
QByteArray byteArray = kai.toUtf8();
SerialPort.write(byteArray);
// if(a==1){ usepython();}
} else {
qDebug() << "十六进制字符串转换为整数失败,无法进行异或运算";
}
}
void bacteria::on_horizontalSlider_5_valueChanged(int value)
{
// ui->label_15->setText(QString::number(value));
// if(a==1){usepython();}
}
void bacteria::on_horizontalSlider_6_valueChanged(int value)
{
// ui->label_17->setText(QString::number(value));
// if(a==1){usepython();}
}
void bacteria::on_horizontalSlider_7_valueChanged(int value)
{
// ui->label_19->setText(QString::number(value));
// if(a==1){usepython();}
}
void bacteria::on_pushButton_6_clicked()
{
if(save==false)
{
QMessageBox::information(NULL,"保存","请先保存照片", QMessageBox::Ok | QMessageBox::Cancel);
}else{
// 获取应用程序目录的父目录
QString appDir = QCoreApplication::applicationDirPath();
QDir dir(appDir);
// if (dir.cdUp()) { // 尝试返回上一级目录
// appDir = dir.absolutePath();
// } else {
// qDebug() << "无法返回上一级目录,使用原始目录";
// }
// 构建完整路径
QString initialDir = QDir::toNativeSeparators(appDir + "/use/xijun");
qDebug() << initialDir;
// 检查目录是否存在
QDir targetDir(initialDir);
if (!targetDir.exists()) {
qDebug() << "目录不存在,使用文档目录作为替代";
initialDir = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
}
// 创建文件对话框对象
QFileDialog fileDialog;
// 设置文件对话框的标题
fileDialog.setWindowTitle("打开文件");
// 设置文件对话框的文件模式为只允许选择文件
fileDialog.setFileMode(QFileDialog::ExistingFile);
// 设置文件对话框的初始目录
fileDialog.setDirectory(initialDir);
// 打开文件对话框,等待用户选择文件
if (fileDialog.exec() == QDialog::Accepted) {
// 获取用户选择的文件路径
QStringList fileNames = fileDialog.selectedFiles();
ui->label_4->setHidden(true);
ui->label_5->setHidden(false);
// 输出用户选择的文件路径
qDebug() << "选择的文件路径: " << fileNames.at(0);
savelujing= fileNames.at(0);
ui->label_5->clear();
ui->label_5->setPixmap(fileNames.at(0));//5存放的是原始图片
ui->label_5->setScaledContents(true);
int result = QMessageBox::information(NULL, "提示", "是否要将此照片进行重新计数", QMessageBox::Ok | QMessageBox::Cancel);
if (result == QMessageBox::Ok) {
if(!f.isRunning()){
ui->label_4->clear();
ui->label_2->clear();
m_msgBox->close();
m_zidingyi->exec();
QString folderName;
QString timeStamp = QDate::currentDate().toString("yyyy-MM-dd");
folderName = "use/xijun/" + timeStamp;
QString timeStampFull = QTime::currentTime().toString("hh-mm-ss");
savePath3 = folderName + "/" + timeStampFull + "_Y" + ".png";
savePath4 = folderName + "/" + timeStampFull + "_J" + ".png";
xtshoudong=folderName + "/" + timeStampFull + "_C" + ".png";
QString zidingyi=ui->label_3->text();
savePath = folderName + "/" + zidingyi + "_Y" + ".png";
savePath2 = folderName + "/" + zidingyi + "_J" + ".png";
zmshoudong=folderName + "/" + zidingyi + "_C" + ".png";
if(jixu==true){
QTimer::singleShot(1000,this,[=](){
qDebug() << "开始尝试调用算法";
usepython();
// f = QtConcurrent::run(&bacteria::usepython, this);
qDebug()<<"开启线程"; });
}
else
{
qDebug() << "图片加载失败: " ;
}
}
else{ QMessageBox::information(NULL, "提示", "图片正在处理中,请稍后" );qDebug()<<"线程还没有结束";}
}else{
qDebug() << "用户取消了文件计数";
}
} else {
qDebug() << "用户取消了文件选择";
}
}
}
void bacteria::on_pushButton_7_clicked()
{
QScrollBar *scrollBar = ui->tableWidget_3->verticalScrollBar();
// 获取当前滚动条的位置
int currentValue = scrollBar->value();
// 向下滑动的偏移量,可以根据需要调整
int offset = 100;
// 设置新的滚动条位置
scrollBar->setValue(currentValue + offset);
}
void bacteria::on_pushButton_8_clicked()
{
QScrollBar *scrollBar = ui->tableWidget_3->verticalScrollBar();
// 获取当前滚动条的位置
int currentValue = scrollBar->value();
// 向下滑动的偏移量,可以根据需要调整
int offset = 100;
// 设置新的滚动条位置
scrollBar->setValue(currentValue - offset);
}
// 获取所有进程信息并返回指定进程名的 PID 列表
QList<DWORD>bacteria::getProcessIdsByName(const QString& processName) {
QList<DWORD> pids;
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot == INVALID_HANDLE_VALUE) {
qDebug() << "CreateToolhelp32Snapshot failed.";
return pids;
}
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(hSnapshot, &pe32)) {
do {
QString currentProcessName = QString::fromWCharArray(pe32.szExeFile);
if (currentProcessName.contains(processName)) {
pids.append(pe32.th32ProcessID);
}
} while (Process32Next(hSnapshot, &pe32));
}
CloseHandle(hSnapshot);
return pids;
}
// 终止指定进程
bool bacteria::terminateProcess(pid_t pid) {
HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
if (hProcess == NULL) {
qDebug() << "OpenProcess failed.";
return false;
}
if (!TerminateProcess(hProcess, 0)) {
qDebug() << "TerminateProcess failed.";
CloseHandle(hProcess);
return false;
}
CloseHandle(hProcess);
return true;
}
// 获取进程名
QString bacteria:: getProcessName(DWORD pid) {
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
if (hProcess == NULL) {
return "";
}
wchar_t buffer[MAX_PATH];
if (GetModuleFileNameExW(hProcess, NULL, buffer, MAX_PATH) == 0) {
CloseHandle(hProcess);
return "";
}
QString processName = QString::fromWCharArray(buffer);
CloseHandle(hProcess);
return processName;
}
void bacteria::on_pushButton_9_clicked()
{
// 要终止的进程名,这里假设为 "your_process_name"
QString targetProcessName = "untitled14.exe";
// 获取指定进程名的 PID 列表
QList<DWORD> pids = getProcessIdsByName(targetProcessName);
if (pids.isEmpty()) {
qDebug() << "No process with name " << targetProcessName << " found.";
} else {
foreach (DWORD pid, pids) {
QString processName = getProcessName(pid);
if (!processName.isEmpty()) {
qDebug() << "Process Name: " << processName << " PID: " << pid;
}
if (terminateProcess(pid)) {
qDebug() << "Process with PID " << pid << " terminated successfully.";
} else {
qDebug() << "Failed to terminate process with PID " << pid << ".";
}
}
}
}
void bacteria::on_pushButton_10_clicked()
{
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot == INVALID_HANDLE_VALUE) {
qDebug() << "CreateToolhelp32Snapshot failed.";
return;
}
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(hSnapshot, &pe32)) {
do {
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pe32.th32ProcessID);
if (hProcess != NULL) {
wchar_t buffer[MAX_PATH];
if (GetModuleFileNameExW(hProcess, NULL, buffer, MAX_PATH) != 0) {
QString processName = QString::fromWCharArray(buffer);
qDebug() << "Process Name: " << processName << " PID: " << pe32.th32ProcessID;
}
CloseHandle(hProcess);
}
} while (Process32Next(hSnapshot, &pe32));
}
CloseHandle(hSnapshot);
}
void bacteria::on_pushButton_11_clicked()
{
if (TerminateThread(hthread, 0)) {
qDebug() << "Thread terminated successfully.";
} else {
qDebug() << "Failed to terminate thread.";
}
}
void bacteria::on_pushButton_12_clicked()
{
//有压缩
//usepython_quick();
//无压缩
// f = QtConcurrent::run(&bacteria::usepython_slow, this);
//ai算法
// usepython("","");
qDebug()<<"开始调用python文件";
}
void bacteria::on_pushButton_13_clicked()
{
//如果已经得到结果照片
if(ui->label_4->isVisible()){
eventFilterEnabled = false;
save=false;//菌落计数手动矫正的判断
qDebug() << "可以正常点击";
ui->pushButton_13->setHidden(true);
ui->pushButton_14->setHidden(false);
}else{
QMessageBox::information(NULL,"提示","无法进入审查模式");
}
}
//手动保存
void bacteria::on_pushButton_14_clicked()
{
qDebug()<<"手动保存";
if(ui->label_3->text().isEmpty()){
QPixmap pixmap2 = ui->label_4->pixmap();
// 开启事务,确保操作的原子性
db.transaction();
// 第一步:更新现有行的 xuhao使其序号依次加 1
// 从最大的 xuhao 开始递减更新,避免 xuhao 冲突
QSqlQuery updateQuery(db);
updateQuery.prepare("SELECT MAX(xuhao) FROM xijun");
if (updateQuery.exec() && updateQuery.next()) {
int maxXuhao = updateQuery.value(0).toInt();
for (int i = maxXuhao; i >= 1; --i) {
QSqlQuery updateXuhaoQuery(db);
updateXuhaoQuery.prepare("UPDATE xijun SET xuhao = :newXuhao WHERE xuhao = :oldXuhao");
updateXuhaoQuery.bindValue(":newXuhao", i + 1);
updateXuhaoQuery.bindValue(":oldXuhao", i);
if (!updateXuhaoQuery.exec()) {
qDebug() << "更新 xuhao 失败:" << updateXuhaoQuery.lastError().text();
// 回滚事务
db.rollback();
return;
}
}
} else {
qDebug() << "获取最大 xuhao 失败:" << updateQuery.lastError().text();
// 回滚事务
db.rollback();
return;
}
// 新增:插入多列数据
QSqlQuery query(db);
query.prepare("INSERT INTO xijun (xuhao,name, time, yuanshi, jieguo, jishu) VALUES (:xuhao,:name, :time, :yuanshi, :jieguo, :jishu)");
query.bindValue(":xuhao", 1);
query.bindValue(":name", id2);
query.bindValue(":time", gettime());
query.bindValue(":yuanshi", savePath3);
query.bindValue(":jieguo", xtshoudong);
query.bindValue(":jishu", ui->label_2->text());
if (!query.exec()) {
qDebug() << "插入多列数据失败:" << query.lastError().text();
// 回滚事务
db.rollback();
return;
}
// 提交事务
if (db.commit()) {
qDebug() << "数据插入并重新排序成功";
} else {
qDebug() << "事务提交失败:" << db.lastError().text();
}
emit shuaxin();
a=0;
ui->horizontalSlider_5->setEnabled(false);
ui->horizontalSlider_6->setEnabled(false);
ui->horizontalSlider_7->setEnabled(false);
ui->tableWidget_3->insertRow(row);
QTableWidgetItem *filePathItem = new QTableWidgetItem();
filePathItem->setText(xtshoudong);
QTableWidgetItem *filePathItem2 = new QTableWidgetItem();
filePathItem2->setText(QString::number(count));
QTableWidgetItem *pixmapItem = new QTableWidgetItem();
pixmap2 = pixmap2.scaled(175,175, Qt::KeepAspectRatio, Qt::SmoothTransformation);
pixmapItem->setIcon(QIcon(pixmap2));
ui->tableWidget_3->setIconSize(QSize(175,175));
ui->tableWidget_3->setRowHeight(row,170);
ui->tableWidget_3->setItem(row, 1, filePathItem);
ui->tableWidget_3->setItem(row, 0, pixmapItem);
ui->tableWidget_3->setItem(row, 2, filePathItem2);
// QtConcurrent::run(&bacteria::savezhao, this);
}
else{
ui->label_4->setPixmap(savePath2);
ui->label_4->setScaledContents(true);
QString zidingyi=ui->label_3->text();
QPixmap pixmap = ui->label_5->pixmap();
QPixmap pixmap2 = ui->label_4->pixmap();
// QString kupath1 = "C:/Users/20486/Desktop/xijun1.3/build/Desktop_Qt_6_5_3_MinGW_64_bit-Debug/"+savePath;
// QString kupath2 = "C:/Users/20486/Desktop/xijun1.3/build/Desktop_Qt_6_5_3_MinGW_64_bit-Debug/"+savePath2;
QSqlQuery query;
query.prepare("INSERT INTO xijun (name, time, yuanshi, jieguo, jishu) VALUES (:name, :time, :yuanshi, :jieguo, :jishu)");
query.bindValue(":name", id2);
query.bindValue(":time", gettime());
query.bindValue(":yuanshi", savePath);
query.bindValue(":jieguo", zmshoudong);
query.bindValue(":jishu", ui->label_2->text());
m_msgBox->close();
emit shuaxin();
// ui->label_4->clear();
// ui->label_5->clear();
// ui->listWidget->clear();
// ui->label_2->clear();
a=0;
ui->horizontalSlider_5->setEnabled(false);
ui->horizontalSlider_6->setEnabled(false);
ui->horizontalSlider_7->setEnabled(false);
if (!query.exec()) {
// 处理错误
qDebug() << query.lastError().text();
QMessageBox::information(NULL, "保存", "保存失败,退出审查模式", QMessageBox::Ok);
}else{
QMessageBox::information(NULL, "保存", "保存成功,退出审查模式", QMessageBox::Ok);
}
ui->tableWidget_3->insertRow(row);
QTableWidgetItem *filePathItem = new QTableWidgetItem();
filePathItem->setText(zmshoudong);
QTableWidgetItem *filePathItem2 = new QTableWidgetItem();
filePathItem2->setText(QString::number(count));
QTableWidgetItem *pixmapItem = new QTableWidgetItem();
pixmap2 = pixmap2.scaled(175,175, Qt::KeepAspectRatio, Qt::SmoothTransformation);
pixmapItem->setIcon(QIcon(pixmap2));
ui->tableWidget_3->setIconSize(QSize(175,175));
ui->tableWidget_3->setItem(row, 1, filePathItem);
ui->tableWidget_3->setItem(row, 0, pixmapItem);
ui->tableWidget_3->setItem(row, 2, filePathItem2);
ui->tableWidget_3->setRowHeight(0,170);
ui->tableWidget_3->setRowHeight(1,170);
row++;
}
if (ui->label_3->text().isEmpty()) {
QPixmap pixmap = ui->label_4->pixmap();
if (pixmap.save(xtshoudong)) {
qDebug() << "Saved pixmap to: " << xtshoudong;
// QMessageBox::information(NULL, "保存", "保存成功,退出审查模式", QMessageBox::Ok | QMessageBox::Cancel);
} else {
QMessageBox::information(NULL, "保存", "保存失败,退出审查模式", QMessageBox::Ok | QMessageBox::Cancel);
}
} else {
QPixmap pixmap = ui->label_4->pixmap();
if (pixmap.save(zmshoudong)) {
qDebug() << "Saved pixmap to: " << zmshoudong;
save=true;
eventFilterEnabled = true;//表示不能再点击了
ui->pushButton_13->setHidden(false);
ui->pushButton_14->setHidden(true);
// QMessageBox::information(NULL, "保存", "保存成功,退出审查模式", QMessageBox::Ok | QMessageBox::Cancel);
} else {
qDebug() << "Failed to save pixmap to: " << zmshoudong;
QMessageBox::information(NULL, "保存", "保存失败,退出审查模式", QMessageBox::Ok | QMessageBox::Cancel);
}
}
ui->label_4->clear();
ui->label_5->clear();
ui->label_4->setHidden(true);
ui->label_5->setHidden(false);
save=true;
eventFilterEnabled = true;//表示不能再点击了
ui->pushButton_13->setHidden(false);
ui->pushButton_14->setHidden(true);
}