Utiliser une webcam et traiter l'image (API)
Publié : jeu. 06/janv./2005 21:23
Je n'étais pas très content de la DLL icamplay alors je compte m'en passer, pour le moment j'en suis au même point par moi même donc je pense que j'ai bien fait. J'ai mis source qui n'est pas totalement optimisée mais commentée et 100% fonctionelle
. Comme tout apparait dans l'écran, sous le handle Image, a vous de jouer
. Je ferais bientôt une version plein écran, sinon je pense que vous savez comment faire, et surtout cachez la fenêtre avec #PB_Window_Invisible mais ne la fermez pas !!!! Car c'est par elle que passe la vidéo.


Code : Tout sélectionner
;- Declarations
; Constantes
Enumeration
#Principale
#AviCap32
EndEnumeration
; Structures
; Listes
; Buffer
; Variables
Sortie.b : hWndPreview.l
Global hWndPreview, Sortie
; ***************************** Procedures ***********************
;Exeption pour les contstantes cap, je les mettrai en Enumeration plus tard
#WM_CAP_START = #WM_USER
#WM_CAP_GET_CAPSTREAMPTR = #WM_CAP_START + 1
#WM_CAP_SET_CALLBACK_ERROR = #WM_CAP_START + 2
#WM_CAP_SET_CALLBACK_STATUS = #WM_CAP_START + 3
#WM_CAP_SET_CALLBACK_YIELD = #WM_CAP_START + 4
#WM_CAP_SET_CALLBACK_FRAME = #WM_CAP_START + 5
#WM_CAP_SET_CALLBACK_VIDEOSTREAM = #WM_CAP_START + 6
#WM_CAP_SET_CALLBACK_WAVESTREAM = #WM_CAP_START + 7
#WM_CAP_GET_USER_DATA = #WM_CAP_START + 8
#WM_CAP_SET_USER_DATA = #WM_CAP_START + 9
#WM_CAP_DRIVER_CONNECT = #WM_CAP_START + 10
#WM_CAP_DRIVER_DISCONNECT = #WM_CAP_START + 11
#WM_CAP_DRIVER_GET_NAME = #WM_CAP_START + 12
#WM_CAP_DRIVER_GET_VERSION = #WM_CAP_START + 13
#WM_CAP_DRIVER_GET_CAPS = #WM_CAP_START + 14
#WM_CAP_FILE_SET_CAPTURE_FILE = #WM_CAP_START + 20
#WM_CAP_FILE_GET_CAPTURE_FILE = #WM_CAP_START + 21
#WM_CAP_FILE_ALLOCATE = #WM_CAP_START + 22
#WM_CAP_FILE_SAVEAS = #WM_CAP_START + 23
#WM_CAP_FILE_SET_INFOCHUNK = #WM_CAP_START + 24
#WM_CAP_FILE_SAVEDIB = #WM_CAP_START + 25
#WM_CAP_EDIT_COPY = #WM_CAP_START + 30
#WM_CAP_SET_AUDIOFORMAT = #WM_CAP_START + 35
#WM_CAP_GET_AUDIOFORMAT = #WM_CAP_START + 36
#WM_CAP_DLG_VIDEOFORMAT = #WM_CAP_START + 41
#WM_CAP_DLG_VIDEOSOURCE = #WM_CAP_START + 42
#WM_CAP_DLG_VIDEODISPLAY = #WM_CAP_START + 43
#WM_CAP_GET_VIDEOFORMAT = #WM_CAP_START + 44
#WM_CAP_SET_VIDEOFORMAT = #WM_CAP_START + 45
#WM_CAP_DLG_VIDEOCOMPRESSION = #WM_CAP_START + 46
#WM_CAP_SET_PREVIEW = #WM_CAP_START + 50
#WM_CAP_SET_OVERLAY = #WM_CAP_START + 51
#WM_CAP_SET_PREVIEWRATE = #WM_CAP_START + 52
#WM_CAP_SET_SCALE = #WM_CAP_START + 53
#WM_CAP_GET_STATUS = #WM_CAP_START + 54
#WM_CAP_SET_SCROLL = #WM_CAP_START + 55
#WM_CAP_GRAB_FRAME = #WM_CAP_START + 60
#WM_CAP_GRAB_FRAME_NOSTOP = #WM_CAP_START + 61
#WM_CAP_SEQUENCE = #WM_CAP_START + 62
#WM_CAP_SEQUENCE_NOFILE = #WM_CAP_START + 63
#WM_CAP_SET_SEQUENCE_SETUP = #WM_CAP_START + 64
#WM_CAP_GET_SEQUENCE_SETUP = #WM_CAP_START + 65
#WM_CAP_SET_MCI_DEVICE = #WM_CAP_START + 66
#WM_CAP_GET_MCI_DEVICE = #WM_CAP_START + 67
#WM_CAP_STOP = #WM_CAP_START + 68
#WM_CAP_ABORT = #WM_CAP_START + 69
#WM_CAP_SINGLE_FRAME_OPEN = #WM_CAP_START + 70
#WM_CAP_SINGLE_FRAME_CLOSE = #WM_CAP_START + 71
#WM_CAP_SINGLE_FRAME = #WM_CAP_START + 72
#WM_CAP_PAL_OPEN = #WM_CAP_START + 80
#WM_CAP_PAL_SAVE = #WM_CAP_START + 81
#WM_CAP_PAL_PASTE = #WM_CAP_START + 82
#WM_CAP_PAL_AUTOCREATE = #WM_CAP_START + 83
#WM_CAP_PAL_MANUALCREATE = #WM_CAP_START + 84
;// Following added post VFW 1.1
#WM_CAP_SET_CALLBACK_CAPCONTROL = #WM_CAP_START + 85
;// Defines end of the message range
#WM_CAP_END = #WM_CAP_SET_CALLBACK_CAPCONTROL
Procedure Cap_Stop()
ProcedureReturn SendMessage_(hWndPreview,#WM_CAP_STOP,0,0)
EndProcedure
Procedure CAP_DRIVER_DISCONNECT()
ProcedureReturn SendMessage_(hWndPreview,#WM_CAP_DRIVER_DISCONNECT,0,0)
EndProcedure
Procedure capCreateCaptureWindowA(lpszWindowName.s,dwStyle.l,x.l,y.l,Width.l,Height.l,hWndParent.l,nId.l)
ProcedureReturn CallFunction(#AviCap32,"capCreateCaptureWindowA", lpszWindowName, dwStyle, x, y,Width,Height,hWndParent,nId)
EndProcedure
Procedure capDriverConnect(i.b)
ProcedureReturn SendMessage_(hWndPreview,#WM_CAP_DRIVER_CONNECT,i,0)
EndProcedure
Procedure capPreviewRate(Rate.b)
ProcedureReturn SendMessage_(hWndPreview,#WM_CAP_SET_PREVIEWRATE,Rate,0)
EndProcedure
Procedure capPreview(Flag.b)
ProcedureReturn SendMessage_(hWndPreview,#WM_CAP_SET_PREVIEW,Flag,0)
EndProcedure
Procedure capCaptureStop()
ProcedureReturn SendMessage_(hWndPreview,#WM_CAP_STOP,0,0)
EndProcedure
Procedure capDriverDisconnect()
ProcedureReturn SendMessage_(hWndPreview,#WM_CAP_DRIVER_DISCONNECT,0,0)
EndProcedure
Procedure capGrabFrameNoStop()
ProcedureReturn SendMessage_(hWndPreview,#WM_CAP_GRAB_FRAME_NOSTOP,0,0)
EndProcedure
Procedure capEditCopy()
ProcedureReturn SendMessage_(hWndPreview,#WM_CAP_EDIT_COPY,0,0)
EndProcedure
Procedure Affichage()
StartDrawing(ScreenOutput())
DrawText("Text")
StopDrawing()
FlipBuffers()
ClearScreen(170,170,170)
EndProcedure
Procedure Clavier()
ExamineKeyboard()
If KeyboardPushed(#PB_Key_Escape) : Sortie = 1 : EndIf
EndProcedure
Procedure Webcam()
capGrabFrameNoStop()
capEditCopy()
ImageID.l = GetClipboardData(#PB_ClipboardImage)
StartDrawing(ScreenOutput())
DrawImage(ImageID,0,0)
StopDrawing()
EndProcedure
; Initialisations
If InitSprite() = 0 Or InitKeyboard() = 0
MessageRequester("Erreur","Le DirectX ne peux pas être initialisé.",#MB_ICONError)
EndIf
; Ouverture de la DLL
If OpenLibrary(#AviCap32,"avicap32.dll") = 0
MessageRequester("Erreur","La DLL avicap32.dll ne peut pas être ouverte.",#MB_ICONError)
EndIf
; Création de la fenêtre pour la preview
OpenWindow(#Principale,0,0,340,260,#PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_TitleBar | #PB_Window_ScreenCentered,"WebCam - Preview")
; | #WS_VISIBLE
; Creation de la fenêtre de preview
hWndPreview = capCreateCaptureWindowA("Preview",#WS_CHILD,0,0,320,240,WindowID(#Principale),0); Le denier paramètre celon le MSDN est "Window identifier", je ne vois pas à quoi il sert.
; On "se connecte" au driver
capDriverConnect(0)
; On active la preview
capPreview(#True)
; On met le le nombre de rafarichissemnt/seconde de la preview, mettre 15 si vous avez une caméra 15 images seconde max.
capPreviewRate(30)
; Création de la fenêtre
; OpenWindow(#Principale,0,0,100,100,#PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_TitleBar | #PB_Window_ScreenCentered,"")
; Ouverture de l'écran
OpenWindowedScreen(WindowID(#Principale),10,10,320,240,0,0,0)
Repeat
ClearScreen(150,150,150)
Select WindowEvent()
Case #PB_Event_CloseWindow
Sortie = 1
EndSelect
; Gère le clavier
Clavier()
; Gère la WebCam
WebCam()
; Gère l'affichage
Affichage()
Delay(5)
Until Sortie = 1
; On arrête la fenêtre de capture
capCaptureStop()
; On libère la caméra : très important pour la réutiliser après et fermer l'EXE rapidement
capDriverDisconnect()
; On ferme les fenêtres
DestroyWindow_(hWndPreview)
CloseWindow(#Principale)
; On freme la DLL
CloseLibrary(#AviCap32)
End