Skip to content

图色方法

函数列表

1. 创建QImage图形处理对象 (QImage.new)

说明: 创建一个QImage图形处理对象

函数: QImage.new()

参数: 无

返回值:

返回值类型说明
userdata返回一个QImage的userdata对象

示例:

lua
local image = QImage.new()
if image ~= nil then
	print("创建QImage对象成功")
else
	print("创建QImage对象失败")
end

2. 创建一个空白QImage画布 (QImage.create)

说明: 创建一个空白QImage画布

函数: image:create(width,height,argb)

参数:

参数名类型说明
width整数画布的宽度
height整数画布的高度
argb整数画布的颜色例如:0xffffffff (白色)

返回值:

返回值类型说明
boolean布尔类型

示例:

lua
local img = QImage.new()
local ret = img:create(500,500,0xffffffff)
local savepath = getWorkPath() .. "/white.png"
local savepath1 = getWorkPath() .. "/yellow.png"
if ret then
	img:save(savepath)
	print("创建白色画布完成,保存路径:",savepath)
end

ret = img:create(100,100,0xffffff00)
if ret then
	img:save(savepath1)
	print("创建黄色黄布完成,保存路径:",savepath1)
end

3. 截取系统屏幕到QImage对象中 (QImage.snapShot)

说明: 截取系统屏幕到QImage对象中

函数: image:snapShot(left, top, right, bottom)

参数:

参数名类型说明
left整数截图区域左边界坐标
top整数截图区域上边界坐标
right整数截图区域右边界坐标
bottom整数截图区域下边界坐标

说明: 如果left top right bottom都为0或者不填写就表示全屏截图

返回值:

返回值类型说明
boolean返回一个布尔类型,true表示截图成功,false表示截图失败

示例:

lua
local image = QImage.new()
if image ~= nil then
    local result = image:snapShot(0, 0, 100, 100)
    if result then
        print("截图成功")
    else
        print("截图失败")
    end
else
    print("创建QImage对象失败")
end

4. 从本地图片文件加载到QImage对象中 (QImage.load)

说明: 从本地图片文件加载到QImage对象中

函数: image:load(filepath)

参数:

参数名类型说明
filepath字符串图片文件的路径字符串

返回值:

返回值类型说明
boolean返回一个布尔类型,true表示加载成功,false表示加载失败

示例:

lua
local image = QImage.new()
local filepath = getWorkPath() .. "/test.png"
if image ~= nil then
    local result = image:load(filepath)
    if result then
        print("图片加载成功")
    else
        print("图片加载失败,请检查文件路径")
    end
else
    print("创建QImage对象失败")
end

5. 将QImage对象保存到本地图片文件 (QImage.save)

说明: 将QImage对象保存到本地图片文件

函数: image:save(filepath)

参数:

参数名类型说明
filepath字符串图片文件的保存路径字符串

返回值:

返回值类型说明
boolean返回一个布尔类型,true表示保存成功,false表示保存失败

示例:

lua
local image = QImage.new()
local filepath = getWorkPath() .. "/test.png"
if image ~= nil then
    local snapResult = image:snapShot(0, 0, 100, 100)
    if snapResult then
        local saveResult = image:save(filepath)
        if saveResult then
            print("图片保存成功")
        else
            print("图片保存失败,请检查路径和权限")
        end
    else
        print("截图失败")
    end
else
    print("创建QImage对象失败")
end

6. 获取QImage对象的宽度 (QImage.width)

说明: 获取QImage对象的宽度

函数: image:width()

参数: 无

返回值:

返回值类型说明
整数返回一个整数,表示图像的宽度(像素)

示例:

lua
local image = QImage.new()
if image ~= nil then
    local result = image:snapShot(0,0,200,200)
    if result then
        local width = image:width()
        print("图片宽度:" .. width .. "像素")
    else
        print("图片加载失败")
    end
else
    print("创建QImage对象失败")
end

7. 获取QImage对象的高度 (QImage.height)

说明: 获取QImage对象的高度

函数: image:height()

参数: 无

返回值:

返回值类型说明
整数返回一个整数,表示图像的高度(像素)

示例:

lua
local image = QImage.new()
if image ~= nil then
    local result = image:snapShot(0, 0, 100, 100)
    if result then
        local height = image:height()
        print("图片高度:" .. height .. "像素")
        
        local width = image:width()
        print("图片尺寸:" .. width .. "x" .. height)
    else
        print("截图失败")
    end
else
    print("创建QImage对象失败")
end

8. 对图像进行二值化处理 (QImage.binarize)

说明: 对图像进行二值化处理

函数: image:binarize(threshold, maxValue, method)

参数:

参数名类型说明
threshold整数阈值,用于区分前景和背景的灰度值界限
maxValue整数最大值,当像素值超过阈值时被设置的值
method整数二值化方法,可选值:0: BINARY - 标准二值化;1: BINARY_INV - 反二值化;2: OTSU - 大津算法;3: ADAPTIVE_MEAN - 自适应均值阈值;4: ADAPTIVE_GAUSSIAN - 自适应高斯阈值

返回值:

返回值类型说明
boolean返回一个布尔类型,true表示二值化成功,false表示失败

示例:

lua
local image = QImage.new()
local filepath = getWorkPath() .. "/test.png"
if image ~= nil then
    local result = image:snapShot(0,0,200,200)
    if result then
        local binarizeResult = image:binarize(128, 255, 0)
        if binarizeResult then
            print("二值化处理成功")
            image:save(filepath)
        else
            print("二值化处理失败")
        end
    else
        print("图片加载失败")
    end
else
    print("创建QImage对象失败")
end

9. 缩放图像尺寸 (QImage.scale)

说明: 缩放图像尺寸

函数:

按比例缩放: image:scale(scaleFactor)
按尺寸缩放: image:scale(width, height)

参数:

参数名类型说明
scaleFactor浮点数缩放比例,如0.5表示缩小一半,2.0表示放大两倍
width整数目标宽度
height整数目标高度

返回值:

返回值类型说明
boolean返回一个布尔类型,true表示缩放成功,false表示失败

示例:

lua
local image = QImage.new()
local scaled_half = getWorkPath() .. "/scaled_half.png"
local scaled_fixed = getWorkPath() .. "/scaled_fixed.png"
if image ~= nil then
    if image:snapShot(0,0,200,200) then
        local result1 = image:scale(0.5)
        if result1 then
            print("按比例缩放成功")
            image:save(scaled_half)
        end
        
        image:snapShot(0,0,200,200)
        
        local result2 = image:scale(200, 150)
        if result2 then
            print("按尺寸缩放成功")
            local newWidth = image:width()
            local newHeight = image:height()
            print("新尺寸:" .. newWidth .. "x" .. newHeight)
            image:save(scaled_fixed)
        end
    else
        print("图片加载失败")
    end
else
    print("创建QImage对象失败")
end

10. 旋转图像 (QImage.rotate)

说明: 旋转图像

函数: image:rotate(angle)

参数:

参数名类型说明
angle浮点数旋转角度,正数表示逆时针旋转,负数表示顺时针旋转

返回值:

返回值类型说明
boolean返回一个布尔类型,true表示旋转成功,false表示失败

示例:

lua
local image = QImage.new()
local rotated_90 = getWorkPath() .. "/rotated_90.png"
local rotated_45 = getWorkPath() .. "/rotated_45.png"
if image ~= nil then
    if image:snapShot() then
        local result = image:rotate(90)
        if result then
            print("图像旋转成功")
            image:save(rotated_90)
        else
            print("图像旋转失败")
        end
        
        image:snapShot()
        if image:rotate(45) then
            print("45度旋转成功")
            image:save(rotated_45)
        end
    else
        print("图片加载失败")
    end
else
    print("创建QImage对象失败")
end

11. 裁剪图像区域 (QImage.crop)

说明: 裁剪图像区域

函数: image:crop(x, y, width, height)

参数:

参数名类型说明
x整数裁剪区域左上角X坐标
y整数裁剪区域左上角Y坐标
width整数裁剪区域宽度
height整数裁剪区域高度

返回值:

返回值类型说明
userdata返回一个新的QImage对象,包含裁剪后的图像,如果裁剪失败返回nil

示例:

lua
local image = QImage.new()
local cropped_image = getWorkPath() .. "/cropped_image.png"
local cropped_binary = getWorkPath() .. "/cropped_binary.png"
if image ~= nil then
    if image:snapShot() then
        local originalWidth = image:width()
        local originalHeight = image:height()
        print("原图尺寸:" .. originalWidth .. "x" .. originalHeight)
        
        local cropX = 50
        local cropY = 50
        local cropWidth = 100
        local cropHeight = 100
        
        local croppedImage = image:crop(cropX, cropY, cropWidth, cropHeight)
        if croppedImage ~= nil then
            print("图像裁剪成功")
            local newWidth = croppedImage:width()
            local newHeight = croppedImage:height()
            print("裁剪后尺寸:" .. newWidth .. "x" .. newHeight)
            
            croppedImage:save(cropped_image)
            
            if croppedImage:binarize(128, 255, 0) then
                croppedImage:save(cropped_binary)
            end
        else
            print("图像裁剪失败,请检查裁剪参数")
        end
    else
        print("图片加载失败")
    end
else
    print("创建QImage对象失败")
end

12. 绘制一条直线 (QImage.drawLine)

说明: 绘制一条直线

函数: image:drawLine(x0, y0, x1,y1,argb,thickness)

参数:

参数名类型说明
x0整数起始点X坐标
y0整数起始点Y坐标
x1整数终点X坐标
y1整数终点Y坐标
argb整数表示用什么颜色绘制例如0xffff0000 (红色)
thickness整数线条的粗细(厚度)

返回值:

返回值类型说明
boolean返回一个布尔类型

示例:

lua
local img = QImage.new()
local ret = img:snapShot()
local savepath = getWorkPath() .. "/save.png"
if ret then
	img:drawLine(100,100,300,300,0xffff0000,30)
	img:save(savepath)
	print("绘制完成保存路径:",savepath)
end

13. 绘制矩形框 (QImage.drawRect)

说明: 绘制矩形框

函数: image:drawRect(x0, y0, x1,y1,argb,thickness)

参数:

参数名类型说明
x0整数起始点X坐标
y0整数起始点Y坐标
x1整数终点X坐标
y1整数终点Y坐标
argb整数表示用什么颜色绘制例如0xffff0000 (红色)
thickness整数线条的粗细(厚度),-1 表示填充

返回值:

返回值类型说明
boolean返回一个布尔类型

示例:

lua
local img = QImage.new()
local ret = img:snapShot()
local savepath = getWorkPath() .. "/save.png"
if ret then
	img:drawRect(100,100,300,300,0xffff0000,5)
	img:save(savepath)
	print("绘制完成保存路径:",savepath)
end

14. 绘制圆形 (QImage.drawCircle)

说明: 绘制圆形

函数: image:drawCircle(x, y, radius,argb,thickness)

参数:

参数名类型说明
x整数圆心X坐标
y整数圆心Y坐标
radius整数圆的半径
argb整数表示用什么颜色绘制例如0xffff0000 (红色)
thickness整数线条的粗细(厚度),-1 表示填充

返回值:

返回值类型说明
boolean返回一个布尔类型

示例:

lua
local img = QImage.new()
local ret = img:create(500,500,0xffffffff)
local savepath = getWorkPath() .. "/save.png"
if ret then
	img:drawCircle(250,250,100,0xffff0000,5)
	img:save(savepath)
	print("绘制完成保存路径:",savepath)
end

15. 填充一个矩形区域 (QImage.fillRect)

说明: 填充一个矩形区域

函数: QImage.fillRect(x0, y0, x1,y1,argb)

参数:

参数名类型说明
x0整数起始点X坐标
y0整数起始点Y坐标
x1整数终点X坐标
y1整数终点Y坐标
argb整数表示用什么颜色绘制例如0xffff0000 (红色)

返回值:

返回值类型说明
boolean返回一个布尔类型

示例:

lua
local img = QImage.new()
local ret = QImage.fillRect(500,500,0xffffffff)
local savepath = getWorkPath() .. "/save.png"
if ret then
	QImage:fillRect(100,100,300,300,0xffff0000)
	QImage:save(savepath)
	print("创建白色画布完成,保存路径:",savepath)
end

16. 绘制多边形 (QImage.drawPolygon)

说明: 绘制多边形

函数: image:drawPolygon(points, argb, thickness,isclosed)

参数:

参数名类型说明
points数组多边形顶点坐标数组,格式为{{x1,y1}, {x2,y2}, ...}
argb整数表示用什么颜色绘制例如0xffff0000 (红色)
thickness整数线条厚度,如果为-1则表示填充多边形
isclosed布尔这个多边形是否闭合

返回值:

返回值类型说明
boolean返回一个布尔类型,表示绘制是否成功

示例:

lua
local img = QImage.new()
local ret = img:create(500,500,0xffffffff)
local savepath = getWorkPath() .. "/polygon.png"
if ret then
    local points = {
        {250, 100},
        {400, 200},
        {350, 350},
        {150, 350},
        {100, 200}
    }
    
    img:drawPolygon(points, 0xffff0000, 3,true)
    
    local trianglePoints = {
        {250, 150},
        {200, 250},
        {300, 250}
    }
    
    img:drawPolygon(trianglePoints, 0xff0000ff, -1,true)
    img:save(savepath)
    print("绘制多边形完成,保存路径:",savepath)
end

17. 区域多点找色 (findMultiColor)

说明: 区域多点找色

函数: findMultiColor([image],x1,y1,x2,y2,first_color,offset_color,dir,sim)

参数:

参数名类型说明
imageuserdataQImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图
x1整数查找区域左上X坐标
y1整数查找区域左上Y坐标
x2整数查找区域右下X坐标
y2整数查找区域右下Y坐标
first_color字符串要对比的16进制颜色,多个颜色用"
offset_color字符串偏移颜色
dir整数查找方向:0:表示从左上向右下查找;1:表示从中心往四周查找;2:表示从右下向左上查找;3:表示从左下向右上查找;4:表示从右上向左下查找
sim浮点数相似度,取值范围0-1

返回值:

返回值类型说明
整数如果成功返回对应点的坐标x,y,否则返回-1,-1

示例:

lua
local firstColor = "757575-101010"
local offsetColor = "757575-101010" , "0|2|757575-101010|0|4|757575-101010|4|5|ffffff-101010|5|52|359bed-101010|-10|82|ffffff-101010|133|437|3e9eeb-101010|133|432|3e9eeb-101010|137|432|cfcfcf-101010"
local x , y = findMultiColor(0 , 0 , 0 , 0 , firstColor , offsetColor , 0 , 0.9)
if x~= nil and y ~= nil then
	print(x , y)
end

local img = QImage.new()
if img ~= nil then
	if img:snapShot() then
		x , y = findMultiColor(img , 0 , 0 , 0 , 0 , firstColor , offsetColor , 0 , 0.9)
		if x~= nil and y ~= nil then
			print(x , y)
		end
	end
end

18. 区域多点找色 (findMultiColorT)

说明: 区域多点找色

函数: findMultiColorT([image],{x1,y1,x2,y2,first_color,offset_color,dir,sim})

参数:

参数名类型说明
imageuserdataQImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图
tb表类型,其中每一项的参数如下:x1:整数型,查找区域左上X坐标;y1:整数型,查找区域左上Y坐标;x2:整数型,查找区域右下X坐标;y2:整数型,查找区域右下Y坐标;first_color:要对比的16进制颜色,多个颜色用"

返回值:

返回值类型说明
整数如果成功返回对应点的坐标x,y,否则返回-1,-1

示例:

lua
local tb = {0,0,0,0,"359bed-101010","30|123|464646-101010|35|159|ffffff-101010|-43|306|cfcfcf-101010|-3|333|bbbbbb-101010|112|366|ffffff-101010|104|391|359bed-101010|-182|-33|ffffff-101010|-174|-15|359bed-101010",0,0.9}

local x , y = findMultiColorT(tb)
if x~= nil and y ~= nil then
	print(x , y)
end

local img = QImage.new()
if img ~= nil then
	if img:snapShot() then
		x , y = findMultiColorT(img,tb)
		if x~= nil and y ~= nil then
			print(x , y)
		end
	end
end

19. 区域多点找色匹配所有目标 (findMultiColorAll)

说明: 区域多点找色匹配所有目标

函数: findMultiColorAll([image],x1,y1,x2,y2,first_color,offset_color,dir,sim)

参数:

参数名类型说明
imageuserdataQImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图
x1整数查找区域左上X坐标
y1整数查找区域左上Y坐标
x2整数查找区域右下X坐标
y2整数查找区域右下Y坐标
first_color字符串要对比的16进制颜色,多个颜色用"
offset_color字符串偏移颜色
dir整数查找方向:0:表示从左上向右下查找;1:表示从中心往四周查找;2:表示从右下向左上查找;3:表示从左下向右上查找;4:表示从右上向左下查找
sim浮点数相似度,取值范围0-1

返回值:

返回值类型说明
如果成功返回所有满足要求的表格数组

示例:

lua
local firstColor = "757575-101010"
local offsetColor = "757575-101010" , "0|2|757575-101010|0|4|757575-101010|4|5|ffffff-101010|5|52|359bed-101010|-10|82|ffffff-101010|133|437|3e9eeb-101010|133|432|3e9eeb-101010|137|432|cfcfcf-101010"
local ret = findMultiColorAll(0 , 0 , 0 , 0 , firstColor , offsetColor , 0 , 0.9)
if ret ~= nil then
	print(ret)
end

local img = QImage.new()
if img ~= nil then
	if img:snapShot() then
		ret = findMultiColorAll(img , 0 , 0 , 0 , 0 , firstColor , offsetColor , 0 , 0.9)
		if ret~= nil then
			print(ret)
		end
	end
end

20. 区域多点找色匹配所有目标 (findMultiColorAllT)

说明: 区域多点找色匹配所有目标

函数: findMultiColorAll([image],{x1,y1,x2,y2,first_color,offset_color,dir,sim})

参数:

参数名类型说明
imageuserdataQImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图
tb表类型,其中每一项的参数如下:x1:整数型,查找区域左上X坐标;y1:整数型,查找区域左上Y坐标;x2:整数型,查找区域右下X坐标;y2:整数型,查找区域右下Y坐标;first_color:要对比的16进制颜色,多个颜色用"

返回值:

返回值类型说明
如果成功返回所有满足要求的表格数组

示例:

lua
local tb = {0,0,0,0,"262626-101010","1|3|222222-101010|-2|61|ffffff-101010|-24|90|242424-101010|-23|92|222222-101010",0,0.9}
local ret = findMultiColorAllT(tb)
if ret ~= nil then
	print(ret)
end

local img = QImage.new()
if img ~= nil then
	if img:snapShot() then
		ret = findMultiColorAllT(img ,tb)
		if ret~= nil then
			print(ret)
		end
	end
end

21. 区域单点找色 (findColor)

说明: 在指定区域内查找指定的颜色

函数: findColor([image],x1, y1, x2, y2,color,dir,sim)

参数:

参数名类型说明
imageuserdataQImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图
x1整数区域左上角x坐标
y1整数区域左上角y坐标
x2整数区域右下角x坐标
y2整数区域右下角y坐标
color字符串要对比的16进制颜色,格式为"BBGGRR"多个颜色用"
dir整数查找方向:0: 表示从左上向右下;1: 表示从中心往四周查找;2: 表示从右下向左上查找;3: 表示从左下向右上查找;4:表示从右上向左下查找
sim浮点数相似度,取值范围0-1

返回值:

返回值类型说明
整数ret:如果有多个颜色用"

示例:

lua
local index = 0-1
local x = 0-1
local y = 0-1
local color = "ffffff-101010|ffffff-101010|ffffff-101010|cfcfcf-101010"
index,x,y= findColor(0 , 0 , 0 , 0 , color , 0 , 0.9)
print(index , x , y)

local img = QImage.new()
if img ~= nil then
	if img:snapShot() then
		index,x,y = findColor(img,0 , 0 , 0 , 0 , color , 0 , 0.9)
		print(index , x , y)
	end
end

22. 区域单点找色 (findColorT)

说明: 在指定区域内查找指定的颜色

函数: findColorT([image],tb)

参数:

参数名类型说明
imageuserdataQImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图
tb表类型,其中每一项的参数如下:x1:整数型,区域左上角x坐标;y1:整数型,区域左上角y坐标;x2:整数型,区域右下角x坐标;y2:整数型,区域右下角y坐标;color 字符串:要对比的16进制颜色,格式为"BBGGRR"多个颜色用"

返回值:

返回值类型说明
整数ret:如果有多个颜色用"

示例:

lua
local index = nil
local x = nil
local y = nil
local tb = {0,0,0,0,"ffffff-101010|ffffff-101010|ffffff-101010|359bed-101010",0,0.9}
index,x,y= findColorT(tb)
print(index , x , y)

local img = QImage.new()
if img ~= nil then
	if img:snapShot() then
		index,x,y = findColorT(tb)
		print(index , x , y)
	end
end

23. 区域多点比色 (cmpColorEx)

说明: 同时比较指定的多个坐标点的颜色,支持多色、偏色、相似度比较

函数: cmpColorEx(mul_color,sim)

参数:

参数名类型说明
imageuserdataQImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图
mul_color字符串需要对比的点xy坐标和16进制颜色,格式为(X坐标|Y坐标|16进制颜色),多个颜色用"
sim浮点数相似度,取值范围0-1

返回值:

返回值类型说明
整数整型,1为完全匹配,0为没有完全匹配到

示例:

lua
local color = "398|962|2b2b2b-101010,397|963|222222-101010,410|958|222222-101010"
local ret = cmpColorEx(color , 0.9)
if ret == 1 then
	print("找到了")
else
	print("没有找到")
end

local img = QImage.new()
if img ~= nil then
	if img:snapShot() then
		local ret = cmpColorEx(img,color , 0.9)
		if ret == 1 then
			print("找到了")
		else
			print("没有找到")
		end
	end
end

24. 区域多点比色 (cmpColorExT)

说明: 同时比较指定的多个坐标点的颜色,支持多色、偏色、相似度比较

函数: cmpColorExT([image],{mul_color,sim})

参数:

参数名类型说明
imageuserdataQImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图
tb表类型,其中每一项的参数如下:mul_color:字符串,需要对比的点xy坐标和16进制颜色,格式为(X坐标|Y坐标|16进制颜色),多个颜色用"

返回值:

返回值类型说明
整数整型,1为完全匹配,0为没有完全匹配到

示例:

lua
local tb = {"398|962|2b2b2b-101010,397|963|222222-101010,410|958|222222-101010",0.9}
local ret = cmpColorExT(tb)
if ret == 1 then
	print("找到了")
else
	print("没有找到")
end

local img = QImage.new()
if img ~= nil then
	if img:snapShot() then
		local ret = cmpColorExT(img,tb)
		if ret == 1 then
			print("找到了")
		else
			print("没有找到")
		end
	end
end

25. 指定坐标颜色比对 (cmpColor)

说明: 指定坐标颜色比对

函数: cmpColor([image],x,y,color,sim)

参数:

参数名类型说明
imageuserdataQImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图
x整数需要对比颜色的X坐标
y整数需要对比颜色的Y坐标
color字符串待比较的16进制颜色,格式为"BBGGRR",多个颜色用"
sim浮点数相似度,取值范围0-1

返回值:

返回值类型说明
整数整型,1为匹配到,0为没有匹配到

示例:

lua
local color = "ffffff-101010|cfcfcf-101010|ffffff-101010"
local ret = cmpColor(198 , 104 , color , 0.9)
if ret == 1 then
	print("找到了")
else
	print("没有找到")
end

local img = QImage.new()
if img ~= nil then
	if img:snapShot() then
		local ret = cmpColor(img , 198 , 104 , color , 0.9)
		if ret == 1 then
			print("找到了")
		else
			print("没有找到")
		end
	end
end

26. 颜色值转化R,G,B三个分量 (colorToRGB)

说明: 颜色比对

函数: colorToRGB(color)

参数:

参数名类型说明
color字符串/整数16进制颜色,格式为"BBGGRR",或者10进制颜色

返回值:

返回值类型说明
整数r:整数型,十进制颜色红分量
整数g:整数型,十进制颜色绿分量
整数b:整数型,十进制颜色蓝分量

示例:

lua
local r,g,b = colorToRGB(0x998899)
print(r,g,b)

27. 颜色比对 (colorDiff)

说明: 颜色比对

函数: colorDiff(color1,color2)

参数:

参数名类型说明
color1字符串/整数16进制颜色,格式为"BBGGRR",或者10进制颜色
color2字符串/整数16进制颜色,格式为"BBGGRR",或者10进制颜色

返回值:

返回值类型说明
整数整数型:两个颜色的红、绿、蓝三个分量的差值之总和

示例:

lua
local diff = colorDiff(0x998899,0x777777)
local diff1 = colorDiff("999999","888888")
print(diff)
print(diff1)

28. 获取指定范围内的像素数组 (getScreenPixel)

说明: 获取指定范围内的像素数组

函数: getScreenPixel([image],x1,y1,x2,y2)

参数:

参数名类型说明
imageuserdataQImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图
x1整数区域左上角x坐标
y1整数区域左上角y坐标
x2整数区域右下角x坐标
y2整数区域右下角y坐标

说明: 当x1,y1,x2,y2全部为0表示获取全屏像素值

返回值:

返回值类型说明
整数w,h是获取范围的长宽
数组arr是返回的数组,如果w,h小于0则表明失败

示例:

lua
function printRGB(color)
	local r , g , b = colorToRGB(color)
	print("col:".."=>"..r..","..g..","..b)
end

function getScreenPixelTest(useimage)
	local img = QImage.new()
	if img ~= nil then
		img:snapShot()
	end
	if useimage then
		w , h , arr = getScreenPixel(img , 0 , 0 , 0 , 0)
	else
		w , h , arr = getScreenPixel(0 , 0 , 0 , 0)
	end
	print("image size:"..w..","..h)
	local x = 50
	local y = 50
	local color = arr[y * w + (x + 1)]
	local cc = getPixelColor(x , y)
	printRGB(cc)
	printRGB(color)
end

getScreenPixelTest(false)
getScreenPixelTest(true)

29. 获取指定坐标的颜色值 (getPixelColor)

说明: 获取指定坐标处屏幕的颜色值

函数: getPixelColor([image],x,y,[type])

参数:

参数名类型说明
imageuserdataQImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图
x整数水平坐标
y整数垂直坐标
type整数值为1的时候返回一个整数,默认不填写type是返回颜色的16进制字符串

返回值:

返回值类型说明
字符串/整数返回指定坐标处的颜色值

示例:

lua
local pixel = getPixelColor(100,100,0)
print(pixel)

local img = QImage.new()
if img ~= nil then
	if img:snapShot() then
		local ret = getPixelColor(img ,100,100,0)
		print(ret)
	end
end

30. 截图并保存 (snapShot)

说明: 截图并保存

函数: snapShot(path,[left,top,right,bottom])

参数:

参数名类型说明
path字符串保存路径
left整数截图区域左边界坐标(可选)
top整数截图区域上边界坐标(可选)
right整数截图区域右边界坐标(可选)
bottom整数截图区域下边界坐标(可选)

返回值:

返回值类型说明
boolean返回截图是否成功

示例:

lua
local savepath = getWorkPath() .. "/save.png"
local ret = snapShot(savepath,0,0,100,100)
if ret then
	print("截图完成")
else
	print("截图失败")
end

31. 截图到内存 (keepCapture)

说明: 截取当前屏幕内容保留在内存,可以供后续图色查找

函数: keepCapture()

参数: 无

返回值: 无

示例:

lua
keepCapture()
for i = 1 , 10 , 1 do
	local x = 0-1
	local y = 0-1
	x , y = findMultiColor(0 , 0 , 0 , 0 , "359bed-101010" , "45|76|ffffff-101010|93|114|ffffff-101010|33|146|ffffff-101010|-9|432|359bed-101010|58|447|359bed-101010" , 0 , 0.9)
	if x ~= 0-1 and y ~= 0-1 then
		print(x , y)
	end
end
releaseCapture()

32. 删除内存中的截图 (releaseCapture)

说明: 删除内存中的截图

函数: releaseCapture()

参数: 无

返回值: 无

示例:

lua
keepCapture()
for i = 1 , 10 , 1 do
	local x = 0-1
	local y = 0-1
	x , y = findMultiColor(0 , 0 , 0 , 0 , "359bed-101010" , "45|76|ffffff-101010|93|114|ffffff-101010|33|146|ffffff-101010|-9|432|359bed-101010|58|447|359bed-101010" , 0 , 0.9)
	if x ~= 0-1 and y ~= 0-1 then
		print(x , y)
	end
end
releaseCapture()

33. opencv模板匹配找图 (findImage)

说明: opencv模板匹配找图

函数: findImage([image],x1, y1, x2, y2,pic_name,sim)

参数:

参数名类型说明
imageuserdataQImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图
x1整数查找区域左上X坐标
y1整数查找区域左上Y坐标
x2整数查找区域右下X坐标
y2整数查找区域右下Y坐标
pic_name字符串要找的图片名字,多个图片用"
sim浮点数相似度,取值范围0-1

返回值:

返回值类型说明
整数ret:如果有多个颜色用"
整数x,y 对应找到的坐标

示例:

lua
index , x , y = findImage(0 , 0 , 0 , 0 , "test2.png|test.png" , 0.9)
print(index , x , y)

local img = QImage.new()
if img ~= nil then
	if img:snapShot() then
		index , x , y = findImage(img , 0 , 0 , 0 , 0 , "test2.png|test.png" , 0.9)
		print(index , x , y)
	end
end

34. 高级区域找图 (findPicEx)

说明: 高级区域找图

函数: findPicEx([image],x1, y1, x2, y2,pic_name,sim)

参数:

参数名类型说明
imageuserdataQImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图
x1整数查找区域左上X坐标
y1整数查找区域左上Y坐标
x2整数查找区域右下X坐标
y2整数查找区域右下Y坐标
pic_name字符串要找的图片名字,多个图片用"
sim浮点数相似度,取值范围0-1

返回值:

返回值类型说明
整数ret:如果有多个颜色用"
整数x,y 对应找到的坐标

示例:

lua
idx , x , y = findPicEx(0 , 0 , 0 , 0 , "1.png|2.png" , 0.9)
print(idx , x , y)
local img = QImage.new()
if img ~= nil then
	if img:snapShot() then
		idx , x , y = findPicEx(img , 0 , 0 , 0 , 0 , "1.png|2.png" , 0.9)
		print(idx , x , y)
	end
end

35. 快速区域找图 (findPicFast)

说明: 快速区域找图(暂时不支持透明图)

函数: findPicFast(x1, y1, x2, y2,pic_name,delta_color,dir,sim)

参数:

参数名类型说明
imageuserdataQImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图
x1整数查找区域左上X坐标
y1整数查找区域左上Y坐标
x2整数查找区域右下X坐标
y2整数查找区域右下Y坐标
pic_name字符串要找的图片名字,多个图片用"
delta_color字符串16进制字符串,偏色
dir整数查找方向:0:表示从左上向右下查找;1:表示从中心往四周查找;2:表示从右下向左上查找;3:表示从左下向右上查找;4:表示从右上向左下查找
sim浮点数相似度,取值范围0-1

返回值:

返回值类型说明
整数index:如果有多个颜色用"
整数x,y 对应找到的坐标

示例:

lua
index , x , y = findPicFast(0 , 0 , 0 , 0 , "1.png|2.png" , "101010" , 0 , 0.9)
print(index , x , y)

36. 快速区域找图 (findPicFastT)

说明: 快速区域找图(暂时不支持透明图)

函数: findPicFastT([image],{x1, y1, x2, y2,pic_name,delta_color,dir,sim})

参数:

参数名类型说明
imageuserdataQImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图
tb表类型,其中每一项的参数如下:x1:整数型,查找区域左上X坐标;y1:整数型,查找区域左上Y坐标;x2:整数型,查找区域右下X坐标;y2:整数型,查找区域右下Y坐标;pic_name:字符串,要找的图片名字,多个图片用"

返回值:

返回值类型说明
整数index:如果有多个颜色用"
整数x,y 对应找到的坐标

示例:

lua
local tb = {0,0,0,0,"1.png|2.png","101010",0,0.9}
index , x , y = findPicFastT(tb)
print(index , x , y)

37. 快速区域找图匹配所有目标 (findPicFastAll)

说明: 快速区域找图匹配所有目标(暂时不支持透明图)

函数: findPicFastAll(x1, y1, x2, y2,pic_name,delta_color,dir,sim)

参数:

参数名类型说明
imageuserdataQImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图
x1整数查找区域左上X坐标
y1整数查找区域左上Y坐标
x2整数查找区域右下X坐标
y2整数查找区域右下Y坐标
pic_name字符串要找的图片名字,多个图片用"
delta_color字符串16进制字符串,偏色
dir整数查找方向:0:表示从左上向右下查找;1:表示从中心往四周查找;2:表示从右下向左上查找;3:表示从左下向右上查找;4:表示从右上向左下查找
sim浮点数相似度,取值范围0-1

返回值:

返回值类型说明
返回所有满足要求的表格数组

示例:

lua
local ret = findPicFastAll(0 , 0 , 0 , 0 , "1.png|2.png" , "101010" , 0 , 0.9)
if ret ~= nil then
	print(ret)
end