Help with webcam or video stream

Just starting out? Need help? Post your questions and find answers here.
chio
User
User
Posts: 30
Joined: Thu Jun 24, 2004 1:55 pm
Location: New York City
Contact:

Help with webcam or video stream

Post 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
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post 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.
Last edited by Sparkie on Mon Jun 28, 2004 1:20 pm, edited 2 times in total.
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
Wolf
Enthusiast
Enthusiast
Posts: 232
Joined: Sat Apr 03, 2004 12:00 pm
Location: S.T

Post 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.
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post 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.
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post 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
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
chio
User
User
Posts: 30
Joined: Thu Jun 24, 2004 1:55 pm
Location: New York City
Contact:

Awesome thanks

Post 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
chio
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post 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. :)
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
chio
User
User
Posts: 30
Joined: Thu Jun 24, 2004 1:55 pm
Location: New York City
Contact:

Same her

Post 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
chio
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Re: Same her

Post 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:
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
PWS32
User
User
Posts: 85
Joined: Sat May 10, 2003 1:02 pm
Location: Germany

Post by PWS32 »

Hi Sparkie,

nice code !
howto can save a captured picture ?

Best regards,
Peter
chio
User
User
Posts: 30
Joined: Thu Jun 24, 2004 1:55 pm
Location: New York City
Contact:

Image capture

Post 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)
chio
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post 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)
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
Wolf
Enthusiast
Enthusiast
Posts: 232
Joined: Sat Apr 03, 2004 12:00 pm
Location: S.T

Post by Wolf »

Hi Sparkie.

Thank you for your nice and small code.

It's work now very good.

It's GREAT :D
Wolf
Enthusiast
Enthusiast
Posts: 232
Joined: Sat Apr 03, 2004 12:00 pm
Location: S.T

Post 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.
PWS32
User
User
Posts: 85
Joined: Sat May 10, 2003 1:02 pm
Location: Germany

Post 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 
Post Reply