Page 1 of 4

Help with webcam or video stream

Posted: Thu Jun 24, 2004 2:01 pm
by chio
Hi and thanks to all for helping.

I know there are several postings that talk about AVICAP and others for grabbing the video stream but I could not find anywhere a SIMPLE or should we say BASIC script that would do this.

Can someone please post a simple script to grab a video stream. I know in other languages such as ActionScript and such all you need to do is call a class and the paths. Can that be achieved here in less thank 20 lines?

Can someone post an example please.

Thanks again,
Carlos

Posted: Sun Jun 27, 2004 5:25 pm
by Sparkie
I have a WebCam stream / frame capture project in progress that has 333 lines thus far. I plucked out some code, and with the following 19 lines, I am able to view a plain and simple WebCam preview window on my system. Your situation may require some additional device/system checks, so you may need to add some additional code. When you want to start capturing a stream, use SendMessage_(hWndC, #WM_CAP_SEQUENCE, 0, 0). You should also take a look at the CAPTUREPARMS and CAPDRIVERCAPS structures in the Win32 API. For more information, check out MSDN Video Capture

Code: Select all

;Code removed by Sparkie due to errors. Will fix and repost.

Posted: Mon Jun 28, 2004 11:34 am
by Wolf
Hi Sparkie.

Your code is very good but after compile and run it see an error message about can't read memory and program will be closed.

What's problem ?

No any problem if your code is more of 20 line . :wink:


Best regards.

Posted: Mon Jun 28, 2004 12:19 pm
by Sparkie
I just noticed I had OpenLibrary(1... and then CloseLibrary(0...

I edited the original code above and changed OpenLibrary(1 to OpenLibrary(0...

Don't know if that will fix your problem because the even the bad code executed without error here with PB 3.91 beta2 on WinXP.

I'm still learning about video capture so maybe someone else can provide a better answer.

Posted: Mon Jun 28, 2004 1:41 pm
by Sparkie
Seems I messed up while cutting those 20 lines of code from my original project. It's more than 20 lines now :wink: but it should work without error.

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, #PB_Window_ScreenCentered | #PB_Window_SystemMenu, "Video capture") 
  If OpenLibrary(0, "C:\WINDOWS\system32\avicap32.dll") 
    *capAddress = IsFunction(0, "capCreateCaptureWindowA") 
    hWndC = CallFunctionFast(*capAddress, "My Capture Window", #WS_CHILD | #WS_VISIBLE, 10, 10, 320, 240, WindowID(),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

Awesome thanks

Posted: Mon Jun 28, 2004 3:12 pm
by chio
Yhis is excellent on the first try the cam came out. And now I can finally see how to get it in a simple way.

Now I need to go for the thougher parts. Edge recognition, motion and other stuuf.

Thanks a lot. This is great.

What kind of app are you doing with this?

I was trying to get icamplay but the owner of the librarieas has not responded. I hear his stuff is pretty good.


Carlos

Posted: Mon Jun 28, 2004 4:46 pm
by Sparkie
What kind of app are you doing with this?
Just a personal use motion/frame grabber to keep an eye on things while I'm away from home. I think for me, it's more of a learning app. :)

Same her

Posted: Mon Jun 28, 2004 5:47 pm
by chio
That is what I am doing exactly but for my office. But I am a total newbie on this.

Can you share some thoughts of what has gone well or bad. Or what not to try?

Thanks,
Carlos

Re: Same her

Posted: Mon Jun 28, 2004 8:33 pm
by Sparkie
chio wrote:That is what I am doing exactly but for my office. But I am a total newbie on this.

Can you share some thoughts of what has gone well or bad. Or what not to try?

Thanks,
Carlos
Well I'm only about a week into this project so I can't say much yet in regards to the good or bad. Capturing stream and/or frame has been little or no problem. I've just started on motion detection and I'm almost certain this will be a little harder to tackle.

I'll let you know of my success/failures in the coming days. :wink:

Posted: Mon Jun 28, 2004 10:11 pm
by PWS32
Hi Sparkie,

nice code !
howto can save a captured picture ?

Best regards,
Peter

Image capture

Posted: Mon Jun 28, 2004 10:24 pm
by chio
This comes from another threath but it works really well.


source.s="source.bmp" ;Switch "source.bmp" to your file
dest.s="dest.jpg" ;Also edit the destination
quality.l=7 ;7 is default. From 0-10
LoadImage(0,source.s)
UseJPEGImageEncoder()
SaveImage(0,dest.s,#PB_ImagePlugin_JPEG,quality.l)

Posted: Mon Jun 28, 2004 11:21 pm
by Sparkie
PWS32 wrote:Hi Sparkie,

nice code !
howto can save a captured picture ?

Best regards,
Peter
Thanks PWS32. As I've said before, this is new territory for me so there may well be a better way to do this. ;)

I start off by creating an image placeholder for my frame image...

Code: Select all

CreateImage(0, 320, 240)
Then whenever I want to grab a frame from the preview window, I copy it to and from the clipboard, then draw to my Image(0)...

Code: Select all

SendMessage_(handle_to_your_capture_preview_window, #WM_CAP_GRAB_FRAME_NOSTOP, 0, 0)
          SendMessage_(handle_to_your_capture_preview_window, #WM_CAP_EDIT_COPY, 0, 0)
          img = GetClipboardData(#PB_ClipboardImage)
          StartDrawing(ImageOutput())
          DrawImage(img, 0, 0)
          StopDrawing()
Then to save it...

Code: Select all

SaveImage(0, your_file_name$, #PB_ImagePlugin_JPEG, 7)

Posted: Tue Jun 29, 2004 12:43 pm
by Wolf
Hi Sparkie.

Thank you for your nice and small code.

It's work now very good.

It's GREAT :D

Posted: Tue Jun 29, 2004 4:58 pm
by Wolf
Sparkie i have small problem.

I can't convert your code for save image.

When compile show error on the #WM_CAP_GRAB_FRAME_NOSTOP .

Can you fix your code for save webcam to pic ?

I don't need to any window for show live webcam.

Thanks :D


Best regards.

Posted: Tue Jun 29, 2004 5:08 pm
by PWS32
Hi,

im have the same problem and have remove the line:
SendMessage_(hWndC, #WM_CAP_GRAB_FRAME_NOSTOP, 0, 0)

and have add:
UseJPEGImageEncoder()

this works fine
cant understand what is #WM_CAP_GRAB_FRAME_NOSTOP
im found not information for this by google

Best regards,
Peter

Code: Select all

Repeat 
  Event = WaitWindowEvent() 
  If Event = #PB_EventGadget
    GadgetID = EventGadgetID()
    If GadgetID = #Button_0
      ;SendMessage_(hWndC, #WM_CAP_GRAB_FRAME_NOSTOP, 0, 0) 
          SendMessage_(hWndC, #WM_CAP_EDIT_COPY, 0, 0) 
          img = GetClipboardData(#PB_ClipboardImage) 
          StartDrawing(ImageOutput()) 
          DrawImage(img, 0, 0) 
          StopDrawing()
          UseJPEGImageEncoder()
          SaveImage(0, "c:\test.jpg", #PB_ImagePlugin_JPEG, 7)
    EndIf
  EndIf
Until Event = #PB_Event_CloseWindow