correct code.

This commit is contained in:
luocai 2024-08-31 11:37:20 +08:00
parent 5e24a831ae
commit c7c24e9388
7 changed files with 54 additions and 23 deletions

View File

@ -43,8 +43,6 @@ Application::Application(int &argc, char **argv) : m_app(std::make_shared<QAppli
});
m_videoFrameProvider = new VideoFrameProvider();
qmlRegisterSingletonInstance("Analyser", 1, 0, "App", this);
if (!std::filesystem::exists(JpgPath)) {
std::filesystem::create_directory(JpgPath);
}
@ -91,6 +89,18 @@ int Application::exec() {
return m_app->exec();
}
Application *Application::create(QQmlEngine *qmlEngine, QJSEngine *jsEngine) {
Application *ret = nullptr;
auto app = Amass::Singleton<Application>::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<uint8_t> &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);
}
}

View File

@ -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);

View File

@ -10,13 +10,15 @@ std::optional<ImageDecoder::Image> 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);
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);
@ -35,6 +37,23 @@ std::optional<ImageDecoder::Image> ImageDecoder::extractJpegYComponent(const std
}
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_destroy_decompress(&cinfo);
fclose(infile);
return std::make_optional(ret);

View File

@ -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<char *>(data.username), username.c_str(), sizeof(data.username));
auto [frameData, frameSize] = generateFrame(EnrollExtended, reinterpret_cast<const uint8_t *>(&data), sizeof(data));
m_serialPort->write(reinterpret_cast<const char *>(frameData), frameSize);
setCurrentMessageIdStatus(EnrollExtended);
LOG_CAT(info, GUI) << "发送获取注册照片指令: " << protocolDataFormatString(frameData, frameSize);
LOG_CAT(info, GUI) << "用户名: " << username << ", 超时时间: " << static_cast<int>(timeout) << "s";
LOG_CAT(info, GUI) << Separator;

View File

@ -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 = "----------";

View File

@ -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) {

View File

@ -115,7 +115,6 @@ Window {
} else if (level === 2) {
resultBrowser.append(tip)
}
console.log(level, Application.Info)
}
function onNewVideoFrame() {
image.source = ""