拼接图片 - ConcatImage
函数简介
将两张图片拼接成一张新图片。此函数可以将两张图片按照指定的方向(水平或垂直)拼接在一起,生成一张新的图片。常用于图片合成、长图制作、图片对比等场景。支持不同尺寸图片的拼接,可以自动调整对齐方式。
接口名称
ConcatImage
DLL调用
long ConcatImage(long ola, long image_ptr1, long image_ptr2, int gap, string color, int direction)
参数定义:
ola
(长整型数): OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。image_ptr1
(长整型数): 第一张图片的句柄。image_ptr2
(长整型数): 第二张图片的句柄。gap
(整型数): 两张图片之间的间隙color
(字符串型) : 填充两张图片之间的间隙的颜色direction
(整型数): 拼接方向:- 0: 水平拼接(左右)
- 1: 垂直拼接(上下)
示例:
// 加载两张图片
long image1 = LoadImage(ola, "D:\\test\\left.png");
long image2 = LoadImage(ola, "D:\\test\\right.png");
if (image1 != 0 && image2 != 0) {
// 水平拼接(左右)
long horizontal = ConcatImage(ola, image1, image2, 3, "#FFFFFF", 0);
if (horizontal != 0) {
SaveImageFromPtr(ola, horizontal, "D:\\test\\horizontal.png");
printf("水平拼接完成\n");
FreeImagePtr(ola, horizontal);
}
// 垂直拼接(上下)
long vertical = ConcatImage(ola, image1, image2, 3, "#FFFFFF", 1);
if (vertical != 0) {
SaveImageFromPtr(ola, vertical, "D:\\test\\vertical.png");
printf("垂直拼接完成\n");
FreeImagePtr(ola, vertical);
}
// 释放原图内存
FreeImagePtr(ola, image1);
FreeImagePtr(ola, image2);
}
// 多图片拼接示例(制作长图)
void CreateLongImage(const char** image_paths, int count, const char* output_path) {
if (count < 2) return;
// 加载第一张图片
long result = LoadImage(ola, image_paths[0]);
if (result == 0) return;
// 依次拼接其他图片
for (int i = 1; i < count; i++) {
long next = LoadImage(ola, image_paths[i]);
if (next != 0) {
long temp = ConcatImage(ola, result, next, 1); // 垂直拼接
FreeImagePtr(ola, result);
FreeImagePtr(ola, next);
result = temp;
}
}
// 保存结果
if (result != 0) {
SaveImageFromPtr(ola, result, output_path);
FreeImagePtr(ola, result);
}
}
// 制作图片对比(左右对比)
void CreateComparisonImage(const char* before_path, const char* after_path, const char* output_path) {
long before = LoadImage(ola, before_path);
long after = LoadImage(ola, after_path);
if (before != 0 && after != 0) {
// 确保两张图片高度相同
int width1, height1, width2, height2;
GetImageSize(ola, before, &width1, &height1);
GetImageSize(ola, after, &width2, &height2);
if (height1 != height2) {
// 调整高度
int target_height = height1 < height2 ? height1 : height2;
long resized_before = ReSize(ola, before, width1 * target_height / height1, target_height);
long resized_after = ReSize(ola, after, width2 * target_height / height2, target_height);
if (resized_before != 0 && resized_after != 0) {
// 水平拼接调整后的图片
long comparison = ConcatImage(ola, resized_before, resized_after, 0);
if (comparison != 0) {
SaveImageFromPtr(ola, comparison, output_path);
FreeImagePtr(ola, comparison);
}
FreeImagePtr(ola, resized_before);
FreeImagePtr(ola, resized_after);
}
} else {
// 直接水平拼接
long comparison = ConcatImage(ola, before, after, 0);
if (comparison != 0) {
SaveImageFromPtr(ola, comparison, output_path);
FreeImagePtr(ola, comparison);
}
}
FreeImagePtr(ola, before);
FreeImagePtr(ola, after);
}
}
返回值
长整型数:
- 0: 拼接失败
- 非0: 拼接成功,返回新图片的句柄
注意事项
- 水平拼接时,两张图片的高度最好相同,否则会以较小的高度为准
- 垂直拼接时,两张图片的宽度最好相同,否则会以较小的宽度为准
- 拼接后会返回新的图片句柄,原图片不会被修改
- 新图片句柄使用完后需要调用 FreeImagePtr 释放内存
- 如果需要在拼接前调整图片大小,可以配合使用 ReSize 或 ScalePixels 函数