From c7c24e938846acb058bad317879d0e90d6f0a7d6 Mon Sep 17 00:00:00 2001 From: luocai Date: Sat, 31 Aug 2024 11:37:20 +0800 Subject: [PATCH] correct code. --- Analyser/Application.cpp | 16 +++++++++-- Analyser/Application.h | 4 ++- Analyser/ImageDecoder.cpp | 49 ++++++++++++++++++++++---------- Analyser/ModuleCommunication.cpp | 4 +-- Analyser/ModuleCommunication.h | 1 + Analyser/qml/OperationItem.qml | 2 +- Analyser/qml/main.qml | 1 - 7 files changed, 54 insertions(+), 23 deletions(-) diff --git a/Analyser/Application.cpp b/Analyser/Application.cpp index 7596757..f6f379d 100644 --- a/Analyser/Application.cpp +++ b/Analyser/Application.cpp @@ -43,8 +43,6 @@ Application::Application(int &argc, char **argv) : m_app(std::make_sharedexec(); } +Application *Application::create(QQmlEngine *qmlEngine, QJSEngine *jsEngine) { + Application *ret = nullptr; + auto app = Amass::Singleton::instance(); + if (app) { + ret = app.get(); +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + QJSEngine::setObjectOwnership(ret, QJSEngine::CppOwnership); +#endif + } + return ret; +} + QStringList Application::availableSerialPorts() const { QStringList ret; auto ports = QSerialPortInfo::availablePorts(); @@ -368,7 +378,7 @@ void Application::onNewImageSliceData(const std::vector &data) { oss.str(""); oss << JpgPath << "/" << username << "_" << m_palmId << "_" << way << "_" << DateTime::toString(std::chrono::system_clock::now(), "%Y%m%d%H%M%S") << ".jpg"; - image.save(QString::fromStdString(oss.str()), "jpg", 100); + image.save(QString::fromStdString(oss.str()), "jpg", 80); } } diff --git a/Analyser/Application.h b/Analyser/Application.h index 0ac3a72..3580543 100644 --- a/Analyser/Application.h +++ b/Analyser/Application.h @@ -17,7 +17,8 @@ class CdcUpdater; class Application : public QObject { Q_OBJECT - QML_ELEMENT + QML_NAMED_ELEMENT(App) + QML_SINGLETON Q_PROPERTY(ModuleCommunication *module READ module NOTIFY connectedChanged) Q_PROPERTY(bool connected READ connected NOTIFY connectedChanged) Q_PROPERTY(bool uvcOpened READ uvcOpened NOTIFY uvcOpenedChanged) @@ -41,6 +42,7 @@ public: void initializeLogger(); int exec(); + static Application *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine); Q_INVOKABLE QStringList availableSerialPorts() const; Q_INVOKABLE QStringList availableUsbVideoCameras() const; Q_INVOKABLE bool open(const QString &portName, int baudRate); diff --git a/Analyser/ImageDecoder.cpp b/Analyser/ImageDecoder.cpp index ef4e269..79bd97c 100644 --- a/Analyser/ImageDecoder.cpp +++ b/Analyser/ImageDecoder.cpp @@ -10,31 +10,50 @@ std::optional ImageDecoder::extractJpegYComponent(const std jpeg_create_decompress(&cinfo); FILE *infile = fopen(filename.c_str(), "rb"); - if (infile == NULL) { + if (infile == nullptr) { LOG(error) << "cannot open " << filename; return std::nullopt; } ImageDecoder::Image ret; jpeg_stdio_src(&cinfo, infile); - jpeg_read_header(&cinfo, TRUE); - cinfo.out_color_space = JCS_YCbCr; // We want YCbCr color space - jpeg_start_decompress(&cinfo); + jpeg_read_header(&cinfo, TRUE); // 1 + LOG(info) << "jpeg color space: " << cinfo.jpeg_color_space; + if (cinfo.jpeg_color_space == JCS_YCbCr) { + cinfo.out_color_space = JCS_YCbCr; // We want YCbCr color space + jpeg_start_decompress(&cinfo); - int row_stride = cinfo.output_width * cinfo.output_components; - JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr)&cinfo, JPOOL_IMAGE, row_stride, 1); + int row_stride = cinfo.output_width * cinfo.output_components; + JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr)&cinfo, JPOOL_IMAGE, row_stride, 1); - ret.width = cinfo.output_width; - ret.height = cinfo.output_height; - ret.data.resize(ret.width * ret.height); - while (cinfo.output_scanline < cinfo.output_height) { - jpeg_read_scanlines(&cinfo, buffer, 1); - for (unsigned int i = 0; i < cinfo.output_width; ++i) { - ret.data[(cinfo.output_scanline - 1) * cinfo.output_width + i] = - buffer[0][i * 3]; // Y component is the first byte in YCbCr + ret.width = cinfo.output_width; + ret.height = cinfo.output_height; + ret.data.resize(ret.width * ret.height); + while (cinfo.output_scanline < cinfo.output_height) { + jpeg_read_scanlines(&cinfo, buffer, 1); + for (unsigned int i = 0; i < cinfo.output_width; ++i) { + ret.data[(cinfo.output_scanline - 1) * cinfo.output_width + i] = + buffer[0][i * 3]; // Y component is the first byte in YCbCr + } } + + jpeg_finish_decompress(&cinfo); + } else if (cinfo.jpeg_color_space == JCS_GRAYSCALE) { + cinfo.out_color_space = JCS_GRAYSCALE; // We want grayscale color space + jpeg_start_decompress(&cinfo); + int row_stride = cinfo.output_width * cinfo.output_components; + JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr)&cinfo, JPOOL_IMAGE, row_stride, 1); + + ret.width = cinfo.output_width; + ret.height = cinfo.output_height; + ret.data.resize(ret.width * ret.height); + while (cinfo.output_scanline < cinfo.output_height) { + (void)jpeg_read_scanlines(&cinfo, buffer, 1); + memcpy(ret.data.data() + (cinfo.output_scanline - 1) * row_stride, buffer[0], row_stride); + } + } else { + LOG(warning) << "jpeg color space(" << cinfo.jpeg_color_space << ") not supported."; } - jpeg_finish_decompress(&cinfo); jpeg_destroy_decompress(&cinfo); fclose(infile); return std::make_optional(ret); diff --git a/Analyser/ModuleCommunication.cpp b/Analyser/ModuleCommunication.cpp index 1766c65..a291d58 100644 --- a/Analyser/ModuleCommunication.cpp +++ b/Analyser/ModuleCommunication.cpp @@ -80,13 +80,13 @@ void ModuleCommunication::enroll(const std::string &username, bool persistence, } void ModuleCommunication::enrollExtended(const std::string &username, bool persistence, uint8_t timeout) { - EnrollData data = {0}; + EnrollData data = {}; data.timeout = timeout; data.skipSave = persistence ? 0 : 1; strncpy(reinterpret_cast(data.username), username.c_str(), sizeof(data.username)); auto [frameData, frameSize] = generateFrame(EnrollExtended, reinterpret_cast(&data), sizeof(data)); m_serialPort->write(reinterpret_cast(frameData), frameSize); - + setCurrentMessageIdStatus(EnrollExtended); LOG_CAT(info, GUI) << "发送获取注册照片指令: " << protocolDataFormatString(frameData, frameSize); LOG_CAT(info, GUI) << "用户名: " << username << ", 超时时间: " << static_cast(timeout) << "s"; LOG_CAT(info, GUI) << Separator; diff --git a/Analyser/ModuleCommunication.h b/Analyser/ModuleCommunication.h index 27eb647..1e84c87 100644 --- a/Analyser/ModuleCommunication.h +++ b/Analyser/ModuleCommunication.h @@ -10,6 +10,7 @@ class ModuleCommunication : public QObject { Q_OBJECT QML_ELEMENT + QML_UNCREATABLE("Only created in C++...") static constexpr uint32_t UsernameSize = 32; static constexpr uint32_t VersionSize = 32; static constexpr const char *Separator = "----------"; diff --git a/Analyser/qml/OperationItem.qml b/Analyser/qml/OperationItem.qml index 2e2c592..3c4793b 100644 --- a/Analyser/qml/OperationItem.qml +++ b/Analyser/qml/OperationItem.qml @@ -64,7 +64,7 @@ Item { } Button { - property bool enrolling: App.module ? (App.module.currentMessageId === 0x1d) || (App.module.currentMessageId === 0x1e) : false + property bool enrolling: App.module ? (App.module.currentMessageId === ModuleCommunication.EnrollSingle) || (App.module.currentMessageId === ModuleCommunication.EnrollExtended) : false text: enrolling ? "取消" : "注册" onClicked: { if (enrolling) { diff --git a/Analyser/qml/main.qml b/Analyser/qml/main.qml index e89a4c3..34e8e2b 100644 --- a/Analyser/qml/main.qml +++ b/Analyser/qml/main.qml @@ -115,7 +115,6 @@ Window { } else if (level === 2) { resultBrowser.append(tip) } - console.log(level, Application.Info) } function onNewVideoFrame() { image.source = ""