From 3b2abce8337f583112e31bdbd4da466fc0815351 Mon Sep 17 00:00:00 2001 From: ocsin1 <2719912597@qq.com> Date: Mon, 13 Jul 2026 11:42:22 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20cv::imencode=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E4=BF=9D=E6=8A=A4=EF=BC=8C=E9=98=B2=E6=AD=A2?= =?UTF-8?q?=E8=A7=A6=E5=8F=91=20save=5Fon=5Ferror=20=E6=97=B6=E5=B4=A9?= =?UTF-8?q?=E6=BA=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/MaaUtils/ImageIo.h | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/include/MaaUtils/ImageIo.h b/include/MaaUtils/ImageIo.h index 5268848..1599da8 100644 --- a/include/MaaUtils/ImageIo.h +++ b/include/MaaUtils/ImageIo.h @@ -8,6 +8,7 @@ #include "MaaUtils/NoWarningCV.hpp" #include "MaaUtils/File.hpp" +#include "MaaUtils/Logger.h" #include "MaaUtils/Platform.h" MAA_NS_BEGIN @@ -37,7 +38,24 @@ inline bool imwrite(const std::filesystem::path& path, cv::InputArray img, const } auto ext = path_to_utf8_string(path.extension()); std::vector encoded; - if (!cv::imencode(ext, img, encoded, params)) { + bool ok = false; + try { + ok = cv::imencode(ext, img, encoded, params); + } + catch (const cv::Exception& e) { + LogError << "cv::imencode exception: code=" << e.code << " msg=" << e.what() << " func=" << e.func + << " file=" << e.file << " line=" << e.line << " ext=" << ext; + return false; + } + catch (const std::exception& e) { + LogError << "cv::imencode std::exception: " << e.what(); + return false; + } + catch (...) { + LogError << "cv::imencode unknown exception"; + return false; + } + if (!ok) { return false; } std::ofstream of(path, std::ios::out | std::ios::binary);