Skip to content

Instantly share code, notes, and snippets.

@KnIfER
Created November 6, 2023 04:09
Show Gist options
  • Save KnIfER/0d95300584502ea67667a06c13b59785 to your computer and use it in GitHub Desktop.
Save KnIfER/0d95300584502ea67667a06c13b59785 to your computer and use it in GitHub Desktop.
Easily recognize texts on screen, or just copy image area to clipboard. Left-click and drag to execute OCR; Right-click and drag to copy the screenshot; Click = use the last selection box.
#NoEnv
#MaxThreadsPerHotkey 2
#SingleInstance Force
; 安装需求: https://github.com/telppa/PaddleOCR-AutoHotkey
; getSelectionCoords 改自社区脚本。 纯英文可用 Capture2Text
F2::
;^Q::
RunOCR()
return
#IfWinActive ahk_exe AutoHotkeyU64.exe
^W::
send !{F4}
return
#Include D:\Code\FigureOut\chrome\extesions\AutoHotKey\PaddleOCR\PaddleOCR.ahk
RunOCR(copy:=false){
global x_start
global x_end
global y_start
global y_end
global _right
; 获得屏幕选框, 左键拖拽执行ocr,右键拖拽复制截图;点击=沿用上次选框。
if(getSelectionCoords(x_start, x_end, y_start, y_end, _right)=1)
{
if(copy || _right) {
;消息("复制图像!")
Gdip_Startup()
TempBitmap:=Gdip_BitmapFromScreen(x_start "|" y_start "|" (x_end-x_start) "|" (y_end-y_start))
; Gdip_SaveBitmapToFile(TempBitmap,A_WorkingDir "\" ShotName ".png", 100)
Gdip_SetBitmapToClipboard(TempBitmap)
Gdip_DisposeImage(TempBitmap)
;~ SoundBeep,400,300
; Traytip,,Shot Saved
return
}
; RunWait, D:\software\Capture2Text_v4.6.3_64bit\Capture2Text.exe --screen-rect "%x_start% %y_start% %x_end% %y_end%" --clipboard
; MsgBox, In area :: --screen-rect "%x_start% %y_start% %x_end% %y_end%" `n`nFound Text:`n`n%clipboard%
global MyText
;消息([x_start, y_start, x_end-x_start, y_end-y_start])
Try {
MyText := PaddleOCR([x_start, y_start, x_end-x_start, y_end-y_start])
;MyText := StrReplace(MyText, "@", "")
Try {
Gui, MyGui:Font, s24
; Gui MyGui:+Resize +MinSize929x347
; Gui, MyGui:Add, Text, , %tmp%
Gui, MyGui:Add, Edit, vMyText, %MyText%
GuiControl,MyGui:Move, MyText, w800 h175
}
GuiControl,MyGui:,MyText,%MyText%
Gui, MyGui:Show, xCenter yCenter AutoSize
;, w929 h347
}
}
}
dist(dx, dy) {
;消息(" " (dx*dx + dy*dy) " " dx " " dy)
return dx*dx + dy*dy
}
; creates a click-and-drag selection box to specify an area
getSelectionCoords(ByRef x_start, ByRef x_end, ByRef y_start, ByRef y_end, ByRef _right) {
;Mask Screen
Gui, Color, 000000
Gui +LastFound
WinSet, Transparent, 75
Gui, -Caption
Gui, +AlwaysOnTop
Gui, Show, x0 y0 h%A_ScreenHeight% w%A_ScreenWidth%,"AutoHotkeySnapshotApp"
;Drag Mouse
CoordMode, Mouse, Screen
CoordMode, Tooltip, Screen
WinGet, hw_frame_m,ID,"AutoHotkeySnapshotApp"
hdc_frame_m := DllCall( "GetDC", "uint", hw_frame_m)
;KeyWait, LButton, D
MouseGetPos, scan_x_start, scan_y_start
MouseGetPos, scan_x_end, scan_x_end
ret := 0
btn := "LButton"
_right := false
Loop
{
Sleep, 10
KeyIsDown := GetKeyState(btn)
if(KeyIsDown!=1 && ret=0 && GetKeyState("RButton")=1){
btn := "RButton"
_right := true
KeyIsDown := 1
}
if (KeyIsDown)
{
if(ret=0) {
MouseGetPos, scan_x_start, scan_y_start
ret := 1
}
MouseGetPos, scan_x_end, scan_y_end
DllCall( "gdi32.dll\Rectangle", "uint", hdc_frame_m, "int", 0,"int",0,"int", A_ScreenWidth,"int",A_ScreenWidth)
DllCall( "gdi32.dll\Rectangle", "uint", hdc_frame_m, "int", scan_x_start,"int",scan_y_start,"int", scan_x_end,"int",scan_y_end)
} else if(ret=1){
break
}
}
;KeyWait, LButton, U
if(ret=0 || !WinActive(, ahk_id %hw_frame_m%)) {
Gui Destroy
return 0
}
MouseGetPos, scan_x_end, scan_y_end
Gui Destroy
; dist(scan_x_start-scan_x_end, scan_y_start-scan_y_end)
if(scan_x_start!=scan_x_end || scan_y_start!=scan_y_end)
if(dist(scan_x_start-scan_x_end, scan_y_start-scan_y_end)>100)
{
if (scan_x_start < scan_x_end)
{
x_start := scan_x_start
x_end := scan_x_end
} else {
x_start := scan_x_end
x_end := scan_x_start
}
if (scan_y_start < scan_y_end)
{
y_start := scan_y_start
y_end := scan_y_end
} else {
y_start := scan_y_end
y_end := scan_y_start
}
}
return 1
}
; gdi 函数用于复制图像,复制了社区脚本 https://www.autohotkey.com/boards/viewtopic.php?t=68956
Gdip_Startup()
{
global _gdip
if(!_gdip) {
; 消息("Gdip_Startup 启动")
Ptr := A_PtrSize ? "UPtr" : "UInt"
if !DllCall("GetModuleHandle", "str", "gdiplus", Ptr)
DllCall("LoadLibrary", "str", "gdiplus")
VarSetCapacity(si, A_PtrSize = 8 ? 24 : 16, 0), si := Chr(1)
DllCall("gdiplus\GdiplusStartup", A_PtrSize ? "UPtr*" : "uint*", pToken, Ptr, &si, Ptr, 0)
_gdip = pToken
}
}
Gdip_Shutdown()
{
global _gdip
pToken := _gdip
if(pToken) {
; 消息("Gdip_Shutdown")
Ptr := A_PtrSize ? "UPtr" : "UInt"
DllCall("gdiplus\GdiplusShutdown", Ptr, pToken)
if hModule := DllCall("GetModuleHandle", "str", "gdiplus", Ptr)
DllCall("FreeLibrary", Ptr, hModule)
return 0
}
}
Gdip_CreateHBITMAPFromBitmap(pBitmap, Background=0xffffffff)
{
DllCall("gdiplus\GdipCreateHBITMAPFromBitmap", A_PtrSize ? "UPtr" : "UInt", pBitmap, A_PtrSize ? "UPtr*" : "uint*", hbm, "int", Background)
return hbm
}
Gdip_CreateBitmapFromHBITMAP(hBitmap, Palette=0)
{
Ptr := A_PtrSize ? "UPtr" : "UInt"
DllCall("gdiplus\GdipCreateBitmapFromHBITMAP", Ptr, hBitmap, Ptr, Palette, A_PtrSize ? "UPtr*" : "uint*", pBitmap)
return pBitmap
}
SelectObject(hdc, hgdiobj)
{
Ptr := A_PtrSize ? "UPtr" : "UInt"
return DllCall("SelectObject", Ptr, hdc, Ptr, hgdiobj)
}
DeleteObject(hObject)
{
return DllCall("DeleteObject", A_PtrSize ? "UPtr" : "UInt", hObject)
}
GetDCEx(hwnd, flags=0, hrgnClip=0)
{
Ptr := A_PtrSize ? "UPtr" : "UInt"
return DllCall("GetDCEx", Ptr, hwnd, Ptr, hrgnClip, "int", flags)
}
GetDC(hwnd=0)
{
return DllCall("GetDC", A_PtrSize ? "UPtr" : "UInt", hwnd)
}
BitBlt(ddc, dx, dy, dw, dh, sdc, sx, sy, Raster="")
{
Ptr := A_PtrSize ? "UPtr" : "UInt"
return DllCall("gdi32\BitBlt"
, Ptr, dDC
, "int", dx
, "int", dy
, "int", dw
, "int", dh
, Ptr, sDC
, "int", sx
, "int", sy
, "uint", Raster ? Raster : 0x00CC0020)
}
DeleteDC(hdc)
{
return DllCall("DeleteDC", A_PtrSize ? "UPtr" : "UInt", hdc)
}
Gdip_DisposeImage(pBitmap)
{
return DllCall("gdiplus\GdipDisposeImage", A_PtrSize ? "UPtr" : "UInt", pBitmap)
}
CreateCompatibleDC(hdc=0)
{
return DllCall("CreateCompatibleDC", A_PtrSize ? "UPtr" : "UInt", hdc)
}
CreateDIBSection(w, h, hdc="", bpp=32, ByRef ppvBits=0)
{
Ptr := A_PtrSize ? "UPtr" : "UInt"
hdc2 := hdc ? hdc : GetDC()
VarSetCapacity(bi, 40, 0)
NumPut(w, bi, 4, "uint")
, NumPut(h, bi, 8, "uint")
, NumPut(40, bi, 0, "uint")
, NumPut(1, bi, 12, "ushort")
, NumPut(0, bi, 16, "uInt")
, NumPut(bpp, bi, 14, "ushort")
hbm := DllCall("CreateDIBSection"
, Ptr, hdc2
, Ptr, &bi
, "uint", 0
, A_PtrSize ? "UPtr*" : "uint*", ppvBits
, Ptr, 0
, "uint", 0, Ptr)
if !hdc
ReleaseDC(hdc2)
return hbm
}
ReleaseDC(hdc, hwnd=0)
{
Ptr := A_PtrSize ? "UPtr" : "UInt"
return DllCall("ReleaseDC", Ptr, hwnd, Ptr, hdc)
}
Gdip_BitmapFromScreen(Screen=0, Raster=""){
if (Screen = 0){
Sysget, x, 76
Sysget, y, 77
Sysget, w, 78
Sysget, h, 79
}
else if (SubStr(Screen, 1, 5) = "hwnd:")
{
Screen := SubStr(Screen, 6)
if !WinExist( "ahk_id " Screen)
return -2
WinGetPos,,, w, h, ahk_id %Screen%
x := y := 0
hhdc := GetDCEx(Screen, 3)
}
else if (Screen&1 != "")
{
Sysget, M, Monitor, %Screen%
x := MLeft, y := MTop, w := MRight-MLeft, h := MBottom-MTop
}
else
{
StringSplit, S, Screen, |
x := S1, y := S2, w := S3, h := S4
}
if (x = "") || (y = "") || (w = "") || (h = "")
return -1
chdc := CreateCompatibleDC(), hbm := CreateDIBSection(w, h, chdc), obm := SelectObject(chdc, hbm), hhdc := hhdc ? hhdc : GetDC()
BitBlt(chdc, 0, 0, w, h, hhdc, x, y, Raster)
ReleaseDC(hhdc)
pBitmap := Gdip_CreateBitmapFromHBITMAP(hbm)
SelectObject(chdc, obm), DeleteObject(hbm), DeleteDC(hhdc), DeleteDC(chdc)
return pBitmap
}
Gdip_SetBitmapToClipboard(pBitmap)
{
Ptr := A_PtrSize ? "UPtr" : "UInt"
off1 := A_PtrSize = 8 ? 52 : 44, off2 := A_PtrSize = 8 ? 32 : 24
hBitmap := Gdip_CreateHBITMAPFromBitmap(pBitmap)
DllCall("GetObject", Ptr, hBitmap, "int", VarSetCapacity(oi, A_PtrSize = 8 ? 104 : 84, 0), Ptr, &oi)
hdib := DllCall("GlobalAlloc", "uint", 2, Ptr, 40+NumGet(oi, off1, "UInt"), Ptr)
pdib := DllCall("GlobalLock", Ptr, hdib, Ptr)
DllCall("RtlMoveMemory", Ptr, pdib, Ptr, &oi+off2, Ptr, 40)
DllCall("RtlMoveMemory", Ptr, pdib+40, Ptr, NumGet(oi, off2 - (A_PtrSize ? A_PtrSize : 4), Ptr), Ptr, NumGet(oi, off1, "UInt"))
DllCall("GlobalUnlock", Ptr, hdib)
DllCall("DeleteObject", Ptr, hBitmap)
DllCall("OpenClipboard", Ptr, 0)
DllCall("EmptyClipboard")
DllCall("SetClipboardData", "uint", 8, Ptr, hdib)
DllCall("CloseClipboard")
}
; killAlert:
; progress,off
; settimer, killAlert,Off
; return
; 消息(Message,Width := "w450",Timeout := "-500", Title := "")
; {
; Progress, b1 zh0 fs18 %Width% , %Message%,,%Title%,
; settimer, killAlert,%Timeout%
; }
@KnIfER
Copy link
Author

KnIfER commented Nov 6, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment