Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Capture Qm Window
#1
Hi everyone thought i would share a little function i use a lot for creating instant screenshots of the Quick Macros window 

requires GDI+ get it here-->> Get GDI+

Trigger: Ctrl+Prnt Scrn

Function CaptureQmWindow 
Trigger C(44)     Help - how to add the trigger to the macro
Code:
Copy      Help
;Captures QM Window and saves as a png(or see below) using GDI+
;Supported formats: bmp, gif, jpg, png, tiff.(png recommended)
;creates a unique file name to avoid saving problems
;REMARKS
;requires GDI+ download it here <link "http://www.quickmacros.com/forum/showthread.php?tid=3703">Get GDI+</link>

if(getopt(nthreads)>1) ret
spe -2
;create file data
mkdir "QM_WindowImages" "$desktop$";;create folder if it doesnt exist to save images in
str sFolderName = "$desktop$\QM_WindowImages\"
str sFileName.timeformat("{yyyy-MM-dd}_{HH-mm-ss}_QmWindow");;unique filename to avoid problems
str sFormat = ".png";;i use png for smaller filesize
str sFile.format("%s%s%s" sFolderName sFileName sFormat)

;captures QM Window to memory
__GdiHandle hb
RECT r; DpiGetWindowRect _hwndqm &r
CaptureImageOnScreen r.left r.top r.right-r.left r.bottom-r.top "" hb

;save image
#compile "__Gdip"
GdipBitmap im
if(!im.FromHBITMAP(hb)) end "error"
if(!im.Save(sFile)) end "error"
;open folder where image is saved
run sFolderName
;optional open file
;run sFile;;uncomment this line to use
requires GDI+ get it here-->> Get GDI+
#2
@kevin

The above code, I have not tested yet, I want to know, can it generate PNG format pictures?

The following code is the sample code I found in the ron tutorial, it can only generate bmp format images

The BMP format image is too big. I want to send the generated image to Baidu's OCR server and then recognize the text.

If the picture is too large, the sending time will be very long. I think the PNG format is the most suitable.

Macro QM_bmp
Code:
Copy      Help
str dd=
;BEGIN DIALOG
;0 "" 0x90C80AC8 0x0 0 0 199 91 "Dialog"
;3 Button 0x54032000 0x0 40 9 129 30 "3. OUTPUT POINTS DRAGGED AREA DESKTOP"
;4 Button 0x54032000 0x0 39 49 130 32 "4. SCREENSHOT DRAG"
;END DIALOG
;DIALOG EDITOR: "" 0x2040202 "*" "" "" ""

if(!ShowDialog(dd &sub.DlgProc 0)) ret

#sub DlgProc
function# hDlg message wParam lParam

sel message
,case WM_INITDIALOG
,,RECT- r
,,EnableWindow id(3 hDlg) 0
,case WM_DESTROY    
,case WM_COMMAND goto messages2
ret
;messages2
sel wParam
,case 3        
,,out r.left
,,out r.top
,,out r.right
,,out r.bottom        
,case 4
,,str saveTo="$desktop$\new.bmp"
,,if(!CaptureImageOrColor(0 0 hDlg saveTo r)) ret
,,EnableWindow id(3 hDlg) 1
,,;saveTo.UniqueFileName
,,;CaptureImageOnScreen r.left r.top r.right r.bottom saveTo
,case IDOK
,case IDCANCEL
ret 1


I want to implement something like the following Smile


Macro SNA+OCR
Code:
Copy      Help
str dd=
;BEGIN DIALOG
;0 "" 0x90C80AC8 0x0 0 0 199 91 "Dialog"
;3 Button 0x54032000 0x0 40 9 129 30 "3. OUTPUT POINTS DRAGGED AREA DESKTOP"
;4 Button 0x54032000 0x0 39 49 130 32 "4. SCREENSHOT DRAG"
;END DIALOG
;DIALOG EDITOR: "" 0x2040202 "*" "" "" ""

if(!ShowDialog(dd &sub.DlgProc 0)) ret

#sub DlgProc
function# hDlg message wParam lParam

sel message
,case WM_INITDIALOG
,,RECT- r
,,EnableWindow id(3 hDlg) 0
,case WM_DESTROY    
,case WM_COMMAND goto messages2
ret
;messages2
sel wParam
,case 3        
,,out r.left
,,out r.top
,,out r.right
,,out r.bottom        
,case 4
,,str saveTo="$desktop$\new.bmp"
,,if(!CaptureImageOrColor(0 0 hDlg saveTo r)) ret
,,_s=sub.ocr(saveTo)
,,ShowText("Recognition result" _s)
,,
,,EnableWindow id(3 hDlg) 1
,,;saveTo.UniqueFileName
,,;CaptureImageOnScreen r.left r.top r.right r.bottom saveTo
,case IDOK
,case IDCANCEL
ret 1


#sub ocr
function~ ~name
typelib WinHttp {662901FC-6951-4854-9EB2-D9A2570F2B2E} 5.1; WinHttp.WinHttpRequest r._create; r.Open("POST" F"https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic"); r.setRequestHeader("content-type", "application/x-www-form-urlencoded"); r.SetRequestHeader("cache-control" , "no-cache")
str access= "access_token=24.b305c9822131e65eda05e29c157dd719.2592000.1559103982.282335-16139329&image="
out name
str sFile.getfile(F"{name}");; change to file location
_s.encrypt(4 sFile "" 2); _s.escape(9); r.Send(F"{access}{_s}")
ARRAY(byte) a=r.ResponseBody; str s.fromn(&a[0] a.len)
IXml x=JsonToXml(s); ARRAY(IXmlNode) a1; x.Path("root/words_result/item/words" a1 1)

int i
str d
for(i 0 a1.len)
,;;out a1[i].Value
,d.formata("%s[]" a1[i].Value)
ret d.trim
#3
;Supported formats: bmp, gif, jpg, png, tiff.(png recommended)
#4
Can you improve the above code? Bmp image size is too large Confused

How to create a drag effect

How to change the size of the default text?


Attached Files Image(s)
   
#5
Code:
Copy      Help
str dd=
;BEGIN DIALOG
;0 "" 0x90C80AC8 0x0 0 0 199 91 "Capture an Image"
;3 Button 0x54032000 0x0 40 9 129 30 "3. OUTPUT POINTS DRAGGED AREA DESKTOP"
;4 Button 0x54032000 0x0 39 49 130 32 "4. SCREENSHOT DRAG"
;END DIALOG
;DIALOG EDITOR: "" 0x2040802 "*" "" "" ""

if(!ShowDialog(dd &sub.DlgProc 0)) ret

#sub DlgProc
function# hDlg message wParam lParam

sel message
,case WM_INITDIALOG
,,RECT- r
,,EnableWindow id(3 hDlg) 0
,case WM_DESTROY
,case WM_COMMAND goto messages2
ret
;messages2
sel wParam
,case 3
,,out r.left
,,out r.top
,,out r.right
,,out r.bottom
,case 4
,,__GdiHandle hb
,,if(!CaptureImageOrColor(hb 0 hDlg "" r)) ret
,,mkdir "QM_CaptureImages" "$desktop$";;create folder if it doesnt exist to save images in
,,str sFolderName = "$desktop$\QM_CaptureImages\"
,,str sFileName.timeformat("{yyyy-MM-dd}_{HH-mm-ss}_QmCapture");;unique filename to avoid problems
,,str sFormat = ".png";;i use png for smaller filesize
,,str sFile.format("%s%s%s" sFolderName sFileName sFormat)
,,;save image
,,#compile "__Gdip"
,,GdipBitmap im
,,if(!im.FromHBITMAP(hb)) end "error"
,,if(!im.Save(sFile)) end "error"
,,_s=sub.ocr(sFile)
,,ShowText("Recognition result" _s)
,,EnableWindow id(3 hDlg) 1
,case IDOK
,case IDCANCEL
ret 1


#sub ocr
function~ ~name
typelib WinHttp {662901FC-6951-4854-9EB2-D9A2570F2B2E} 5.1; WinHttp.WinHttpRequest r._create; r.Open("POST" F"https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic"); r.setRequestHeader("content-type", "application/x-www-form-urlencoded"); r.SetRequestHeader("cache-control" , "no-cache")
str access= "access_token=24.b305c9822131e65eda05e29c157dd719.2592000.1559103982.282335-16139329&image="
out name
str sFile.getfile(F"{name}");; change to file location
_s.encrypt(4 sFile "" 2); _s.escape(9); r.Send(F"{access}{_s}")
ARRAY(byte) a=r.ResponseBody; str s.fromn(&a[0] a.len)
IXml x=JsonToXml(s); ARRAY(IXmlNode) a1; x.Path("root/words_result/item/words" a1 1)

int i
str d
for(i 0 a1.len)
,d.formata("%s[]" a1[i].Value)
ret d.trim

didn't change much just added code to convert bitmap to png and save as a unique filename.

requires GDI+ get it here-->> Get GDI+
#6
Thank you, the speed of recognition is greatly improved.
#7
probably should clear the __GdiHandle and the GdipBitmap if your going to keep the dialog open or will have problems over time

if you plan on keeping the dialog open

use this code
Code:
Copy      Help
str dd=
;BEGIN DIALOG
;0 "" 0x90C80AC8 0x0 0 0 199 91 "Capture an Image"
;3 Button 0x54032000 0x0 40 9 129 30 "3. OUTPUT POINTS DRAGGED AREA DESKTOP"
;4 Button 0x54032000 0x0 39 49 130 32 "4. SCREENSHOT DRAG"
;END DIALOG
;DIALOG EDITOR: "" 0x2040802 "*" "" "" ""

if(!ShowDialog(dd &sub.DlgProc 0)) ret

#sub DlgProc
function# hDlg message wParam lParam

sel message
,case WM_INITDIALOG
,,RECT- r
,,EnableWindow id(3 hDlg) 0
,case WM_DESTROY
,case WM_COMMAND goto messages2
ret
;messages2
sel wParam
,case 3
,,out r.left
,,out r.top
,,out r.right
,,out r.bottom
,case 4
,,__GdiHandle hb
,,if(!CaptureImageOrColor(hb 0 hDlg "" r)) ret
,,mkdir "QM_CaptureImages" "$desktop$";;create folder if it doesnt exist to save images in
,,str sFolderName = "$desktop$\QM_CaptureImages\"
,,str sFileName.timeformat("{yyyy-MM-dd}_{HH-mm-ss}_QmCapture");;unique filename to avoid problems
,,str sFormat = ".png";;i use png for smaller filesize
,,str sFile.format("%s%s%s" sFolderName sFileName sFormat)
,,;save image
,,#compile "__Gdip"
,,GdipBitmap im
,,if(!im.FromHBITMAP(hb)) end "error"
,,if(!im.Save(sFile)) end "error"
,,_s=sub.ocr(sFile)
,,ShowText("Recognition result" _s)
,,EnableWindow id(3 hDlg) 1
,,im.Delete
,,hb.Delete
,case IDOK
,case IDCANCEL
ret 1


#sub ocr
function~ ~name
typelib WinHttp {662901FC-6951-4854-9EB2-D9A2570F2B2E} 5.1; WinHttp.WinHttpRequest r._create; r.Open("POST" F"https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic"); r.setRequestHeader("content-type", "application/x-www-form-urlencoded"); r.SetRequestHeader("cache-control" , "no-cache")
str access= "access_token=24.b305c9822131e65eda05e29c157dd719.2592000.1559103982.282335-16139329&image="
out name
str sFile.getfile(F"{name}");; change to file location
_s.encrypt(4 sFile "" 2); _s.escape(9); r.Send(F"{access}{_s}")
ARRAY(byte) a=r.ResponseBody; str s.fromn(&a[0] a.len)
IXml x=JsonToXml(s); ARRAY(IXmlNode) a1; x.Path("root/words_result/item/words" a1 1)

int i
str d
for(i 0 a1.len)
,d.formata("%s[]" a1[i].Value)
ret d.trim
#8
Gdi related code, I don't understand
#9
The gdi objects need to be deleted if the dialog remains open or will create a memory leak with repeated capture attempts. If the dialog is closed after a capture and ocr return then dont need to delete the handles it is done automatically when thread ends
#10
How to save an image as a 256-color PNG format?  Sometimes I need a smaller picture Huh


Forum Jump:


Users browsing this thread: 7 Guest(s)