WinCapture:毫秒级屏幕取色+图像识别库(AHKv1封装版)

性能基准测试

操作类型耗时(毫秒)
单次取色/找色<1ms
10次截图+文件保存[待补充数据]

注:基于C++底层实现,满足游戏压枪等高帧率场景需求

核心技术对比

1. GDI(图形设备接口)

  • 架构原理:通过屏幕DC获取位图数据的软件层方案
  • 优势
    • 全Windows版本兼容
    • 无需特殊硬件支持
  • 局限
    • CPU占用率高(典型占用率>30%)
    • 鼠标采集时会出现闪烁
    • 无法过滤指定窗口

2. DXGI(DirectX图形基础设施)

  • 架构原理:基于DirectX的硬件加速方案(Win8+)
  • 优势
    • GPU直接处理纹理
    • 性能峰值可达1000FPS+
  • 局限
    • 需额外处理鼠标采集
    • 不同Direct3D版本存在兼容差异

3. WGC(Windows图形捕获)

  • 架构原理:基于D3D11的现代采集方案(Win10 1803+)
  • 优势
    • 1080P采集GPU占用<5%
    • 多显示器支持完善
  • 局限
    • 窗口采集时显示黄色边框
    • 最低需Win10 1903版本

4. DWM(桌面窗口管理器)

  • 架构原理:系统级窗口合成器接口
  • 优势
    • 无视觉干扰(相比WGC)
    • 性能与WGC持平
  • 局限
    • 仅支持窗口采集(不支持全屏)

功能模块详解

核心功能

  1. 图像采集
    • 支持全屏/区域截图
    • 多显示器采集
    • 后台窗口捕获(DWM方案)
  2. 色彩处理
    • 单点取色(getHexColor)
    • 多色匹配(findMultiColors)
    • 色彩统计(像素级分析)
  3. 图像识别
    • 模板匹配(findPic)
    • 多目标检测(findAllPic)
    • 与FindText库互通

典型应用场景

; 示例1:游戏自动化
if (findColor(100, 200, "0xFF0000")) {
; 发现红色目标时执行操作
}

; 示例2:UI测试自动化
findAllPic(buttonPositions, "submit_button.png")

集成方案

FindText兼容模式

通过FtoW()函数实现多色查找字符串转换:

; 将FindText格式转为WinCapture字典
colorPattern := FtoW("|<>##1010$71.000000kA0000000")
findMultiColors(0, 0, colorPattern)

优化建议

  1. 游戏应用优先选用DXGI方案
  2. 企业软件建议DWM+WGC混合方案
  3. 需要兼容XP系统时使用GDI方案
SetBatchLines -1
#SingleInstance Force

; 初始化WinCapture库
WinCapture_init()  

; ==================== DXGI截图与颜色操作示例 ====================
;/*
MeasureExecutionTime()
screenCapture := DXGIcaptureAndSave()  ; 全屏截图
screenCapture.findColor(foundX, foundY, "0xFFFFFF")  ; 查找白色像素
ShowDebugTooltip("全屏找色:" foundX ", " foundY " - 耗时:" MeasureExecutionTime() "ms")

MeasureExecutionTime()
colorValue := screenCapture.getHexColor(foundX, foundY)  ; 获取指定位置颜色
ShowDebugTooltip("取色数据:" colorValue " 耗时:" MeasureExecutionTime() "ms")

; 区域截图测试
MeasureExecutionTime()
captureArray := []
Loop 10 {
    ; 截取屏幕中心区域(640x640)
    screenCapture := DXGIcaptureAndSave([960-320, 540-320, 960+320, 540+320])  
    screenCapture.Save("DXGIScreenShot_" A_Index ".bmp")
    captureArray.push(screenCapture)
}
ShowDebugTooltip("10次区域截屏保存 耗时:" MeasureExecutionTime() "ms")

; 批量保存截图
MeasureExecutionTime()
for index, capture in captureArray {
    capture.Save("ArrScreenShot_" index ".bmp")
}
ShowDebugTooltip("10次数组读取保存 耗时:" MeasureExecutionTime() "ms")
Return
;*/

; ==================== 多色查找示例 ====================
/*
; 多色点阵查找测试
Loop 10 {
    MeasureExecutionTime()
    screenCapture := DXGIcaptureAndSave([500, 500, 1920, 1080])  ; 指定区域截图
    screenCapture.findMultiColors(foundX, foundY, [["0xFFFFFF", 0, 0], ["0xFFFFFF", 1, 0]])
    ShowDebugTooltip(foundX ", " foundY " 耗时:" MeasureExecutionTime() "ms")
}

; FindText格式转换
colorPattern := "|<>##0$0/0/723859,0/5/713859,0/8/723859,5/5/723859,10/2/723859,10/6/723859,10/9/723859,5/12/CAE4FC"
colorPoints := ConvertFindTextToColorArray(colorPattern)

; 多色点阵查找
MeasureExecutionTime()
screenCapture := DXGIcaptureAndSave()
foundPoints := []
screenCapture.findAllMultiColors(foundPoints, colorPoints)
ShowDebugTooltip("全屏多色查找耗时:" MeasureExecutionTime() "ms")

for index, point in foundPoints {
    ShowDebugTooltip("找到坐标" index ":" point["x"] ", " point["y"])
}

; 热键取色示例
F1::
    targetX := 26, targetY := 12
    screenCapture := DXGIcaptureAndSave()
    colorValue := screenCapture.getHexColor(targetX, targetY)
    ShowDebugTooltip("F1取色数据:" colorValue)
Return
*/

; ==================== 窗口后台截图示例 ====================
/*
targetWindow := WinExist("微信 ahk_class WeChatMainWndForPC")
if (!targetWindow) {
    ShowDebugTooltip("未找到目标窗口")
    Return
}

; 窗口截图
windowCapture := DWMcapture(targetWindow)
windowCapture.Show("微信窗口截图")

; 图像查找测试
targetImagePath := A_ScriptDir "\search_target.bmp"
targetImage := BitmapBuffer.LoadPicture(targetImagePath)

Loop 10 {
    MeasureExecutionTime()
    windowCapture := DWMcapture(targetWindow)
    windowCapture.findPic(foundX, foundY, targetImage)
    ShowDebugTooltip(foundX ", " foundY " 耗时:" MeasureExecutionTime() "ms")
}

; 查找所有匹配位置
foundLocations := []
windowCapture.findAllPic(foundLocations, targetImage)
for index, location in foundLocations {
    ShowDebugTooltip("找到位置" index ":" location["x"] ", " location["y"])
}
*/

; ==================== 截图工具示例 ====================
/*
MeasureExecutionTime()
screenCapture := DXGIcaptureAndSave()  ; 全屏截图
screenCapture.Save("FullScreen.bmp")
ShowDebugTooltip("截图保存耗时:" MeasureExecutionTime() "ms")

; 显示截图
screenCapture.Show("全屏截图预览")

; 截取部分区域并显示
resizedCapture := screenCapture.range(0, 0, 900, 500)
resizedCapture.Show("缩放后截图")
*/

; ==================== WGC截图示例 ====================
/*
; 窗口截图模式
editorWindow := WinExist("ahk_class AHKEditor")
if (editorWindow) {
    wgcSource := WGCcaptureSource(editorWindow)
    editorCapture := WGCcapture(wgcSource)
    editorCapture.Show("AHK编辑器窗口截图")
}

Sleep 2000

; 主屏幕截图模式
mainScreenCapture := WGCcapture(WGCcaptureSource(0))  ; 0表示主屏幕
mainScreenCapture.Show("主屏幕截图")
Sleep 3000
*/

; ==================== 颜色统计分析示例 ====================
/*
MeasureExecutionTime()
startX := 0
startY := 0
width := 110
height := 64

colorStats := Map()
totalPixels := 0

screenCapture := DXGIcaptureAndSave()
Loop %width% {
    currentX := startX + A_Index - 1
    Loop %height% {
        currentY := startY + A_Index - 1
        colorValue := screenCapture.getHexColor(currentX, currentY)
        colorStats[colorValue] := colorStats.Has(colorValue) ? colorStats[colorValue] + 1 : 1
        totalPixels++
    }
}

; 显示统计结果
resultMsg := "白色(0xFFFFFF)出现次数: " (colorStats.Has("0xFFFFFF") ? colorStats["0xFFFFFF"] : 0) "`n"
resultMsg .= "总像素数: " totalPixels "`n"
resultMsg .= "独特颜色数: " colorStats.Count "`n"
resultMsg .= "分析耗时: " MeasureExecutionTime() "ms"

MsgBox % resultMsg
Return
*/
下载权限
查看
  • 免费下载
    评论并刷新后下载
    登录后下载
  • {{attr.name}}:
您当前的等级为
登录后免费下载登录 小黑屋反思中,不准下载! 评论后刷新页面下载评论 支付以后下载 请先登录 您今天的下载次数(次)用完了,请明天再来 支付积分以后下载立即支付 支付以后下载立即支付 您当前的用户组不允许下载升级会员
您已获得下载权限 您可以每天下载资源次,今日剩余
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA捐赠
共{{data.count}}人
人已捐赠
其他函数应用案例

某网页单据打印辅助AutoHotkey脚本-v2.0-2025-04-03

2025-4-3 13:41:52

函数

数学表达式求值库eval:工程师的计算神器

2025-6-28 9:56:02

2 条回复 A文章作者 M管理员
  1. hexuren

    感谢分享

  2. 财评审计陈

    按键精灵可以调用这个么??

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索