概念澄清 CxImage 是用于图像加载、处理与格式转换的 C++ 类库,并非系统监控工具。如果你的诉求是“在 CentOS 上做图像相关的监控(如:监控目录图片变化、对抓屏/摄像头帧做处理告警)”,通常做法是把 CxImage 集成到你的监控程序或服务中,再配合文件/进程监控手段来实现。CxImage 本身支持 BMP、JPEG、PNG、TIFF、GIF 等多种格式,能做缩放、旋转、滤镜、亮度/对比度调整等常见处理。
在 CentOS 上准备 CxImage 开发环境
用 CxImage 做一个“目录图片变化监控+处理”的最小示例
#include <iostream>
#include <string>
#include <sys/inotify.h>
#include <unistd.h>
#include <cstring>
#include <thread>
#include <chrono>
#include "ximage.h"
static const int EVENT_SIZE = sizeof(struct inotify_event);
static const int BUF_LEN = 1024 * (EVENT_SIZE + 16);
void handle_image(const std::string& file) {
CxImage image;
// 自动探测格式加载
if (!image.Load(file.c_str(), CXIMAGE_FORMAT_UNKNOWN)) {
std::cerr << "Load failed: " << file << std::endl;
return;
}
// 示例处理:缩放 + 灰度
image.Resample(800, 600);
image.GrayScale();
std::string out = file.substr(0, file.find_last_of('.')) + "_proc.png";
if (!image.Save(out.c_str(), CXIMAGE_FORMAT_PNG)) {
std::cerr << "Save failed: " << out << std::endl;
return;
}
std::cout << "Processed: " << out << std::endl;
}
int main(int argc, char* argv[]) {
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " <watch_dir>" << std::endl;
return 1;
}
std::string path = argv[1];
if (path.back() != '/') path += '/';
int fd = inotify_init1(IN_NONBLOCK);
if (fd < 0) { perror("inotify_init1"); return 1; }
int wd = inotify_add_watch(fd, path.c_str(),
IN_CREATE | IN_MOVED_TO | IN_MODIFY);
if (wd < 0) { perror("inotify_add_watch"); close(fd); return 1; }
char buffer[BUF_LEN];
while (true) {
ssize_t len = read(fd, buffer, BUF_LEN);
if (len < 0) {
if (errno == EAGAIN) { std::this_thread::sleep_for(std::chrono::milliseconds(100)); continue; }
perror("read"); break;
}
for (char* p = buffer; p < buffer + len; ) {
struct inotify_event* event = (struct inotify_event*)p;
if (event->len && (event->mask & (IN_CREATE | IN_MOVED_TO | IN_MODIFY))) {
std::string file = path + event->name;
// 简单按后缀过滤
size_t dot = file.find_last_of('.');
if (dot != std::string::npos) {
std::string ext = file.substr(dot + 1);
std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
if (ext == "jpg" || ext == "jpeg" || ext == "png" || ext == "tif" || ext == "tiff" || ext == "bmp" || ext == "gif") {
std::thread(handle_image, file).detach();
}
}
}
p += EVENT_SIZE + event->len;
}
}
inotify_rm_watch(fd, wd);
close(fd);
return 0;
}
说明:CxImage 提供 Load/Save/Resample/GrayScale 等常用接口;inotify 用于高效监听目录事件,两者组合即可实现“监控到图像就自动处理”的能力。常见坑与排查
如果你要找的是系统监控工具