在Ubuntu上使用CXImage处理图像的完整流程
在Ubuntu上使用CXImage前,需先安装库文件及其依赖项(如libjpeg、libpng等),确保编译和运行时能找到必要的组件。
sudo apt update && sudo apt upgrade -y
libcximage-dev
预编译包,可直接安装:sudo apt install libcximage-dev build-essential
若需从源码编译(如使用最新版本),需额外安装构建工具和依赖库:sudo apt install build-essential cmake libpng-dev libjpeg-dev libtiff-dev zlib1g-dev
若官方仓库的版本不满足需求,可从GitHub克隆源码编译:
git clone https://github.com/cximage/cximage.git
cd cximage
mkdir build && cd build
cmake ..
make -j$(nproc) # 使用多核加速编译
sudo make install
安装完成后,CXImage的头文件会存放在/usr/local/include
,库文件在/usr/local/lib
。在C++项目中使用CXImage时,需告知编译器头文件和库文件的路径。以简单的图像加载/保存程序为例:
mkdir cximage_demo && cd cximage_demo
main.cpp
):#include <cximage.h>
#include <iostream>
int main() {
// 创建CXImage对象并加载图像
CXImage image;
if (!image.Load("input.jpg")) { // 替换为你的图像路径
std::cerr << "Failed to load image!" << std::endl;
return 1;
}
// 输出图像信息
std::cout << "Loaded image: " << image.GetWidth() << "x" << image.GetHeight()
<< ", Format: " << image.GetFormatName() << std::endl;
// 转换为PNG格式并保存
if (!image.Save("output.png", CXIMAGE_FORMAT_PNG)) {
std::cerr << "Failed to save image!" << std::endl;
return 1;
}
std::cout << "Image saved successfully!" << std::endl;
return 0;
}
g++
编译时,需链接CXImage库及依赖(-lcximage
):g++ main.cpp -o cximage_demo -I/usr/local/include -L/usr/local/lib -lcximage -lpng -ljpeg -ltiff -lz
说明:-I
指定头文件路径,-L
指定库文件路径,-lcximage
链接CXImage库,后续为依赖库(如libpng、libjpeg等)。CXImage提供了丰富的图像处理功能,以下是常见操作的代码示例:
CXImage image;
if (image.Load("input.jpg")) {
image.Save("output.png", CXIMAGE_FORMAT_PNG);
}
std::cout << "Width: " << image.GetWidth()
<< ", Height: " << image.GetHeight()
<< ", Bits per pixel: " << image.GetBpp() << std::endl;
if (image.IsValid()) {
image.Resample(image.GetWidth() / 2, image.GetHeight() / 2, CXIMAGE_FILTER_BOX);
}
if (image.IsValid()) {
image.Rotate90(true); // true表示顺时针
}
cximage.h: No such file or directory
”,需检查-I
路径是否正确(默认为/usr/local/include
);若报错“cannot find -lcximage
”,需检查-L
路径及库文件是否存在(/usr/local/lib/libcximage.so
)。undefined reference to png_create_read_struct
”,需安装libpng开发包(sudo apt install libpng-dev
)并重新编译。Permission denied
”,需确保目标目录有写入权限(如使用chmod
添加权限)。通过以上步骤,即可在Ubuntu上完成CXImage的安装、配置及基本图像处理任务。CXImage支持JPEG、PNG、BMP等多种格式,能满足常见的图像加载、保存、格式转换、缩放、旋转等需求。