Jump to content

Recommended Posts

Posted (edited)

In Autoit forums I often learn GDI+ from UEZ articles ,thank you very much for your selfless sharing,here let me share my image deskew crop program. (It contains methods shared by many people. )

 

#include <GDIPlus.au3>
#include <Array.au3>
#include <File.au3>
#include <Date.au3>
#include <Math.au3>
#include <Process.au3>
#include <WinAPIDiag.au3>
Global $nPI = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
Global $pixelcolorvaule = 80, $sinpa, $cospa, $iResultx
If $CmdLine[0] = 0 Then
    Local $hTimer = TimerInit()
    Example(@ScriptDir&"\0.jpg")
    Example(@ScriptDir&"\1.jpg")
    Example(@ScriptDir&"\2.jpg")
    Example(@ScriptDir&"\3.jpg")
    Example(@ScriptDir&"\4.jpg")
    Example(@ScriptDir&"\5.jpg")
    Example(@ScriptDir&"\6.jpg")
    Example(@ScriptDir&"\7.jpg")
    Example(@ScriptDir&"\8.jpg")
    Example(@ScriptDir&"\9.jpg")
    Example(@ScriptDir&"\10.jpg")
    Example(@ScriptDir&"\11.jpg")
    Example(@ScriptDir&"\12.jpg")
    Example(@ScriptDir&"\13.jpg")
    Example(@ScriptDir&"\14.jpg")
    Example(@ScriptDir&"\15.jpg")
    Local $fDiff = TimerDiff($hTimer)
    MsgBox($MB_SYSTEMMODAL, "Time Difference", $fDiff)
Else
    For $i = 1 To $CmdLine[0]
        Example($CmdLine[$i])
    Next
EndIf
Func Example($imagepathroate)
    Local $hImage2, $iWidth, $iHeight
    Global $sDrive = "", $sDir = "", $sFileName = "", $sExtension = ""
    Global $aPathSplit = _PathSplit($imagepathroate, $sDrive, $sDir, $sFileName, $sExtension) ;$sDrive  磁碟C:   $sDir 目錄\test\    $sFileName 檔名  $sExtension 副檔名.jpg
    Global $newfilepath = $sDrive & $sDir
    _GDIPlus_Startup()
    $hImage2 = _GDIPlus_ImageLoadFromFile($imagepathroate)
    $iWidth = _GDIPlus_ImageGetWidth($hImage2)
    $iHeight = _GDIPlus_ImageGetHeight($hImage2)
    Local $newhight
    Local $blackorwhite = 2
    Global $GC = _GDIPlus_GraphicsCreateFromHWND(_WinAPI_GetDesktopWindow())
    Global $vratio = 2
    Global $bw = 0
    If $vratio = 0 Then
        $vratio = 1
    EndIf
    If $vratio <> 1 Then
        $iWidth = Round($iWidth / $vratio)
        $iHeight = Round($iHeight / $vratio)
        $hImage23 = _GDIPlus_BitmapCreateFromGraphics($iWidth, $iHeight, $GC)
        $resizesmall3 = _GDIPlus_ImageGetGraphicsContext($hImage23)
        _GDIPlus_GraphicsSetPixelOffsetMode($resizesmall3, 4)
        _GDIPlus_GraphicsSetInterpolationMode($resizesmall3, 5)
        _GDIPlus_GraphicsDrawImageRect($resizesmall3, $hImage2, 0, 0, $iWidth, $iHeight)
        $hImage2 = $hImage23
        $hImage23 = 0
    EndIf
    Local $iPixels = $iWidth * $iHeight
    Local $tBitmapData = _GDIPlus_BitmapLockBits($hImage2, 0, 0, $iWidth, $iHeight, $GDIP_ILMREAD, $GDIP_PXF32RGB)
    Local $pScan = $tBitmapData.Scan0
    Local $tCodeBuffer = DllStructCreate("byte ASM[60]")
    $tCodeBuffer.ASM = "0xb8000000008b7424048b4c240c8b1689d381e3ff00000001d889d381e300ff0000c1eb0801d889d381e30000ff00c1eb1001d883c60483e90177d2c3"
    Local $aRet = DllCallAddress("int:cdecl", DllStructGetPtr($tCodeBuffer), _
            "ptr", $pScan, _
            "ptr", Null, _
            "uint", $iPixels, _
            "int", Null)
    _GDIPlus_BitmapUnlockBits($hImage2, $tBitmapData)
    Local $iResult = Int(($aRet[0]) / $iPixels / 3)
    $iResultx = $iResult
    If $iResult < 30 Then
        Global $iResult0 = 1
        Global $iResult2 = 0
        Global $iResult3 = 2500
        Global $iResult4 = 1
        Global $iResult6 = 0
        Global $iResult7 = 0
    ElseIf $iResult < 46 Then
        Global $iResult0 = 0
        Global $iResult2 = 1500
        Global $iResult3 = 2500
        Global $iResult4 = 3
        Global $iResult6 = 0
        Global $iResult7 = 0
    ElseIf $iResult < 65 Then
        Global $iResult0 = 1
        Global $iResult2 = 10000
        Global $iResult3 = 65000
        Global $iResult6 = 4000
        Global $iResult7 = 100
        If $vratio <> 1 Then
            Global $iResult4 = 5
        Else
            Global $iResult4 = 12
        EndIf
    Else
        Global $iResult0 = 0
        Global $iResult2 = 4500
        Global $iResult3 = 2500
        Global $iResult6 = 0
        Global $iResult7 = 15
        If $vratio <> 1 Then
            Global $iResult4 = 5
        Else
            Global $iResult4 = 10
        EndIf
    EndIf
    $iW = Int($iWidth / $iResult4)
    $iH = Int($iHeight / $iResult4)
    ;$hImage2kbna = _GDIPlus_BitmapBlur2($hImage2, $iW, $iH, 0.999)
    $hImage2kbna = _GDIPlus_BitmapBlur_new($hImage2, $iWidth,$iHeight, $iW, $iH)
    If $iResultx < 158 And $iResultx > 13 Then
        $hEffect = _GDIPlus_EffectCreateLevels(99, 100, $iResult7)
        _GDIPlus_BitmapApplyEffect($hImage2kbna, $hEffect)
    EndIf
    $testh = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage2kbna)
    $tAdjust = _WinAPI_CreateColorAdjustment($iResult0, 0, $iResult3, $iResult3, $iResult3, $iResult6, $iResult2, 0, 0, -100, 0)
    $hBitmap = _WinAPI_AdjustBitmap($testh, -1, -1, 4, $tAdjust)
    $hImage1234 = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap)
    $hImage12345 = _GDIPlus_BitmapCloneArea($hImage1234, 0, 0, $iW, $iH, $GDIP_PXF01INDEXED)
    Local $tBitmapData = _GDIPlus_BitmapLockBits($hImage12345, 0, 0, $iW, $iH, $GDIP_ILMREAD, $GDIP_PXF01INDEXED)
    Local $iScan0 = DllStructGetData($tBitmapData, "Scan0")
    Local $iStride = DllStructGetData($tBitmapData, "Stride")
    Local $tPixel = DllStructCreate("byte[" & $iStride * $iH & "];", $iScan0)
    Local $ap_t_x_l, $ap_t_y_l, $ap_t_x_r, $ap_t_y_r, $ap_b_x, $ap_b_y, $ap_l_x, $ap_l_y, $ap_r_x, $ap_r_y
    Dim $recordps[7]
    For $iY = 0 To $iH - 2 Step 1
        $iRowOffset = ($iY * $iStride)
        For $iX = 1 To $iW Step 8
            $iPixel = DllStructGetData($tPixel, 1, $iRowOffset + Ceiling($iX / 8))
            If $iPixel <> 0 Then
                For $iXX = $iX To $iX + 7
                    Local $input_number = Mod($iXX, 8)
                    If $input_number = 0 Then
                        $input_number = 8
                    EndIf
                    $byte = BitShift(Binary("0x" & Hex($iPixel, 2)), 8 - $input_number)
                    $bits = BitAND($byte, 1)
                    If $bits = 1 Then
                        $ap_t_x_l = $iXX - 1
                        $ap_t_y_l = $iY
                        $recordps[0] = $iXX - 1
                        ExitLoop 3
                    EndIf
                Next
            EndIf
        Next
    Next
    For $iY = 0 To $iH - 2 Step 1
        $iRowOffset = ($iY * $iStride)
        For $iX = $iW - Mod($iW, 8) + 1 - 8 To 1 Step -8
            $iPixel = DllStructGetData($tPixel, 1, $iRowOffset + Ceiling($iX / 8))
            If $iPixel <> 0 Then
                For $iXX = $iX + 7 To $iX Step -1
                    Local $input_number = Mod($iXX, 8)
                    If $input_number = 0 Then
                        $input_number = 8
                    EndIf
                    $byte = BitShift(Binary("0x" & Hex($iPixel, 2)), 8 - $input_number)
                    $bits = BitAND($byte, 1)
                    If $bits = 1 Then
                        $ap_t_x_r = $iXX - 1
                        $ap_t_y_r = $iY
                        $recordps[1] = $iW - $ap_t_x_r
                        ExitLoop 3
                    EndIf
                Next
            EndIf
        Next
    Next
    For $iY = $iH - 2 To 0 Step -1
        $iRowOffset = ($iY * $iStride)
        For $iX = $iW - Mod($iW, 8) + 1 - 8 To 1 Step -8
            $iPixel = DllStructGetData($tPixel, 1, $iRowOffset + Ceiling($iX / 8))
            If $iPixel <> 0 Then
                For $iXX = $iX + 7 To $iX Step -1
                    Local $input_number = Mod($iXX, 8)
                    If $input_number = 0 Then
                        $input_number = 8
                    EndIf
                    $byte = BitShift(Binary("0x" & Hex($iPixel, 2)), 8 - $input_number)
                    $bits = BitAND($byte, 1)
                    If $bits = 1 Then
                        $ap_b_x = $iXX - 1
                        $ap_b_y = $iY
                        ExitLoop 3
                    EndIf
                Next
            EndIf
        Next
    Next
    _GDIPlus_BitmapUnlockBits($hImage12345, $tBitmapData)
    _GDIPlus_ImageRotateFlip($hImage12345, 1)
    Local $tBitmapData = _GDIPlus_BitmapLockBits($hImage12345, 0, 0, $iH, $iW, $GDIP_ILMREAD, $GDIP_PXF01INDEXED)
    Local $iScan0 = DllStructGetData($tBitmapData, "Scan0")
    Local $iStride = DllStructGetData($tBitmapData, "Stride")
    Local $tPixel = DllStructCreate("byte[" & $iStride * $iW & "];", $iScan0)
    For $iY = 0 To $iW - 2 Step 1
        $iRowOffset = ($iY * $iStride)
        For $iX = $iH - Mod($iH, 8) + 1 - 8 To 1 Step -8
            $iPixel = DllStructGetData($tPixel, 1, $iRowOffset + Ceiling($iX / 8))
            If $iPixel <> 0 Then
                For $iXX = $iX + 7 To $iX Step -1
                    Local $input_number = Mod($iXX, 8)
                    If $input_number = 0 Then
                        $input_number = 8
                    EndIf
                    $byte = BitShift(Binary("0x" & Hex($iPixel, 2)), 8 - $input_number)
                    $bits = BitAND($byte, 1)
                    If $bits = 1 Then
                        $ap_l_x = $iY
                        $ap_l_y = $iH - $iXX
                        $recordps[6] = $ap_l_y
                        ExitLoop 3
                    EndIf
                Next
            EndIf
        Next
    Next
    For $iY = $iW - 2 To 0 Step -1
        $iRowOffset = ($iY * $iStride)
        For $iX = $iH - Mod($iH, 8) + 1 - 8 To 1 Step -8
            $iPixel = DllStructGetData($tPixel, 1, $iRowOffset + Ceiling($iX / 8))
            If $iPixel <> 0 Then
                For $iXX = $iX + 7 To $iX Step -1
                    Local $input_number = Mod($iXX, 8)
                    If $input_number = 0 Then
                        $input_number = 8
                    EndIf
                    $byte = BitShift(Binary("0x" & Hex($iPixel, 2)), 8 - $input_number)
                    $bits = BitAND($byte, 1)
                    If $bits = 1 Then
                        $ap_r_x = $iY
                        $ap_r_y = $iH - $iXX
                        $recordps[5] = $ap_r_y
                        ExitLoop 3
                    EndIf
                Next
            EndIf
        Next
    Next
    _GDIPlus_BitmapUnlockBits($hImage12345, $tBitmapData)
    $newwidthl = $ap_l_x
    $newhight = $ap_t_y_l
    $newwidthr = ($iW - $ap_r_x) - 2
    $newhightd = ($iH - $ap_b_y) - 2
    $iH = $iH - $newhight - $newhightd
    $iW = $iW - $newwidthr - $newwidthl
    $recordps[0] = $recordps[0] - $newwidthl
    $recordps[1] = $recordps[1] - $newwidthr - 1
    $recordps[5] = $recordps[5] - $newhight
    $recordps[6] = $recordps[6] - $newhight
    If $recordps[0] >= $recordps[1] Then
        $recordps[2] = $recordps[0] - 1
        $recordps[3] = $recordps[6] - 1
        $recordps[4] = 90 - _Degree(ATan($recordps[2] / $recordps[3]))
        If $recordps[2] <= 0 Or $recordps[2] = "" Then
            $recordps[4] = 0
        EndIf
        If $recordps[3] = 0 Or $recordps[3] = "" Then
            $recordps[4] = 0
        EndIf
        If $recordps[4] <> 0 Then
            setdegree($recordps[4], $iHeight, $iWidth, $hImage2, $blackorwhite, $iResult4, $newwidthl, $newhight, $iW, $iH)
        EndIf
        If $recordps[4] = "" Then
            setdegree($recordps[4], $iHeight, $iWidth, $hImage2, $blackorwhite, $iResult4, $newwidthl, $newhight, $iW, $iH)
        EndIf
        Return 1
    ElseIf $recordps[0] < $recordps[1] Then
        $recordps[2] = $recordps[1]
        $recordps[3] = $recordps[5]
        $recordps[4] = _Degree(ATan($recordps[2] / $recordps[3])) + 270
        If $recordps[2] <= 0 Or $recordps[2] = "" Then
            $recordps[4] = 0
        EndIf
        If $recordps[3] <= 0 Or $recordps[3] = "" Then
            $recordps[4] = 0
        EndIf
        If $recordps[4] <> 0 Then
            setdegree($recordps[4], $iHeight, $iWidth, $hImage2, $blackorwhite, $iResult4, $newwidthl, $newhight, $iW, $iH)
        EndIf

        If $recordps[4] = "" Then
            setdegree($recordps[4], $iHeight, $iWidth, $hImage2, $blackorwhite, $iResult4, $newwidthl, $newhight, $iW, $iH)
        EndIf
        Return -1
    ElseIf $recordps[0] = "" And "" = $recordps[1] Then
        setdegree($recordps[4], $iHeight, $iWidth, $hImage2, $blackorwhite, $iResult4, $newwidthl, $newhight, $iW, $iH)
        Return 0
    ElseIf $recordps[0] <> "" And "" <> $recordps[1] And $recordps[0] = $recordps[1] Then
        setdegree($recordps[4], $iHeight, $iWidth, $hImage2, $blackorwhite, $iResult4, $newwidthl, $newhight, $iW, $iH)
    EndIf
EndFunc
Func DrawInsert($hGraphic, $hImage2x, $iX, $iY, $fAngle, $iWidth, $iHeight, $iARGB = 0xFF000000, $iPenWidth = 1)
    Local $hMatrix, $hPen2, $nXt, $nYt
    $hMatrix = _GDIPlus_MatrixCreate()
    _GDIPlus_MatrixRotate($hMatrix, Round($fAngle), "False")
    _GDIPlus_GraphicsSetTransform($hGraphic, $hMatrix)
    $nXt = $iX * $cospa + $iY * $sinpa
    $nYt = -$iX * $sinpa + $iY * $cospa
    _GDIPlus_GraphicsDrawImage($hGraphic, $hImage2x, $nXt, $nYt)
    _GDIPlus_MatrixDispose($hMatrix)
EndFunc
Func _CreateBMP($sWidth, $sHeight)
    Local $hWnd = _WinAPI_GetDesktopWindow()
    Local $hDC = _WinAPI_GetDC($hWnd)
    Local $hBMP = _WinAPI_CreateCompatibleBitmap($hDC, $sWidth, $sHeight)
    _WinAPI_ReleaseDC($hWnd, $hDC)
    Return $hBMP
EndFunc
Func _GDIPlus_BitmapConvertTo8Bit(ByRef $hBitmap, $iDitherType = $GDIP_DitherTypeDualSpiral8x8, $iPaletteType = $GDIP_PaletteTypeFixedHalftone252, $iColorCount = 253, $bUseTransparentColor = True)
    Local $tPalette = _GDIPlus_PaletteInitialize($iColorCount, $iPaletteType, 0, $bUseTransparentColor, $hBitmap)
    Local $iRet = _GDIPlus_BitmapConvertFormat($hBitmap, $GDIP_PXF08INDEXED, $iDitherType, $iPaletteType, $tPalette)
    Return SetError(@error, @extended, $iRet)
EndFunc
Func setdegree($degreespa, $iHeight, $iWidth, $hImage2, $blackorwhite, $ratio, $Xl, $Xt, $Yr, $Yb)
    If $ratio <> 1 Then
        $ADW = 8 * $vratio
        $ADH = 1 * $vratio
        $iWidth = Round($Yr * ($ratio + 0.07))
        $iHeight = Round($Yb * ($ratio + 0.07))
        $ofxin = Round($Xl * ($ratio * 0.991))
        $ofyin = Round($Xt * ($ratio * 0.995))
    Else
        $ofxin = $Xl
        $ofyin = $Xt
    EndIf
    $hImage2k = _GDIPlus_BitmapCreateFromGraphics($iWidth, $iHeight, $GC)
    $hCtxt = _GDIPlus_ImageGetGraphicsContext($hImage2k)
    _GDIPlus_GraphicsSetPixelOffsetMode($hCtxt, 4)
    _GDIPlus_GraphicsSetInterpolationMode($hCtxt, 5)
    _GDIPlus_GraphicsDrawImageRectRect($hCtxt, $hImage2, $ofxin, $ofyin, $iWidth, $iHeight, 0, 0, $iWidth, $iHeight)
    Local $hBitmap1, $hImage1, $hGraphic, $noww = 0, $nowh = 0
    If $degreespa <> 0 And $degreespa <> 360 Then
        $sinpa = Sin($degreespa * $nPI / 180)
        $cospa = Cos($degreespa * $nPI / 180)
        If $degreespa >= 0 And $degreespa <= 90 Then
            $noww = $iHeight * $sinpa
            $noww2 = $iWidth * $cospa
            $nowws = $noww + $noww2
            $nowh2 = $iHeight * $cospa
            $nowh3 = $iWidth * $sinpa
            $nowhs = $nowh2 + $nowh3
        EndIf
        If $degreespa > 90 And $degreespa <= 180 Then
            $noww = $iHeight * $sinpa - $iWidth * $cospa
            $nowh = -$iHeight * $cospa
            $noww1 = -$iWidth * $cospa
            $noww2 = $iHeight * $sinpa
            $nowws = $noww2 + $noww1
            $nowh1 = $iWidth * $sinpa
            $nowhs = $nowh + $nowh1
        EndIf
        If $degreespa > 180 And $degreespa <= 270 Then
            $noww = -$iWidth * $cospa
            $nowh = -$iHeight * $cospa - $iWidth * $sinpa
            $noww1 = -$iHeight * $sinpa
            $nowws = $noww1 + $noww
            $nowh4 = -$iHeight * $cospa
            $nowh3 = -$iWidth * $sinpa
            $nowhs = $nowh3 + $nowh4
        EndIf
        If $degreespa > 270 And $degreespa <= 360 Then
            $nowh = -$iWidth * $sinpa
            $noww1 = $iWidth * $cospa
            $noww2 = -$iHeight * $sinpa
            $nowws = $noww1 + $noww2
            $nowh1 = $iHeight * $cospa
            $nowhs = $nowh + $nowh1
        EndIf
        $nowws = Round($nowws)
        $nowhs = Round($nowhs)
        $hImage1 = _GDIPlus_BitmapCreateFromGraphics($nowws, $nowhs, $GC)
        $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage1)
        _GDIPlus_GraphicsSetPixelOffsetMode($hGraphic, 4)
        _GDIPlus_GraphicsSetInterpolationMode($hGraphic, 5)
        DrawInsert($hGraphic, $hImage2k, $noww, $nowh, $degreespa, $nowws, $nowhs)
    Else
        $hImage1 = $hImage2k
        $nowws = $iWidth
        $nowhs = $iHeight
    EndIf
    If $iResultx < 46 Then
        $iW = Round($nowws / 1)
        $iH = Round($nowhs / 1)
    Else
        If $vratio <> 1 Then
            $iW = Round($nowws / 6)
            $iH = Round($nowhs / 6)
        Else
            $iW = Round($nowws / 15)
            $iH = Round($nowhs / 15)
        EndIf
    EndIf
    $hImage1bb = _GDIPlus_BitmapCreateFromGraphics($iW, $iH, $GC)
    $resizesmall = _GDIPlus_ImageGetGraphicsContext($hImage1bb)
    _GDIPlus_GraphicsSetPixelOffsetMode($resizesmall, 4)
    _GDIPlus_GraphicsSetInterpolationMode($resizesmall, 5)
    _GDIPlus_GraphicsDrawImageRect($resizesmall, $hImage1, 0, 0, $iW, $iH)
    If $iResultx < 158 And $iResultx > 58 Then
        $hEffect = _GDIPlus_EffectCreateLevels(10, 0, 0)
        _GDIPlus_BitmapApplyEffect($hImage1bb, $hEffect)
    ElseIf $iResultx <= 58 And $iResultx > 13 Then
        $hEffect = _GDIPlus_EffectCreateLevels(20, 0, 0)
        _GDIPlus_BitmapApplyEffect($hImage1bb, $hEffect)
    ElseIf $iResultx <= 13 Then
        $hEffect = _GDIPlus_EffectCreateLevels(1, -100, 2)
        _GDIPlus_BitmapApplyEffect($hImage1bb, $hEffect)
    Else
        $hEffect = _GDIPlus_EffectCreateLevels(85, 90, 0)
        _GDIPlus_BitmapApplyEffect($hImage1bb, $hEffect)
    EndIf
    $hImage2kbn2 = _GDIPlus_BitmapCreateFromGraphics($nowws, $nowhs, $GC)
    $resizesmall2 = _GDIPlus_ImageGetGraphicsContext($hImage2kbn2)
    _GDIPlus_GraphicsSetPixelOffsetMode($resizesmall2, 4)
    _GDIPlus_GraphicsSetInterpolationMode($resizesmall2, 5)
    _GDIPlus_GraphicsDrawImageRect($resizesmall2, $hImage1bb, 0, 0, $nowws, $nowhs)
    Local $tBitmapData = _GDIPlus_BitmapLockBits($hImage2kbn2, 0, 0, $nowws, $nowhs, $GDIP_ILMREAD, $GDIP_PXF32ARGB)
    Local $iScan0 = DllStructGetData($tBitmapData, "Scan0")
    Local $tPixel = DllStructCreate("int[" & $nowws * $nowhs & "];", $iScan0)
    If $blackorwhite = 1 Then
        For $kkk = 2 To 4
            $iY = Round($nowhs / $kkk)
            $iRowOffset = $iY * $nowws + 1
            For $iX = 0 To $nowws - 1
                $iPixel = DllStructGetData($tPixel, 1, $iRowOffset + $iX)
                $B = BitAND($iPixel, 0xFF)
                If $B > 250 Then
                    $offsetwidthl = $iX
                    ExitLoop 2
                EndIf
            Next
        Next
        If IsDeclared("offsetwidthl") = 0 Then
            MsgBox(0, "", "Not found")
            Exit
        EndIf
        For $kkk = 2 To 4
            $iY = Round($nowhs / $kkk)
            $iRowOffset = $iY * $nowws + 1
            For $iX = $nowws - 1 To 0 Step -1
                $iPixel = DllStructGetData($tPixel, 1, $iRowOffset + $iX)
                $B = BitAND($iPixel, 0xFF)
                If $B > 90 Then
                    $offsetwidthr = $iX - $offsetwidthl + 1
                    ExitLoop 2
                EndIf
            Next
        Next
        For $kkk = 2 To 4
            $iX = Round($nowws / $kkk)
            For $iY = 0 To $nowhs - 1
                $iRowOffset = $iY * $nowws + 1
                $iPixel = DllStructGetData($tPixel, 1, $iRowOffset + $iX)
                $B = BitAND($iPixel, 0xFF)
                If $B > 250 Then
                    $offsetHightt = $iY
                    ExitLoop 2
                EndIf
            Next
        Next
        If IsDeclared("offsetHightt") = 0 Then
            MsgBox(0, "", "Not found")
            Exit
        EndIf
        For $kkk = 2 To 4
            $iX = Round($nowws / $kkk)
            For $iY = $nowhs - 1 To 0 Step -1
                $iRowOffset = $iY * $nowws + 1
                $iPixel = DllStructGetData($tPixel, 1, $iRowOffset + $iX)
                $B = BitAND($iPixel, 0xFF)
                If $B > 90 Then
                    $offsetHightb = $iY - $offsetHightt + 1
                    ExitLoop 2
                EndIf
            Next
        Next
    Else
        For $kkk = 2 To 4
            $iY = Round($nowhs / $kkk)
            $iRowOffset = $iY * $nowws + 1
            For $iX = 0 To $nowws - 1 Step 4
                $iPixel = DllStructGetData($tPixel, 1, $iRowOffset + $iX)
                If $iPixel = -1 Then
                    $offsetwidthl = $iX
                    ExitLoop 2
                EndIf
            Next
        Next
        If IsDeclared("offsetwidthl") = 0 Then
            MsgBox(0, "", "Not found")
            Exit
        EndIf
        For $kkk = 2 To 4
            $iY = Round($nowhs / $kkk)
            $iRowOffset = $iY * $nowws + 1
            For $iX = $nowws - 1 To 0 Step -4
                $iPixel = DllStructGetData($tPixel, 1, $iRowOffset + $iX)
                If $iPixel = -1 Then
                    $offsetwidthr = $iX - $offsetwidthl - 2
                    ExitLoop 2
                EndIf
            Next
        Next
        For $kkk = 2.2 To 4 Step 1.2
            $iX = Round($nowws / $kkk)
            For $iY = 0 To $nowhs - 1 Step 4
                $iRowOffset = $iY * $nowws + 1
                $iPixel = DllStructGetData($tPixel, 1, $iRowOffset + $iX)
                If $iPixel = -1 Then
                    $offsetHightt = $iY
                    ExitLoop 2
                EndIf
            Next
        Next
        If IsDeclared("offsetHightt") = 0 Then
            MsgBox(0, "", "Not found")
            Exit
        EndIf
        For $kkk = 2 To 4 Step 1.2
            $iX = Round($nowws / $kkk)
            For $iY = $nowhs - 1 To 0 Step -4
                $iRowOffset = $iY * $nowws + 1
                $iPixel = DllStructGetData($tPixel, 1, $iRowOffset + $iX)
                If $iPixel = -1 Then
                    $offsetHightb = $iY - $offsetHightt
                    ExitLoop 2
                EndIf
            Next
        Next
    EndIf
    _GDIPlus_BitmapUnlockBits($hImage2kbn2, $tBitmapData)
    If $bw = 1 Then
        $hImage2kk = _GDIPlus_BitmapCreateFromScan0($offsetwidthr * $vratio, $offsetHightb * $vratio, $GDIP_PXF01INDEXED)
        $hCtxt = _GDIPlus_ImageGetGraphicsContext($hImage2kk)
        _GDIPlus_GraphicsSetPixelOffsetMode($hCtxt, 4)
        _GDIPlus_GraphicsSetInterpolationMode($hCtxt, 5)
        _GDIPlus_GraphicsDrawImageRectRect($hCtxt, $hImage1, $offsetwidthl, $offsetHightt, $offsetwidthr, $offsetHightb, 0, 0, $offsetwidthr * $vratio, $offsetHightb * $vratio)
    ElseIf $bw = 8 Then
        Local $aColorTable[256]
        For $i = 0 To 255 Step 1
            $aColorTable[$i] = _WinAPI_RGB($i, $i, $i)
        Next
        Local $tColorTable = _WinAPI_CreateDIBColorTable($aColorTable)
        $hImage2kkk = _WinAPI_CreateDIB($offsetwidthr * $vratio, $offsetHightb * $vratio, 8)
        _WinAPI_SetDIBColorTable($hImage2kkk, $tColorTable, 256)
        $hImage2kk = _GDIPlus_BitmapCreateFromHBITMAP($hImage2kkk)
        $hCtxt = _GDIPlus_ImageGetGraphicsContext($hImage2kk)
        _GDIPlus_GraphicsSetPixelOffsetMode($hCtxt, 4)
        _GDIPlus_GraphicsSetInterpolationMode($hCtxt, 5)
        _GDIPlus_GraphicsDrawImageRectRect($hCtxt, $hImage1, $offsetwidthl, $offsetHightt, $offsetwidthr, $offsetHightb, 0, 0, $offsetwidthr * $vratio, $offsetHightb * $vratio)
    ElseIf $vratio <> 1 Then
        $hImage2kk = _GDIPlus_BitmapCreateFromGraphics($offsetwidthr * $vratio, $offsetHightb * $vratio, $GC)
        $hCtxt = _GDIPlus_ImageGetGraphicsContext($hImage2kk)
        _GDIPlus_GraphicsSetPixelOffsetMode($hCtxt, 4)
        _GDIPlus_GraphicsSetInterpolationMode($hCtxt, 5)
        _GDIPlus_GraphicsDrawImageRectRect($hCtxt, $hImage1, $offsetwidthl, $offsetHightt, $offsetwidthr, $offsetHightb, 0, 0, $offsetwidthr * $vratio, $offsetHightb * $vratio)
        If $bw = 256 Then
            _GDIPlus_BitmapConvertTo8Bit($hImage2kk)
        EndIf
    Else
        $hImage2kk = _GDIPlus_BitmapCreateFromGraphics($offsetwidthr, $offsetHightb, $GC)
        $hCtxt = _GDIPlus_ImageGetGraphicsContext($hImage2kk)
        _GDIPlus_GraphicsSetPixelOffsetMode($hCtxt, 4)
        _GDIPlus_GraphicsSetInterpolationMode($hCtxt, 5)
        _GDIPlus_GraphicsDrawImageRectRect($hCtxt, $hImage1, $offsetwidthl, $offsetHightt, $offsetwidthr, $offsetHightb, 0, 0, $offsetwidthr, $offsetHightb);這是讓邊比較滑順的參數
        If $bw = 256 Then
            _GDIPlus_BitmapConvertTo8Bit($hImage2kk)
        EndIf
    EndIf
    _GDIPlus_ImageSaveToFile($hImage2kk, $newfilepath & $sFileName & "_ACD" & $sExtension)
EndFunc
Func _GDI_CreateDIBitmap($hDC, $lpbmih, $fdwInit, $lpbInit, $lpbmi, $fuUsage)
    Local $aResult = DllCall("Gdi32.dll", "ptr", "CreateDIBitmap", "ptr", $hDC, "ptr", $lpbmih, "DWORD", $fdwInit, "ptr", $lpbInit, "ptr", $lpbmi, "UINT", $fuUsage)
    If @error Then Return SetError(1, 0, 0)
    Return $aResult[0]
EndFunc
Func _GDIPlus_BitmapBlur($hBitmap, $iW, $iH, $fScale = 0.15, $qual = 5)
    Local Const $hBmpSmall = _GDIPlus_BitmapCreateFromGraphics($iW, $iH, $GC)
    Local Const $hGfxSmall = _GDIPlus_ImageGetGraphicsContext($hBmpSmall)
    _GDIPlus_GraphicsSetPixelOffsetMode($hGfxSmall, 4)
    Local Const $hBmpBig = _GDIPlus_BitmapCreateFromGraphics($iW, $iH, $GC)
    Local Const $hGfxBig = _GDIPlus_ImageGetGraphicsContext($hBmpBig)
    _GDIPlus_GraphicsSetPixelOffsetMode($hGfxBig, 4)
    _GDIPlus_GraphicsScaleTransform($hGfxSmall, $fScale, $fScale)
    _GDIPlus_GraphicsSetInterpolationMode($hGfxSmall, $qual)
    _GDIPlus_GraphicsScaleTransform($hGfxBig, 1 / $fScale, 1 / $fScale)
    _GDIPlus_GraphicsSetInterpolationMode($hGfxBig, $qual)
    _GDIPlus_GraphicsDrawImageRect($hGfxSmall, $hBitmap, 0, 0, $iW, $iH)
    _GDIPlus_GraphicsDrawImageRect($hGfxBig, $hBmpSmall, 0, 0, $iW, $iH)
    Return $hBmpBig
EndFunc
Func _GDIPlus_BitmapBlur2($hBitmap, $iW, $iH, $fScale = 0.15, $qual = 5, $nRatio = 5)
    Local Const $hBmpSmall = _GDIPlus_BitmapCreateFromGraphics($iW, $iH, $GC)
    Local Const $hGfxSmall = _GDIPlus_ImageGetGraphicsContext($hBmpSmall)
    Local Const $hBmpBig = _GDIPlus_BitmapCreateFromGraphics($iW, $iH, $GC)
    Local Const $hGfxBig = _GDIPlus_ImageGetGraphicsContext($hBmpBig)
    _GDIPlus_GraphicsSetPixelOffsetMode($hGfxSmall, 4)
    _GDIPlus_GraphicsScaleTransform($hGfxSmall, $fScale, $fScale)
    _GDIPlus_GraphicsSetInterpolationMode($hGfxSmall, $qual)
    _GDIPlus_GraphicsSetPixelOffsetMode($hGfxBig, 4)
    _GDIPlus_GraphicsScaleTransform($hGfxBig, 1 / $fScale, 1 / $fScale)
    _GDIPlus_GraphicsSetInterpolationMode($hGfxBig, $qual)
    _GDIPlus_GraphicsDrawImageRect($hGfxSmall, $hBitmap, 0, 0, $iW, $iH)
    _GDIPlus_GraphicsDrawImageRect($hGfxBig, $hBmpSmall, 0, 0, $iW, $iH)
    Return $hBmpBig
EndFunc
Func _GDIPlus_BitmapBlur_new($hImg, $iW1, $iH1, $iW2, $iH2, $iInterMode = 5, $iSmoothMode = 0, $iPixOffsetMode = 4)
    $hImg2 = _GDIPlus_BitmapCreateFromGraphics($iW2, $iH2, $GC)
    $hMemGraph = _GDIPlus_ImageGetGraphicsContext($hImg2)
    $a = DllCall("gdiplus.dll", "int", "GdipSetInterpolationMode", "hwnd", $hMemGraph, "int", $iInterMode)
    $a = DllCall("gdiplus.dll", "int", "GdipSetPixelOffsetMode", "hwnd", $hMemGraph, "int", $iPixOffsetMode)
    ;$a = DllCall("gdiplus.dll", "int", "GdipSetSmoothingMode", "hwnd", $hMemGraph, "int", $iSmoothMode)
    _GDIPlus_GraphicsDrawImageRectRect($hMemGraph, $hImg, 0, 0, $iW1, $iH1, 0, 0, $iW2, $iH2)
    Return $hImg2
EndFunc
Func Load_BMP_From_Mem($pic)
    Local $memBitmap, $len, $tMem, $hImage, $hData, $pData, $hStream, $hBitmapFromStream
    $memBitmap = Binary($pic)
    $len = BinaryLen($memBitmap)
    $hData = _MemGlobalAlloc($len, 0x0002)
    $pData = _MemGlobalLock($hData)
    $tMem = DllStructCreate("byte[" & $len & "]", $pData)
    DllStructSetData($tMem, 1, $memBitmap)
    _MemGlobalUnlock($hData)
    $hStream = _WinAPI_CreateStreamOnHGlobal($pData)
    $hBitmapFromStream = _GDIPlus_BitmapCreateFromStream($hStream)
    $tMem = ""
    Return $hBitmapFromStream
EndFunc

 

Result.JPG

Konya_GDI+_Image_Deskew_Crop.zip

Edited by konya

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...