taking picture with the webcam

Just starting out? Need help? Post your questions and find answers here.
gabriel
Enthusiast
Enthusiast
Posts: 137
Joined: Sat Aug 01, 2009 4:49 pm
Location: Beirut, Lebanon

taking picture with the webcam

Post by gabriel »

Hello:

is it possible to take a picture with the webcam? and how?

thanks
User avatar
idle
Always Here
Always Here
Posts: 5915
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: taking picture with the webcam

Post by idle »

Yes you can but what exactly do you want to do? There are a few ways you can do it.
gabriel
Enthusiast
Enthusiast
Posts: 137
Joined: Sat Aug 01, 2009 4:49 pm
Location: Beirut, Lebanon

Re: taking picture with the webcam

Post by gabriel »

thank you.
In fact, I want to take a picture and save it to a file when I click on a button.
User avatar
idle
Always Here
Always Here
Posts: 5915
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: taking picture with the webcam

Post by idle »

Code: Select all

#WM_CAP_START = #WM_USER
#WM_CAP_DRIVER_CONNECT = #WM_CAP_START + 10
#WM_CAP_DRIVER_DISCONNECT = #WM_CAP_START + 11
#WM_CAP_DRIVER_GET_CAPS = #WM_CAP_START + 14
#WM_CAP_EDIT_COPY = #WM_CAP_START + 30
#WM_CAP_SET_PREVIEW = #WM_CAP_START + 50
#WM_CAP_SET_PREVIEWRATE = #WM_CAP_START + 52
#WM_CAP_STOP = #WM_CAP_START + 68
#WM_CAP_SET_SCALE = #WM_CAP_START + 53

#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

Import "avicap32.lib"
CapCreateCaptureWindow.i(name.s, style.i, x.i, y.i, width.i, height.i, hWndParent.i, nId.i) As "_capCreateCaptureWindowA@32"
CapGetDriverDescription.i(index.i, name.i, cbName.i, ver.i, cbVer.i) As "_capGetDriverDescriptionA@20"
EndImport

;======if the compiler can't find the lib use this and call close library at the end of your code 
; Prototype.i ProtCapCreateCaptureWindow(name.s, style.i, x.i, y.i, width.i, height.i, hWndParent.i, nId.i)
; Prototype.i ProtCapGetDriverDescription(index.i, name.i, cbName.i, ver.i, cbVer.i)
; 
; If OpenLibrary(0, "avicap32.dll")
;    Global CapCreateCaptureWindow.ProtCapCreateCaptureWindow = GetFunction(0, "capCreateCaptureWindowA")
;    Global CapGetDriverDescription.ProtCapGetDriverDescription = GetFunction(0, "capGetDriverDescriptionA")
; EndIf


Procedure CapImage()
  Static sct
   
  sfile.s = SaveFileRequester("Save","c:\cap" + Str(sct) + ".bmp","*.bmp",1)
  image = GetClipboardImage(#PB_Any,32)

  If image 
     SaveImage(image,sfile)
     sct+1
  EndIf 
    
EndProcedure    
    
mwind = OpenWindow(#PB_Any,0,0,320,270,"WebCam",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)

If mwind   
  Butcap = ButtonGadget(#PB_Any,5,245,60,20,"Capture")
  hCapWnd= CapCreateCaptureWindow("CapWindow", #WS_CHILD | #WS_VISIBLE,0,0,320,240,WindowID(mwind),0)
  SendMessage_(hCapWnd, #WM_CAP_DRIVER_CONNECT, 0, 0)
  SendMessage_(hCapWnd, #WM_CAP_SET_PREVIEW, 1, 0)
  SendMessage_(hCapWnd, #WM_CAP_SET_PREVIEWRATE, 10, 0)
  SendMessage_(hCapWnd, #WM_CAP_SET_SCALE,1,0)
  SendMessage_(hCapWnd, #WM_CAP_DLG_VIDEOFORMAT, 0, 0) 
  
  Repeat
   
  ev = WaitWindowEvent()
   
  If ev = #PB_Event_Gadget 
     evg = EventGadget()

     If evg = Butcap 
        PostMessage_(hCapWnd,#WM_CAP_EDIT_COPY,0,0)
        CapImage()
     EndIf
  EndIf      

Until ev = #PB_Event_CloseWindow

SendMessage_(hCapWnd, #WM_CAP_STOP, 0, 0)
SendMessage_(hCapWnd,#WM_CAP_DRIVER_DISCONNECT,0,0)
DestroyWindow_(hCapWnd)

EndIf 

infratec
Always Here
Always Here
Posts: 7623
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: taking picture with the webcam

Post by infratec »

Hi Gabriel,

if you want to take single pictures and your webcam offers a twain driver,
than you can have a look to
http://www.purebasic.fr/english/viewtop ... 27&t=36281

I used a webcam for making pictures like a photo camera.

Bernd
gabriel
Enthusiast
Enthusiast
Posts: 137
Joined: Sat Aug 01, 2009 4:49 pm
Location: Beirut, Lebanon

Re: taking picture with the webcam

Post by gabriel »

Thank you, all
Post Reply