-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpageRecorder.au3
429 lines (343 loc) · 13.9 KB
/
pageRecorder.au3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#include <Misc.au3>
#include <WinAPI.au3>
#include <GDIPlus.au3>
#include <WindowsConstants.au3>
#include <WinAPIGdi.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <String.au3>
#include <File.au3>
Global $screenScale = 2 ; Adjust screen scale factor as needed
Opt("MouseCoordMode", 1) ; Use absolute coordinates for mouse position
Global $mainGUI
Global $pageRect, $buttonRect, $pageCount, $autoCrop
Global $hOverlay, $hOverlayDC, $hPageRectOverlay, $hPageRectOverlayDC, $hButtonRectOverlay, $hButtonRectOverlayDC
Global $avgCroppedWidth, $avgCroppedHeight, $numCroppedImage
Func Main()
; Create main GUI dialog
$mainGUI = GUICreate("Document Screen Capture Tool", 300, 200)
GUISetFont(10)
Local $btnSelectPage = GUICtrlCreateButton("Page Area", 50, 10, 200, 30)
Local $btnSelectButton = GUICtrlCreateButton("Turn Page Button", 50, 50, 200, 30)
Local $btnStartCapture = GUICtrlCreateButton("Start Capture", 50, 160, 200, 30)
Local $lblPageCount = GUICtrlCreateLabel("Page Num", 50, 95, 80, 20)
Local $txtPageCount = GUICtrlCreateInput("1", 120, 90, 40, 25)
Local $checkboxAutoCrop = GUICtrlCreateCheckbox("AutoCrop", 170, 90, 100, 25)
GUICtrlSetState ($checkboxAutoCrop, $GUI_CHECKED)
GUISetState(@SW_SHOW, $mainGUI)
; Handle main GUI events
While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
ExitLoop
Case $btnSelectPage
$pageRect = SelectRect("PageArea")
Case $btnSelectButton
$buttonRect = SelectRect("ButtonArea")
Case $btnStartCapture
$pageCount = GUICtrlRead($txtPageCount)
$autoCrop = IsChecked($checkboxAutoCrop)
If Not StringIsInt($pageCount) Or $pageCount < 1 Then
MsgBox($MB_ICONERROR, "Invalid Input", "Please enter a valid number of pages.")
Else
; Clear the overlay and DCs
_WinAPI_ReleaseDC($hOverlay, $hOverlayDC)
GUIDelete($hOverlay)
$hOverlay = 0
_WinAPI_ReleaseDC($hPageRectOverlay, $hPageRectOverlayDC)
GUIDelete($hPageRectOverlay)
$hPageRectOverlay = 0
_WinAPI_ReleaseDC($hButtonRectOverlay, $hButtonRectOverlayDC)
GUIDelete($hButtonRectOverlay)
$hButtonRectOverlay = 0
StartCapture()
EndIf
EndSwitch
WEnd
EndFunc
Func IsChecked($idControlID)
Return BitAND(GUICtrlRead($idControlID), $GUI_CHECKED) = $GUI_CHECKED
EndFunc ;==>_IsChecked
Func SelectRect($selctAreaName)
If $selctAreaName = "PageArea" And $hPageRectOverlay <> 0 Then
_WinAPI_ReleaseDC($hPageRectOverlay, $hPageRectOverlayDC)
GUIDelete($hPageRectOverlay)
EndIf
If $selctAreaName = "ButtonArea" And $hButtonRectOverlay <> 0 Then
_WinAPI_ReleaseDC($hButtonRectOverlay, $hButtonRectOverlayDC)
GUIDelete($hButtonRectOverlay)
EndIf
; Hide the main GUI temporarily
GUISetState(@SW_HIDE, $mainGUI)
If $hOverlay = 0 Then
; Create full-screen overlay for selection
$hOverlay = GUICreate("Overlay", @DesktopWidth * $screenScale, @DesktopHeight * $screenScale, 0, 0, $WS_POPUP, $WS_EX_LAYERED + $WS_EX_TOPMOST + $WS_EX_TOOLWINDOW)
GUISetBkColor(0x000000)
WinSetTrans("Overlay", "", 100) ; Set overlay transparency
GUISetState(@SW_SHOW)
$hOverlayDC = _WinAPI_GetDC($hOverlay)
Else
WinActivate("Overlay")
EndIf
; Initialize GDI+ for drawing
_GDIPlus_Startup()
; Load cross cursor
Local $hCursor = _WinAPI_LoadCursor(0, $OCR_CROSS)
Local $isDragging = False
Local $startX, $startY, $endX, $endY
; Continuously set cross cursor until selection is completed
While Not $isDragging
_WinAPI_SetCursor($hCursor)
; Wait for mouse click to start selection
If _IsPressed("01") Then
Local $startPos = MouseGetPos()
$startX = $startPos[0]
$startY = $startPos[1]
$isDragging = True
EndIf
Sleep(10)
WEnd
; Track and draw selection rectangle
While $isDragging
_WinAPI_SetCursor($hCursor) ; Reinforce cross cursor during selection
Local $currentPos = MouseGetPos()
Local $currentX = $currentPos[0]
Local $currentY = $currentPos[1]
; Clear previous drawings
_WinAPI_RedrawWindow($hOverlay, 0, 0, $RDW_INVALIDATE + $RDW_UPDATENOW + $RDW_ERASE)
; Draw the rubber-band rectangle
If Abs($currentX - $startX) > 1 Or Abs($currentY - $startY) > 1 Then
DrawRectangle($hOverlayDC, $startX, $startY, $currentX, $currentY)
EndIf
; Finalize selection on mouse release
If Not _IsPressed("01") Then
$endX = $currentX
$endY = $currentY
$isDragging = False
EndIf
Sleep(10)
WEnd
; Cleanup
_WinAPI_SetCursor(0) ; Reset cursor to default
_GDIPlus_Shutdown()
; Hide the overlay window
GUISetState(@SW_MINIMIZE, $hOverlay)
; Show the main GUI again
GUISetState(@SW_SHOW, $mainGUI)
; Calculate and return the rectangle area
Local $rectX = Min($startX, $endX)
Local $rectY = Min($startY, $endY)
Local $rectWidth = Abs($endX - $startX)
Local $rectHeight = Abs($endY - $startY)
; Create smaller overlay for the selected rectangle area
Local $hSelectedOverlay = GUICreate($selctAreaName, $rectWidth, $rectHeight, $rectX, $rectY, $WS_POPUP, $WS_EX_LAYERED + $WS_EX_TOPMOST + $WS_EX_TOOLWINDOW)
GUISetBkColor(0x000000)
WinSetTrans($selctAreaName, "", 100)
GUISetState(@SW_SHOW, $hSelectedOverlay)
; Draw the selected rectangle on the smaller overlay
Local $hSelectedOverlayDC = _WinAPI_GetDC($hSelectedOverlay)
; Assign the UI handle and DC handle from local to global
If $selctAreaName = "PageArea" Then
$hPageRectOverlay = $hSelectedOverlay
$hPageRectOverlayDC = $hSelectedOverlayDC
ElseIf $selctAreaName = "ButtonArea" Then
$hButtonRectOverlay = $hSelectedOverlay
$hButtonRectOverlayDC = $hSelectedOverlayDC
EndIf
DrawRectangle($hSelectedOverlayDC, 0, 0, $rectWidth, $rectHeight) ; Adjust coordinates to overlay's size
; Define and return array explicitly
Local $rectArray[4]
$rectArray[0] = $rectX
$rectArray[1] = $rectY
$rectArray[2] = $rectWidth
$rectArray[3] = $rectHeight
Return $rectArray
EndFunc
Func StartCapture()
_GDIPlus_Startup()
Local $pageImages[$pageCount]
$numCroppedImage = 0
$avgCroppedWidth = 0
$avgCroppedHeight = 0
For $i = 1 To $pageCount
Local $imageName = "Page_" & StringFormat("%03d", $i) & ".png"
CaptureAndSaveImage($pageRect[0], $pageRect[1], $pageRect[2], $pageRect[3], $imageName)
Sleep(50)
If $autoCrop = 1 Then
Local $croppedImageName = CropImage($imageName)
$pageImages[$i - 1] = $croppedImageName
Else
$pageImages[$i - 1] = $imageName
EndIf
; Click the turn-page button
MouseClick("left", $buttonRect[0] + ($buttonRect[2] / 2), $buttonRect[1] + ($buttonRect[3] / 2), 1, 10)
Sleep(1000)
Next
; Convert cropped images to pdf
Local $pdfFileName = @YEAR & "-" & @MON & "-" & @MDAY & "_" & @HOUR & "_" & @MIN & "_" & @SEC & "_output.pdf"
Local $pdfCreated = CombineImagesToPDF($pageImages, $pdfFileName)
; Delete the crooped images file
If $pdfCreated = True Then
For $i = 0 To UBound($pageImages) - 1
FileDelete($pageImages[$i])
Next
EndIf
_GDIPlus_Shutdown()
EndFunc
Func CombineImagesToPDF($aImages, $sOutputPDF)
; Build the ImageMagick command string
Local $sCommand = "magick "
; Append each image path to the command string
For $i = 0 To UBound($aImages) - 1
$sCommand &= '"' & @ScriptDir & "\" & $aImages[$i] & '" '
Next
; Append the output PDF path to the command
$sCommand &= '"' & @ScriptDir & "\" & $sOutputPDF & '"'
ConsoleWrite($sCommand)
; Run the command to generate the PDF
RunWait(@ComSpec & " /c " & $sCommand, "", @SW_HIDE )
; Check if the PDF was created successfully
If FileExists($sOutputPDF) Then
MsgBox(0, "Success", "PDF created successfully: " & $sOutputPDF)
Return True
Else
MsgBox(16, "Error", "Failed to create PDF.")
Return False
EndIf
EndFunc
Func DrawRectangle($hDC, $x1, $y1, $x2, $y2)
Local $left = Min($x1, $x2)
Local $top = Min($y1, $y2)
Local $right = Max($x1, $x2)
Local $bottom = Max($y1, $y2)
Local $hGraphics = _GDIPlus_GraphicsCreateFromHDC($hDC)
Local $hPen = _GDIPlus_PenCreate(0x7FFF0000, 2)
_GDIPlus_GraphicsDrawRect($hGraphics, $left, $top, $right - $left, $bottom - $top, $hPen)
_GDIPlus_PenDispose($hPen)
_GDIPlus_GraphicsDispose($hGraphics)
EndFunc
Func CaptureAndSaveImage($x, $y, $width, $height, $filename)
$x *= $screenScale
$y *= $screenScale
$width *= $screenScale
$height *= $screenScale
Local $hBitmap = _WinAPI_CreateCompatibleBitmap(_WinAPI_GetDC(0), $width, $height)
Local $hDCMem = _WinAPI_CreateCompatibleDC(_WinAPI_GetDC(0))
Local $hOld = _WinAPI_SelectObject($hDCMem, $hBitmap)
_WinAPI_BitBlt($hDCMem, 0, 0, $width, $height, _WinAPI_GetDC(0), $x, $y, $SRCCOPY)
Local $hBitmapGDI = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap)
_GDIPlus_ImageSaveToFile($hBitmapGDI, $filename)
_WinAPI_SelectObject($hDCMem, $hOld)
_WinAPI_DeleteObject($hBitmap)
_WinAPI_DeleteDC($hDCMem)
_GDIPlus_ImageDispose($hBitmapGDI)
EndFunc
Func Min($val1, $val2)
Return ($val1 < $val2) ? $val1 : $val2
EndFunc
Func Max($val1, $val2)
Return ($val1 > $val2) ? $val1 : $val2
EndFunc
Func ConvertColorToGrey($color)
Local $iR = BitShift(BitAND($color, 0x00FF0000), 16) ;extract red color channel
Local $iG = BitShift(BitAND($color, 0x0000FF00), 8) ;extract green color channel
Local $iB = BitAND($color, 0x000000FF) ;;extract blue color channel
Local $iGrey = ($iR + $iG + $iB) / 3 ;convert pixels to average greyscale color format
return $iGrey;
EndFunc
Func CropImage($filePath)
_GDIPlus_Startup()
Local $hImage = _GDIPlus_ImageLoadFromFile($filePath)
Local $width = _GDIPlus_ImageGetWidth($hImage)
Local $height = _GDIPlus_ImageGetHeight($hImage)
Local $hBitmap = _GDIPlus_BitmapCloneArea($hImage, 0, 0, $width, $height, $GDIP_PXF32ARGB)
; Calculate the average color with larger step size to speed up
Local $totalR = 0, $totalG = 0, $totalB = 0, $pixelCount = 0, $totalGrey = 0
For $y = 0 To $height - 1 Step 1
Local $color = _GDIPlus_BitmapGetPixel($hBitmap, $width / 2, $y)
$totalGrey += ConvertColorToGrey($color)
$pixelCount += 1
Next
Local $avgGrey = $totalGrey / $pixelCount
Local $threshold = $avgGrey / 3 ; Adjust sensitivity
ConsoleWrite("$avgGrey: " & $avgGrey & @CRLF);
ConsoleWrite("$threshold: " & $threshold & @CRLF);
; Initialize crop boundaries
Local $left = 0, $top = 0, $right = $width - 1, $bottom = $height - 1
Local $color, $delta
Local $step = 2
Local $margin = 10
; Find boundaries with larger steps
For $x = 0 To $width - 1 Step $step
For $y = 0 To $height - 1 Step $step
$color = _GDIPlus_BitmapGetPixel($hBitmap, $x, $y)
Local $thisGrey = ConvertColorToGrey($color);
If $thisGrey < $threshold Then
$left = $x - $margin
ExitLoop 2
EndIf
Next
Next
For $x = $width - 1 To 0 Step - $step
For $y = 0 To $height - 1 Step $step
$color = _GDIPlus_BitmapGetPixel($hBitmap, $x, $y)
Local $thisGrey = ConvertColorToGrey($color);
If $thisGrey < $threshold Then
$right = $x + $margin
ExitLoop 2
EndIf
Next
Next
For $y = 0 To $height - 1 Step $step
For $x = 0 To $width - 1 Step $step
$color = _GDIPlus_BitmapGetPixel($hBitmap, $x, $y)
Local $thisGrey = ConvertColorToGrey($color);
If $thisGrey < $threshold Then
$top = $y - $margin
ExitLoop 2
EndIf
Next
Next
For $y = $height - 1 To 0 Step - $step
For $x = 0 To $width - 1 Step $step
$color = _GDIPlus_BitmapGetPixel($hBitmap, $x, $y)
Local $thisGrey = ConvertColorToGrey($color);
If $thisGrey < $threshold Then
$bottom = $y + $margin
ExitLoop 2
EndIf
Next
Next
; Calculate new cropped dimensions
Local $cropWidth = $right - $left + 1
Local $cropHeight = $bottom - $top + 1
; Adjust the crop width and height if they are abnormally small
; Usually this happens at the last page
Local $portion = 0.5
If ($cropWidth < $avgCroppedWidth * $portion) Then
$cropWidth = $avgCroppedWidth
EndIf
If ($cropHeight < $avgCroppedHeight * $portion) Then
$cropHeight = $avgCroppedHeight
EndIf
$avgCroppedWidth = ($avgCroppedWidth * $numCroppedImage + $cropWidth) / ($numCroppedImage + 1)
$avgCroppedHeight = ($avgCroppedHeight * $numCroppedImage + $cropHeight) / ($numCroppedImage + 1)
$numCroppedImage += 1
; Create the cropped bitmap
Local $hCroppedBitmap = _GDIPlus_BitmapCloneArea($hBitmap, $left, $top, $cropWidth, $cropHeight, $GDIP_PXF32ARGB)
; Save cropped image
Local $newFilePath = StringReplace($filePath, ".png", "_cropped.png")
_GDIPlus_ImageSaveToFile($hCroppedBitmap, $newFilePath)
ConsoleWrite("Cropped image saved to: " & $newFilePath & @CRLF)
; Cleanup
_GDIPlus_BitmapDispose($hCroppedBitmap)
_GDIPlus_BitmapDispose($hBitmap)
_GDIPlus_ImageDispose($hImage)
_GDIPlus_Shutdown()
; Delete original image
FileDelete($filePath)
ConsoleWrite("Original image deleted: " & $filePath & @CRLF)
Return $newFilePath
EndFunc
; Start the program
Main()