本文旨在介绍如何使用易语言编写对比图片是否一致的程序。
一、读取图片
要想对比两张图片是否一致,首先需要将这两张图片读取进来。易语言中可以使用GDI+来读取图片。
// 导入GDI+模块 #include "gdiplus.e" // 读取图片 image1 = Gdip_CreateBitmapFromFile("image1.jpg") image2 = Gdip_CreateBitmapFromFile("image2.jpg")
二、获取图片大小
得到两张图片后,需要先判断它们是否大小一致。使用GetImageWidth和GetImageHeight可以很容易地获取图片的宽度和高度。
// 获取图片宽度和高度 w1 = Gdip_GetImageWidth(image1) h1 = Gdip_GetImageHeight(image1) w2 = Gdip_GetImageWidth(image2) h2 = Gdip_GetImageHeight(image2)
三、像素点对比
得到图片的大小后,接下来需要对比两张图片的每一个像素点是否一致。可以使用Gdip_BitmapLockBits和Gdip_BitmapUnlockBits函数来锁定位图的数据。再使用指针遍历位图的像素点进行对比。
// 锁定位图数据 Gdip_BitmapLockBits(image1, rect, GDIP_IMAGELOCKMODE_READ, pixelFormat, &bitmapData1) Gdip_BitmapLockBits(image2, rect, GDIP_IMAGELOCKMODE_READ, pixelFormat, &bitmapData2) ptr1 = bitmapData1.Scan0 ptr2 = bitmapData2.Scan0 // 遍历像素点 for(i = 0; i < h1; i++) { for(j = 0; j < w1; j++) { // 获取像素点信息 pixel1 = *(ptr1 + i * bitmapData1.Stride + j * bytesPerPixel) pixel2 = *(ptr2 + i * bitmapData2.Stride + j * bytesPerPixel) // 判断像素点是否一致 if(pixel1 != pixel2) { // 不一致,处理方法 } } } // 解锁位图数据 Gdip_BitmapUnlockBits(image1, &bitmapData1) Gdip_BitmapUnlockBits(image2, &bitmapData2)
四、处理方法
当发现两张图片的像素点不一致时,可以根据需求进行不同的处理方法。如果只需要判断它们是不是完全一样,那么只需要在像素点不一致的时候退出循环并给出结果即可。如果需要找到不同的像素点,可以记录下坐标并且绘制出来。
// 找到不同的像素点 if(pixel1 != pixel2) { // 记录坐标 diff_x = j diff_y= i // 处理方法 // 绘制不同像素点的框 Gdip_DrawRectangle(graphics, pen, diff_x, diff_y, 1, 1) }
五、完整代码示例
#include "gdiplus.e" func main() // 初始化GDI+ Gdiplus_Startup() // 读取图片 image1 = Gdip_CreateBitmapFromFile("image1.jpg") image2 = Gdip_CreateBitmapFromFile("image2.jpg") // 获取图片宽度和高度 w1 = Gdip_GetImageWidth(image1) h1 = Gdip_GetImageHeight(image1) w2 = Gdip_GetImageWidth(image2) h2 = Gdip_GetImageHeight(image2) // 锁定位图数据 rect = Gdip_RectF(0, 0, w1, h1) pixelFormat = GDIP_PIXEFORMAT_32BPP_ARGB Gdip_BitmapLockBits(image1, rect, GDIP_IMAGELOCKMODE_READ, pixelFormat, &bitmapData1) Gdip_BitmapLockBits(image2, rect, GDIP_IMAGELOCKMODE_READ, pixelFormat, &bitmapData2) ptr1 = bitmapData1.Scan0 ptr2 = bitmapData2.Scan0 bytesPerPixel = 4 // 初始化画笔 penColor = RGB(255, 0, 0) penWidth = 1 pen = Gdip_CreatePen1(penColor, penWidth) // 创建用于绘制不同像素点的图形 graphics = Gdip_CreateFromHDC(GetClientDC(hwnd)) Gdip_SetSmoothingMode(graphics, GDIP_SMOOTHINGMODE_ANTIALIAS) // 遍历像素点 for(i = 0; i < h1; i++) { for(j = 0; j < w1; j++) { // 获取像素点信息 pixel1 = *(ptr1 + i * bitmapData1.Stride + j * bytesPerPixel) pixel2 = *(ptr2 + i * bitmapData2.Stride + j * bytesPerPixel) // 判断像素点是否一致 if(pixel1 != pixel2) { // 记录坐标 diff_x = j diff_y= i // 处理方法 // 绘制不同像素点的框 Gdip_DrawRectangle(graphics, pen, diff_x, diff_y, 1, 1) } } } // 解锁位图数据 Gdip_BitmapUnlockBits(image1, &bitmapData1) Gdip_BitmapUnlockBits(image2, &bitmapData2) // 显示处理结果 if(diff_x == -1 && diff_y == -1) { MessageBox("两张图片完全一致。", "提示") } else { MessageBox("两张图片不完全一致,已经标记不同的像素点。", "提示") } // 释放资源 ReleaseDC(hwnd, graphics) Gdip_DeleteGraphics(graphics) Gdip_DeletePen(pen) Gdip_DisposeImage(image1) Gdip_DisposeImage(image2) // 关闭GDI+ Gdiplus_Shutdown() return 0 endfunc