Webcam capture???

Just starting out? Need help? Post your questions and find answers here.
Hydrate
Enthusiast
Enthusiast
Posts: 436
Joined: Mon May 16, 2005 9:37 pm
Contact:

Webcam capture???

Post by Hydrate »

I need a fast example of how to capture the webcam and save it to a file on the users computer for a quick program to help me learn and for a birthday suprise, can anyone help??? I have two days.
.::Image::.
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

This one is old but it may help you get started. :)

http://www.purebasic.fr/english/viewtop ... ght=webcam
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
Hydrate
Enthusiast
Enthusiast
Posts: 436
Joined: Mon May 16, 2005 9:37 pm
Contact:

Post by Hydrate »

Sparkie wrote:This one is old but it may help you get started. :)

http://www.purebasic.fr/english/viewtop ... ght=webcam
The example looks good,it detects my drivers... but i can only get it to display a black screen, any ideas why??
.::Image::.
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

Is this the code you are using :?:

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_SET_PREVIEW = #WM_CAP_START + 50 
#WM_CAP_SET_PREVIEWRATE = #WM_CAP_START + 52 
#WM_CAP_STOP = #WM_CAP_START + 68 

If OpenWindow(0, 0, 0, 340, 260, "Cam Capture", #PB_Window_ScreenCentered | #PB_Window_SystemMenu) 
  If OpenLibrary(0, "avicap32.dll") 
    *capAddress = GetFunction(0, "capCreateCaptureWindowA") 
    hWndC = CallFunctionFast(*capAddress, "My Capture Window", #WS_CHILD | #WS_VISIBLE, 10, 10, 320, 240, WindowID(0),1) 
    SendMessage_(hWndC, #WM_CAP_DRIVER_CONNECT, 0, 0) 
    SendMessage_(hWndC, #WM_CAP_SET_PREVIEW, #True, 0) 
    SendMessage_(hWndC, #WM_CAP_SET_PREVIEWRATE, 15, 0) 
  EndIf 
EndIf 

Repeat 
  Event = WaitWindowEvent() 
Until Event = #PB_Event_CloseWindow 

SendMessage_(hWndC, #WM_CAP_STOP, 0, 0) 
SendMessage_(hWndC, #WM_CAP_DRIVER_DISCONNECT, 0, 0) 
DestroyWindow_(hWndC) 
CloseLibrary(0) 

End
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
danraymond
User
User
Posts: 43
Joined: Wed Jun 28, 2006 6:02 am

My Version

Post by danraymond »

This one I cobbled together from previous advice as I needed the same thing.

when you press "show" button the "shown" image is removed wiht a right button click.

the final one is skinned and allows sequential numbering of images etc.

but see if you can use any of this:

Code: Select all

UseJPEGImageEncoder()
UseJPEGImageDecoder()
Enumeration
  
  #Window_0
  #Button_0
  #Button_1
  #Button_2
  #Button_4
  #viewer1
  #gadget1
  #counter
  #counter1
  #name
  #takebut
  #viewbut
  #quitbut
EndEnumeration
#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
Dim snapshot.l(50)
i=1
If OpenWindow(#Window_0, 100, 100, 408, 338, "Little Camera", #PB_Window_SystemMenu |  #PB_Window_MinimizeGadget     )
  If CreateGadgetList(WindowID(#Window_0))
    
    ButtonGadget(#Button_0, 150, 280, 50, 25, "Capture")
    ButtonGadget(#Button_1, 250, 280, 50, 25, "Quit")
    ButtonGadget(#Button_2, 200, 280, 50, 25, "Show") 
    EndIf
    
    
    
  EndIf
  
  If OpenLibrary(0, "avicap32.dll")
    *capAddress = GetFunction(0, "capCreateCaptureWindowA")
    
    hWndC.l = CallFunctionFast(*capAddress, "My Capture Window", #WS_CHILD | #WS_VISIBLE, 50,38, 310, 230, WindowID(0),0)
    SendMessage_(hWndC, #WM_CAP_DRIVER_CONNECT, 0, 0)
    SendMessage_(hWndC, #WM_CAP_SET_PREVIEW, #True, 0)
    SendMessage_(hWndC, #WM_CAP_SET_PREVIEWRATE, 1, 0)
    SendMessage_(hWndC, #WM_CAP_SET_SCALE, #True, 0)
    
    
    
  EndIf



Repeat
  Event=WaitWindowEvent(10)
  Select Event
    
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #Button_0 
          SendMessage_(hWndC, #WM_CAP_EDIT_COPY, 0, 0)
          snapshot(i) = GetClipboardImage(#PB_Any)
          If snapshot(i)
             
            saver$="snapshot"+Str(i)+".jpg"
            SaveImage(snapshot(i),saver$,#PB_ImagePlugin_JPEG,10)
            FreeImage(snapshot(i))
            
            i=i+1
            If i=51
              i=0
            EndIf
          EndIf
        Case #Button_1
          SendMessage_(hWndC, #WM_CAP_STOP, 0, 0)
          SendMessage_(hWndC, #WM_CAP_DRIVER_DISCONNECT, 0, 0)
          DestroyWindow_(hWndC)
           CloseLibrary(0)
          End
          
        Case #Button_2
          
          If OpenWindow(#viewer1, 10, 10, 340, 270, "ImageGadget", #PB_Window_ScreenCentered|#PB_Window_BorderLess    ) 
            StickyWindow(#viewer1, 1) 
            If CreateGadgetList((WindowID(#viewer1)))
              
               
              existimage=  LoadImage(0, saver$)     ;Path/filename of the image
              If existimage
              ImageGadget(#gadget1,  10, 10, 310, 230, ImageID(0),#PB_Image_Border)                      ; imagegadget standard
            Else
              ResizeWindow(#viewer1,#PB_Ignore ,#PB_Ignore ,10,10)
              
              MessageRequester("No Image", "image doesn,t exist",#PB_MessageRequester_Ok)
              CloseWindow(#viewer1)
              quit=1
              EndIf
              
             
            EndIf
            Repeat
              event2=WaitWindowEvent()
              bugger=EventType()
              Select bugger
                Case #PB_EventType_RightClick
                  CloseWindow(#viewer1)
                  quit=1
                  
              EndSelect
            Until quit=1
          EndIf
          quit=0
        
      EndSelect
  EndSelect
  
  
  
Until Event = #PB_Event_CloseWindow

SendMessage_(hWndC, #WM_CAP_STOP, 0, 0)
SendMessage_(hWndC, #WM_CAP_DRIVER_DISCONNECT, 0, 0)
DestroyWindow_(hWndC)
CloseLibrary(0)

End
dan raymond
JCV
Enthusiast
Enthusiast
Posts: 580
Joined: Fri Jun 30, 2006 4:30 pm
Location: Philippines

Post by JCV »

Btw, how to change the device to use?
It shows my tv tuner instead of my webcam.
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

Change this line SendMessage_(hWndC, #WM_CAP_DRIVER_CONNECT, 0, 0)

to this

Code: Select all

SendMessage_(hWndC, #WM_CAP_DRIVER_CONNECT, 1, 0)
or add this line right after SendMessage_(hWndC, #WM_CAP_SET_SCALE, #True, 0)

Code: Select all

;............................................
#WM_CAP_START = #WM_USER
#WM_CAP_DLG_VIDEOSOURCE = #WM_CAP_START + 42
;............................................

SendMessage_(hWndC, #WM_CAP_DLG_VIDEOSOURCE, 0, 0)
That will open a dialog window for you to select source.
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
Pantcho!!
Enthusiast
Enthusiast
Posts: 538
Joined: Tue Feb 24, 2004 3:43 am
Location: Israel
Contact:

Post by Pantcho!! »

Sparkie you did it again 8)
JCV
Enthusiast
Enthusiast
Posts: 580
Joined: Fri Jun 30, 2006 4:30 pm
Location: Philippines

Post by JCV »

yeah. :D Great Job!
Hydrate
Enthusiast
Enthusiast
Posts: 436
Joined: Mon May 16, 2005 9:37 pm
Contact:

Post by Hydrate »

Ah yes, thank you very much :D.
.::Image::.
AndyMK
Enthusiast
Enthusiast
Posts: 582
Joined: Wed Jul 12, 2006 4:38 pm
Location: UK

Re: Webcam capture???

Post by AndyMK »

Here is a crude, undocumented example that uses the escapi dll Copyright (c)2007 Jari Komppa http://iki.fi/sol and the Wxl Jpeg in memory compressor lib. This example will capture video from the first video capture device it finds on your PC and compresses each frame to jpeg on screen, all done in memory. You can modify it to write to disk in uncompressed or your own style motion jpeg video format or send over the internet/network. Copy Wxl_Lib_Win32 to your \PureLibraries\UserLibraries folder and run demo.pb. This is what it looks like. It is much faster than using avicap.

Code: Select all

timeBeginPeriod_(1)
IncludeFile "escapi.pbi"
UseJPEGImageDecoder()

device = 0
Width = 320
Height = 240

dest = AllocateMemory(Width*Height)
destLen = MemorySize(dest)
count = setupESCAPI()

name$ = Space(1000)
getCaptureDeviceName(device, @name$, 1000)

scp.SimpleCapParams
scp\mWidth = Width
scp\mHeight = Height
scp\mTargetBuf = AllocateMemory(scp\mWidth*scp\mHeight*4)

If initCapture(device, @scp)
  
  image = CreateImage(#PB_Any, Width, Height)
  
  OpenWindow(0, 0, 0, Width, Height, name$, #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
  ImageGadget(0, 0, 0, Width, Height, ImageID(image))
  wxl_EncodePixelFormat(#PB_PixelFormat_32Bits_BGR)

  Repeat
    Event = WindowEvent()
    
    doCapture(device)
    While isCaptureDone(device) = 0
      Delay(1)
    Wend
        
    len = wxl_JPEGEncode(scp\mTargetBuf, Width, Height, 50, dest, destLen)
    
    CatchImage(image2, dest, len)
    SetGadgetState(0, ImageID(image2))
    
  Until Event = #PB_Event_CloseWindow
  
  deinitCapture(device)
Else
  Debug "init capture failed!"
EndIf
timeEndPeriod_(1)
End
Download:
http://dl.dropbox.com/u/7526263/Appz/PB ... apture.zip

**********************************************


LOL just realized the date of the last post on this thread! For some reason it was on the front page of the forum.. oh well
Post Reply