Page 1 sur 1

Capture photo via webcam en résolution max

Posté : lun. 13 nov. 2023 23:30
par LeRebel
Bonjour,

J'ai besoin de prendre des photos à l'aide d'une webcam pour photographier des éléments sur mon bureau (physique) comme si je possédais un banc de reproduction photo. Ne possédant pas les moyens d'avoir une installation professionnelle, je me contente de ce type de solution. Je ne suis pas informaticien mais j'arrive à effectuer quelques petits scripts en piochant les informations récupérées sur les différents forums (merci aux contributeurs). J'ai récupéré un script intéressant répondant partiellement à ma demande. Il me permet d'effectuer des photos à partir de la webcam. Ce dernier étant d'un niveau trop avancé par rapport à mes connaissances, je ne trouve pas les valeurs à modifier pour bénéficier de la résolution max de la webcam. En effet les photos récupérées sont en 640 * 480 alors que la webcam dispose d'une résolution max de 1280 * 720 (testé avec la fonction prise de photo de Windows 10). Si quelqu'un pouvait partager la solution ce serait super sympa.

Je vous mets en dessous le code récupéré.
J'ai réuni dans le même code celui de webcam.au3 (bibliothèque contenant les fonctions permettant de contrôler la webcam) avec un code permettant de gérer les photos prises avec ajout de texte
#cs ----------------------------------------------------------------------------

    AutoIt Version: 3.3.6.1
    Author:         autoBert  http://www.autoit.de/index.php?page=Thread&postID=182937#post182937
    based on http://www.autoitscript.com/forum/index.php?showtopic=79366&view=findpost&p=572154

    Script Function:
    Demo for Webcam Udf

#ce ----------------------------------------------------------------------------

#include <StructureConstants.au3>
#include <WindowsConstants.au3>
#include-once

Global $WM_CAP_START = 0x400
Global $WM_CAP_UNICODE_START = $WM_CAP_START +100
Global $WM_CAP_PAL_SAVEA = $WM_CAP_START + 81
Global $WM_CAP_PAL_SAVEW = $WM_CAP_UNICODE_START + 81
Global $WM_CAP_UNICODE_END = $WM_CAP_PAL_SAVEW
Global $WM_CAP_ABORT = $WM_CAP_START + 69
Global $WM_CAP_DLG_VIDEOCOMPRESSION = $WM_CAP_START + 46
Global $WM_CAP_DLG_VIDEODISPLAY = $WM_CAP_START + 43
Global $WM_CAP_DLG_VIDEOFORMAT = $WM_CAP_START + 41
Global $WM_CAP_DLG_VIDEOSOURCE = $WM_CAP_START + 42
Global $WM_CAP_DRIVER_CONNECT = $WM_CAP_START + 10
Global $WM_CAP_DRIVER_DISCONNECT = $WM_CAP_START + 11
Global $WM_CAP_DRIVER_GET_CAPS = $WM_CAP_START + 14
Global $WM_CAP_DRIVER_GET_NAMEA = $WM_CAP_START + 12
Global $WM_CAP_DRIVER_GET_NAMEW = $WM_CAP_UNICODE_START + 12
Global $WM_CAP_DRIVER_GET_VERSIONA = $WM_CAP_START + 13
Global $WM_CAP_DRIVER_GET_VERSIONW = $WM_CAP_UNICODE_START + 13
Global $WM_CAP_EDIT_COPY = $WM_CAP_START + 30
Global $WM_CAP_END = $WM_CAP_UNICODE_END
Global $WM_CAP_FILE_ALLOCATE = $WM_CAP_START + 22
Global $WM_CAP_FILE_GET_CAPTURE_FILEA = $WM_CAP_START + 21
Global $WM_CAP_FILE_GET_CAPTURE_FILEW = $WM_CAP_UNICODE_START + 21
Global $WM_CAP_FILE_SAVEASA = $WM_CAP_START + 23
Global $WM_CAP_FILE_SAVEASW = $WM_CAP_UNICODE_START + 23
Global $WM_CAP_FILE_SAVEDIBA = $WM_CAP_START + 25
Global $WM_CAP_FILE_SAVEDIBW = $WM_CAP_UNICODE_START + 25
Global $WM_CAP_FILE_SET_CAPTURE_FILEA = $WM_CAP_START + 20
Global $WM_CAP_FILE_SET_CAPTURE_FILEW = $WM_CAP_UNICODE_START + 20
Global $WM_CAP_FILE_SET_INFOCHUNK = $WM_CAP_START + 24
Global $WM_CAP_GET_AUDIOFORMAT = $WM_CAP_START + 36
Global $WM_CAP_GET_CAPSTREAMPTR = $WM_CAP_START + 1
Global $WM_CAP_GET_MCI_DEVICEA = $WM_CAP_START + 67
Global $WM_CAP_GET_MCI_DEVICEW = $WM_CAP_UNICODE_START + 67
Global $WM_CAP_GET_SEQUENCE_SETUP = $WM_CAP_START + 65
Global $WM_CAP_GET_STATUS = $WM_CAP_START + 54
Global $WM_CAP_GET_USER_DATA = $WM_CAP_START + 8
Global $WM_CAP_GET_VIDEOFORMAT = $WM_CAP_START + 44
Global $WM_CAP_GRAB_FRAME = $WM_CAP_START + 60
Global $WM_CAP_GRAB_FRAME_NOSTOP = $WM_CAP_START + 61
Global $WM_CAP_PAL_AUTOCREATE = $WM_CAP_START + 83
Global $WM_CAP_PAL_MANUALCREATE = $WM_CAP_START + 84
Global $WM_CAP_PAL_OPENA = $WM_CAP_START + 80
Global $WM_CAP_PAL_OPENW = $WM_CAP_UNICODE_START + 80
Global $WM_CAP_PAL_PASTE = $WM_CAP_START + 82
Global $WM_CAP_SEQUENCE = $WM_CAP_START + 62
Global $WM_CAP_SEQUENCE_NOFILE = $WM_CAP_START + 63
Global $WM_CAP_SET_AUDIOFORMAT = $WM_CAP_START + 35
Global $WM_CAP_SET_CALLBACK_CAPCONTROL = $WM_CAP_START + 85
Global $WM_CAP_SET_CALLBACK_ERRORA = $WM_CAP_START + 2
Global $WM_CAP_SET_CALLBACK_ERRORW = $WM_CAP_UNICODE_START + 2
Global $WM_CAP_SET_CALLBACK_FRAME = $WM_CAP_START + 5
Global $WM_CAP_SET_CALLBACK_STATUSA = $WM_CAP_START + 3
Global $WM_CAP_SET_CALLBACK_STATUSW = $WM_CAP_UNICODE_START + 3
Global $WM_CAP_SET_CALLBACK_VIDEOSTREAM = $WM_CAP_START + 6
Global $WM_CAP_SET_CALLBACK_WAVESTREAM = $WM_CAP_START + 7
Global $WM_CAP_SET_CALLBACK_YIELD = $WM_CAP_START + 4
Global $WM_CAP_SET_MCI_DEVICEA = $WM_CAP_START + 66
Global $WM_CAP_SET_MCI_DEVICEW = $WM_CAP_UNICODE_START + 66
Global $WM_CAP_SET_OVERLAY = $WM_CAP_START + 51
Global $WM_CAP_SET_PREVIEW = $WM_CAP_START + 50
Global $WM_CAP_SET_PREVIEWRATE = $WM_CAP_START + 52
Global $WM_CAP_SET_SCALE = $WM_CAP_START + 53
Global $WM_CAP_SET_SCROLL = $WM_CAP_START + 55
Global $WM_CAP_SET_SEQUENCE_SETUP = $WM_CAP_START + 64
Global $WM_CAP_SET_USER_DATA = $WM_CAP_START + 9
Global $WM_CAP_SET_VIDEOFORMAT = $WM_CAP_START + 45
Global $WM_CAP_SINGLE_FRAME = $WM_CAP_START + 72
Global $WM_CAP_SINGLE_FRAME_CLOSE = $WM_CAP_START + 71
Global $WM_CAP_SINGLE_FRAME_OPEN = $WM_CAP_START + 70
Global $WM_CAP_STOP = $WM_CAP_START + 68
Global $SAVE_FILE = @ScriptDir & "\snapshot.bmp"
Global $ptrCallBack = -1
Local $avi = DllOpen("avicap32.dll")
Local $WM_CAP_USER_DLL = DllOpen("user32.dll")

;===============================================================================
;
; Description:      Open's a webcam preview screen in your gui
; Syntax:           _WebcamOpen($sHwnd, $sLeft, $sTop, $sWidth, $sHeight)
; Parameter(s):     $sHwnd     - The handle of the gui
;                   $sLeft     - Left coord. of the preview screen
;                   $sTop      - Top coord. of the preview screen
;                   $sWidth    - Width of the preview screen
;                   $sHeight   - Height of the preview screen
; Requirement(s):   A webcam
; Return Value(s):  On Success - Returns id needed for other controls
;                   On Failure - Returns -1
; Author(s):        Ludocus
; Note(s):          None
;
;===============================================================================
func _WebcamOpen($sHwnd, $sLeft, $sTop, $sWidth, $sHeight, $index = 0)
    Local $cap = DllCall($avi, "int", "capCreateCaptureWindow", "str", "cap", "int", BitOR($WS_CHILD,$WS_VISIBLE), "int", $sLeft, "int", $sTop, "int", $sWidth, "int", $sHeight, "hwnd", $sHwnd, "int", 1)
    DllCall($WM_CAP_USER_DLL, "int", "SendMessage", "hWnd", $cap[0], "int", $WM_CAP_DRIVER_CONNECT, "int", $index, "int", 0)
    DllCall($WM_CAP_USER_DLL, "int", "SendMessage", "hWnd", $cap[0], "int", $WM_CAP_SET_SCALE, "int", 1, "int", 0)
    DllCall($WM_CAP_USER_DLL, "int", "SendMessage", "hWnd", $cap[0], "int", $WM_CAP_SET_OVERLAY, "int", 1, "int", 0)
    DllCall($WM_CAP_USER_DLL, "int", "SendMessage", "hWnd", $cap[0], "int", $WM_CAP_SET_PREVIEW, "int", 1, "int", 0)
;~     DllCall($WM_CAP_USER_DLL, "int", "SendMessage", "hWnd", $cap[0], "int", $WM_CAP_SET_PREVIEWRATE, "int", 1, "int", 0)
    if @error then return -1
    return $cap[0]
EndFunc

;~ Func _WebcamListCam($sId
;===============================================================================
;
; Description:      Creates a Snapshot from a webcam
; Syntax:           _WebcamSnap($sId, $sFile)
; Parameter(s):     $sId       - Id (returned from _WebcamOpen)
;                   $sFile     - File to save the snapshot to (*.bmp)
; Requirement(s):   A webcam
; Return Value(s):  On Success - Returns 1
;                   On Failure - Returns 0
; Author(s):        Ludocus
; Note(s):          None
;
;===============================================================================
Func _WebcamSnap($sId, $sFile = $SAVE_FILE)
    DllCall($WM_CAP_USER_DLL, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_GRAB_FRAME, "int", 0, "int", 0)
    DllCall($WM_CAP_USER_DLL, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_FILE_SAVEDIBA, "int", 0, "str", $sFile)
    if @error Then
        return 0
    Else
        return 1
    EndIf
EndFunc

;===============================================================================
;
; Description:      Closes the preview screen created with _WebcamOpen
; Syntax:           _WebcamClose($sId)
; Parameter(s):     $sId       - Id (returned from _WebcamOpen)
; Requirement(s):   A webcam
; Return Value(s):  On Success - Returns 1
;                   On Failure - Returns 0
; Author(s):        Ludocus
; Note(s):          None
;
;===============================================================================
Func _WebcamClose($sId)
    DllCall($WM_CAP_USER_DLL, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_END, "int", 0, "int", 0)
    DllCall($WM_CAP_USER_DLL, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_DRIVER_DISCONNECT, "int", 0, "int", 0)
    DllClose($WM_CAP_USER_DLL)
    if @error Then
        return 0
    Else
        return 1
    EndIf
EndFunc

;===============================================================================
;
; Description:      Starts recording the webcam to a file
; Syntax:           _WebcamRecordStart($sFile, $sId)
; Parameter(s):     $sId       - Id (returned from _WebcamOpen)
;                   $sFile     - File to save the movie to (*.avi)
; Requirement(s):   A webcam
; Return Value(s):  On Success - Returns 1
;                   On Failure - Returns 0
; Author(s):        Ludocus
; Note(s):          Stop recording by: _WebcamRecordStop($Id)
;
;===============================================================================
Func _WebcamRecordStart($sFile, $sId)
    DllCall($WM_CAP_USER_DLL, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_FILE_SET_CAPTURE_FILEA, "int", 0, "str", $sFile)
    DllCall($WM_CAP_USER_DLL, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_SEQUENCE, "int", 0, "int", 0)
    if @error Then
        return 0
    Else
        return 1
    EndIf

EndFunc

;===============================================================================
;
; Description:      Stops recording.
; Syntax:           _WebcamRecordStop($sId)
; Parameter(s):     $sId       - Id (returned from _WebcamOpen)
; Requirement(s):   A webcam
; Return Value(s):  On Success - Returns 1
;                   On Failure - Returns 0
; Author(s):        Ludocus
; Note(s):          None
;
;===============================================================================
Func _WebcamRecordStop($sId)
    DllCall($WM_CAP_USER_DLL, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_STOP, "int", 0, "int", 0)
    if @error Then
        return 0
    Else
        return 1
    EndIf
EndFunc ;==> _WebcamRecordStop

Func _WebCamGetNumberOfDevices()
    Local $Devices = _WebCamGetDevicesDescription()
    Return $Devices[0][0]
EndFunc

Func _WebCamGetDevicesDescription()
    Local $compteur = 0
    Local $Devices[1][3] = [[0, 0, 0]]
    Local $DeviceTemp
    While(True)
        $DeviceTemp = _WebCamGetDeviceDescription($compteur)
        If(@error) Then ExitLoop
        $Devices[0][0] += 1
        ReDim $Devices[$Devices[0][0]+1][UBound($Devices, 2)]
        $Devices[$Devices[0][0]][0] = $compteur
        $Devices[$Devices[0][0]][1] = $DeviceTemp[0]
        $Devices[$Devices[0][0]][2] = $DeviceTemp[1]
        $compteur += 1
    WEnd
    Return $Devices
EndFunc

Func _WebCamGetDeviceDescription($iIndex)
    Local $tBuffName, $tBuffVer
    Local $aResult, $aRet[2]

    $tBuffName = DllStructCreate("char[128]")
    $tBuffVer  = DllStructCreate("char[128]")

    $aResult = DllCall("avicap32.dll", "int", "capGetDriverDescriptionA", "ushort", $iIndex, "ptr", DllStructGetPtr($tBuffName), _
                        "int", 128, "ptr", DllStructGetPtr($tBuffVer), "int", 128)

    If @error Then Return SetError(@error, @extended, 0)

    If $aResult[0] Then
        $aRet[0] = DllStructGetData($tBuffName, 1)
        $aRet[1] = DllStructGetData($tBuffVer, 1)
        Return $aRet
    EndIf

    Return SetError(1, 0, 0)
EndFunc

Func _WebcamGetFrame($sId)
    DllCall($WM_CAP_USER_DLL, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_GRAB_FRAME, "int", 0, "int", 0)
EndFunc

Func _WebcamSetCallBackFram($sId, $func)
    If($ptrCallBack <> -1) Then
        DllCallbackFree($ptrCallBack)
    EndIf
    $CallBack    = DllCallbackRegister($func, "long", "hwnd;ptr")
    $ptrCallBack = DllCallbackGetPtr($CallBack)
    DllCall($WM_CAP_USER_DLL, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_SET_CALLBACK_FRAME, "int", 0, "lparam", $ptrCallBack)
EndFunc

Func _WebcamStopCallBackFram($sId)
    DllCallbackFree($ptrCallBack)
    $ptrCallBack = -1
    DllCall($WM_CAP_USER_DLL, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_SET_CALLBACK_FRAME, "int", 0, "lparam", 0)
EndFunc

Func _WebcamGetVideoFormat($sId)
    Local $structVideo = DllStructCreate($tagBITMAPINFO)
    Local $Temp = DllCall($WM_CAP_USER_DLL, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_GET_VIDEOFORMAT, "int", DllStructGetSize($structVideo), "int", DllStructGetPtr($structVideo))
    Return $structVideo
EndFunc

Func _WebcamGetResolution($sId)
    Local $structVideo = DllStructCreate($tagBITMAPINFO)
    Local $Temp = DllCall($WM_CAP_USER_DLL, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_GET_VIDEOFORMAT, "int", DllStructGetSize($structVideo), "int", DllStructGetPtr($structVideo))
    Local $Taille[2] = [DllStructGetData($structVideo, 2), DllStructGetData($structVideo, 3)]
    Return $Taille
EndFunc

Func _WebcamDisplayVideoFormat($sId)
    Local $structBITMAPINFO = _WebcamGetVideoFormat($sId)
    MsgBox(0, "",    DllStructGetData($structBITMAPINFO, 1) & @CRLF & _
                    DllStructGetData($structBITMAPINFO, 2) & @CRLF & _
                    DllStructGetData($structBITMAPINFO, 3) & @CRLF & _
                    DllStructGetData($structBITMAPINFO, 4) & @CRLF & _
                    DllStructGetData($structBITMAPINFO, 5) & @CRLF & _
                    DllStructGetData($structBITMAPINFO, 6) & @CRLF & _
                    DllStructGetData($structBITMAPINFO, 7) & @CRLF & _
                    DllStructGetData($structBITMAPINFO, 8) & @CRLF & _
                    DllStructGetData($structBITMAPINFO, 9) & @CRLF & _
                    DllStructGetData($structBITMAPINFO, 10) & @CRLF & _
                    DllStructGetData($structBITMAPINFO, 11))
EndFunc






#include<GDIPlus.au3>
#Include <Misc.au3>
#include<array.au3>

$cam_xlength = 1280;Breite
$cam_ylength = 720 ;Höhe

$sProgTitel ="SnapShot from WebCam"
$sProgVer = "v 0.8.1.3"

$capDir = @ScriptDir & "\captures"
If Not FileExists($capDir) Then DirCreate($capDir)

If _Singleton($sProgTitel & $sProgVer, 1) = 0 Then
    WinActivate($sProgTitel & $sProgVer)
    Exit
EndIf

$gui = GUICreate($sProgTitel & $sProgVer, $cam_xlength, $cam_ylength + 20, 0, 0)

$WebcamID = _WebcamOpen($gui, 0, 0, $cam_xlength + 30, $cam_ylength)

if $WebcamID = -1 Then
    MsgBox(16,"Attention","Pas d'accès au logiciel de gestion de la caméra")
    exit
EndIf
$label1 = GUICtrlCreateLabel("", 0, $cam_ylength + 50, $cam_xlength, 20)
_GDIPlus_Startup()

GUISetState(@SW_SHOW)

$nr = 1
While FileExists($capDir & "\Bild" & $nr & ".jpg")
    $nr += 1
WEnd

$oFile = $capDir & "\Bild.bmp"
$nFile = $capDir & "\Bild" & $nr & ".jpg"
if FileExists($oFile) Then FileDelete($oFile)
 _WebcamSnap($WebcamID, $oFile)
MsgBox(0,"",$oFile)

Local $aDT = FileGetTime($oFile)
$sInfo = $aDT[0] & "/" & $aDT[1] & "/" & $aDT[2] & " " & $aDT[3] & ":" & $aDT[4] & ":" & $aDT[5]
ConsoleWrite($sInfo & @CRLF)

$himg = _GDIPlus_ImageLoadFromFile($oFile)
$hGraphics = _GDIPlus_ImageGetGraphicsContext($himg)
$iX = _GDIPlus_ImageGetWidth($himg)
$iy = _GDIPlus_ImageGetHeight($himg)
$hBrush = _GDIPlus_BrushCreateSolid(); $Color)
$hFormat = _GDIPlus_StringFormatCreate()
$hFamily = _GDIPlus_FontFamilyCreate("Arial")
$hFont = _GDIPlus_FontCreate($hFamily, 20, 2)
$tLayout = _GDIPlus_RectFCreate($iX -260, $iy -30)
$aInfo = _GDIPlus_GraphicsMeasureString($hGraphics, $sInfo, $hFont, $tLayout, $hFormat)
 _GDIPlus_GraphicsDrawStringEx($hGraphics, $sInfo, $hFont, $aInfo[0], $hFormat, $hBrush)
 _GDIPlus_GraphicsDrawImage($hGraphics, $himg, $iX, $iy)
 _GDIPlus_ImageSaveToFile($himg, $nFile)
 _GDIPlus_FontDispose($hFont)
 _GDIPlus_BrushDispose($hBrush)
 _GDIPlus_GraphicsDispose($hGraphics)
 _GDIPlus_ImageDispose($himg)
 GUICtrlSetData($label1, $nr)
 FileDelete($oFile)
 
Merci