webcam problem | Member | | Join Date: Sep 2007
Posts: 43
| | |
I have made a picturebox and a coding witch takes a picture in the picturebox, but the problem is that I need to save it as a image file before I can use getpixel, so when I use a save picturebox as image then VB.NET returns an error with image not exists,
| | Moderator | | Join Date: Oct 2006 Location: Australia
Posts: 7,748
| | | re: webcam problem
Can you show us what you've attempted so far, that doesn't work?
|  | Familiar Sight | | Join Date: Mar 2007 Location: igirisu~
Posts: 184
| | | re: webcam problem
I know what Patr's talking about, and I'd like to know how to do this too.
Debasisdas has made an example of how to capture from a webcam and "display the live video in a picturebox". However, what Windows is actually doing is displaying the picture on top of the picture box (known as overlaying). Therefore the picturebox actually still is empty, meaning that GetPixel() will fail to give back colours of the camera's images, and SavePicture() will obviously also fail to save the images.
We need a way to actually set the contents of the picturebox, and the easiest way seems to be by saving an image from the camera and loading it into the picturebox, where it can then be used as any other loaded image file. The downside to this is that the frame rate would probably be very low (probably no more than 4 fps or so) due to overheads from the file system (or possibly the hard disk itself, reading/writing the file over and over).
EDIT: I'm using VB6 and Windows XP, by the way, and this still happens. However, I tried using MCI to play a video in a picture box (this being related to webcams because MCI also just overlays in XP), on an extremely old computer running Windows 98, and GetPixel() worked - I think that may be because GetPixel() works differently on 98 compared to XP, rather than anything else.
EDIT2: Stupidly, it didn't occur to me at the time to try SavePicture(), which would confirm whether it was just GetPixel() being different on 98, or if 98 was actually placing the picture into the picturebox
.
| | Member | | Join Date: Sep 2007
Posts: 43
| | | re: webcam problem
that is exactly what I am talking about.
| | Moderator | | Join Date: Oct 2006 Location: Australia
Posts: 7,748
| | | re: webcam problem
Thanks for the input, Robbie.
I don't know the answer, but it occurs to me there may be another workaround. Not a really elegant way to go, but possibly faster. That is, use the API call (I forget the details, this is probably going back over 5 years) which is typically used by screensavers to grab the desktop.
When you write a screensaver, typically the first thing you do (at least in the simple examples I coded, way back) is to grab a snapshot of the current display, so you can play with it. It should be possible either do this then throw away the part you don't want, or if possible, grab just the part where your picturebox lives.
If interested, you should be able to find plenty of examples by searching for info on VB and screen savers.
| | Newbie | | Join Date: Feb 2008
Posts: 1
| | | re: webcam problem
Ok ok the answer is actually very simple. i made an instant messanger with webcam and voice about a year ago. for the image thing just get the handle of the picturebox.
camHwnd = capCreateCaptureWindow(CamWnd, 0, 0, 0, 320, 240, me.hwnd, 0)
Then all you have to do is save like this:
SavePicture Picture1.picture, Path
Then Reload the picture like this:
Picture1.picture = loadpicture(path )
thats is.
if thats what you were doing that should werk. im testing right now and after getting the picture reloaded into the picturebox i am able to use getpixel function on it.
P.S. the frame rate on my webcam is about twice as good as the frame rate of yahoo instant messanger's webcam. my computer specs. are: xp pro, AMD 64 moblie Athlon, 1gig ram. like i said the saving and opening of the image does slow the framerate but if you broad casting over the internet your fps will well over exceed your streaming speeds.
| | Newbie | | Join Date: Feb 2008
Posts: 4
| | | re: webcam problem
Hi, I'm having the exact same problem. I am trying to detect certain pixels from a video streaming from a webcam (don't really need to detect the entire webcam image, just 10 or 20 pixels of it to see if they change).
But I can't get it working! I've followed this thread and this is the code I've ended up with:
This is the code in a module (.bas file): -
Option Explicit
-
-
Public Const ws_child As Long = &H40000000
-
Public Const ws_visible As Long = &H10000000
-
Global Const WM_USER = 1024
-
Global Const wm_cap_driver_connect = WM_USER + 10
-
Global Const wm_cap_set_preview = WM_USER + 50
-
Global Const WM_CAP_SET_PREVIEWRATE = WM_USER + 52
-
Global Const WM_CAP_DRIVER_DISCONNECT As Long = WM_USER + 11
-
Public Const WM_CAP_DLG_VIDEOFORMAT As Long = WM_USER + 41
-
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
-
Declare Function capCreateCaptureWindow Lib "avicap32.dll" Alias "capCreateCaptureWindowA" (ByVal a As String, ByVal b As Long, ByVal c As Integer, ByVal d As Integer, ByVal e As Integer, ByVal f As Integer, ByVal g As Long, ByVal h As Integer) As Long
-
This is the code in my form: - Option Explicit
-
'General Declaration
-
Dim hwdc As Long
-
Dim startcap As Boolean
-
-
Private Sub cmdCapture_Click()
-
Dim temp As Long
-
-
hwdc = capCreateCaptureWindow("Debasis Das", ws_child Or ws_visible, 0, 0, 320, 240, Picture1.hWnd, 0)
-
If (hwdc <> 0) Then
-
temp = SendMessage(hwdc, wm_cap_driver_connect, 0, 0)
-
temp = SendMessage(hwdc, wm_cap_set_preview, 1, 0)
-
temp = SendMessage(hwdc, WM_CAP_SET_PREVIEWRATE, 30, 0)
-
startcap = True
-
Else
-
MsgBox ("No Webcam found")
-
End If
-
End Sub
-
-
Private Sub cmdClose_Click()
-
Dim temp As Long
-
If startcap = True Then
-
temp = SendMessage(hwdc, WM_CAP_DRIVER_DISCONNECT, 0&, 0&)
-
startcap = False
-
End If
-
End Sub
-
-
Private Sub cmdexit_Click()
-
Unload Me
-
End Sub
-
-
Private Sub cmdVideoFormat_Click()
-
Dim temp As Long
-
If startcap = True Then
-
temp = SendMessage(hwdc, WM_CAP_DLG_VIDEOFORMAT, 0&, 0&)
-
End If
-
End Sub
-
-
-
Private Sub Timer1_Timer()
-
Dim camwnd As String
-
Dim CamHwnd As String
-
Dim Path As String
-
- camwnd = Me.hWnd
- CamHwnd = capCreateCaptureWindow(camwnd, 0, 0, 0, 320, 240, Me.hWnd, 0)
-
-
Path = "C:\Test.jpg"
-
-
SavePicture Picture1.Picture, Path
-
Picture1.Picture = LoadPicture(Path)
-
-
Me.lblColor = Me.Picture1.Point(100, 100) 'Just a test, trying to get a pixel value
-
End Sub
-
The idea is that the timer will periodically (several times per second) check some pixel (in this example at x=100, y=100) and for now just print the value of this pixel in a label in the form.
But the program crashes at the SavePicture line, saying "Invalid property value", and then VB6 itself crashes.
I think that we're really close here. I've tried to fill the "camwnd" variable with both the name and hWnd of both the form and the picturebox, but there's no difference in the outcome.
Does anyone know how I can get the value of certain pixels in the video stream (preferrably without having to save it to a file and load it again)?
Thanks. :-)
| | Newbie | | Join Date: Feb 2008
Posts: 4
| | | re: webcam problem
Well, I just made the strangest breakthrough.
I removed the saving and loading of the image file, and replaced "Picture1.hwnd" with "Me.HWnd" in the capCreateCaptureWindow command at line 9 in my code, and... it works!!!
I have no idea why or how, but it works. The image is now drawn directly on the form's surface, and I can still use Me.Picture1.Point to get the value, which can then be split up in it's Red, Green and Blue components.
But I had to move the picturebox to position 0,0 (upper left corner in the form) and make it the same size as the picture from the webcam (320x240 pixels in my case). I also set the ScaleMode of both the form and picturebox to "Pixel".
I really don't have any idea why this works, but I don't really care as long as it does. :-)
| | Newbie | | Join Date: Feb 2008
Posts: 4
| | | re: webcam problem
BTW, if anyone's interested, this is the sub I use to get Red, Green and Blue from the color value: -
Public Function get_rgb(Color, rd, gr, bl)
-
Dim tmpColor
-
If Color < 0 Then Color = 0
-
-
tmpColor = Color / 65536
-
bl = Int(tmpColor)
-
tmpColor = (tmpColor - Int(tmpColor)) * 256
-
-
gr = Int(tmpColor)
-
tmpColor = (tmpColor - Int(tmpColor)) * 256
-
-
rd = Round(tmpColor)
-
End Function
-
You put in the color you get from the webcam (Long data type), and three variables for Red, Green, and Blue (normally data type Byte). These variables will then be filled with RGB values.
| | Moderator | | Join Date: Oct 2006 Location: Australia
Posts: 7,748
| | | re: webcam problem
Thanks for sharing that with us, rfoshaug. I've had to split colours like this a few times, too. I'd like to make a couple of small comments about this version. - Ideally, you should work with Long data type wherever possible, for performance reasons. It might seem crazy, but it's actually slightly faster to work with Long than with Byte. (On a 32 bit system, that is. 64 bit may be different).
- Integer division is faster, too. Try using \ instead of /.
- You're doing the same calculation twice, unnecessarily. In line 7, for instance, you should use bl rather than Int(tmpColor).
This is all just nitpicking, of course - if the routine works, great. :)
EDIT: Oops! The integer division thing (\) might not work with your method.
| | Newbie | | Join Date: Feb 2008
Posts: 4
| | | re: webcam problem
I've just discovered that these lines:
camwnd = Me.hWnd
CamHwnd = capCreateCaptureWindow(camwnd, 0, 0, 0, 320, 240, Me.hWnd, 0)
are not needed in the timer sub, and actually cause lockups and problems, so I'm removing them from the code in my earlier post.
Edit: It seems I can't edit that older post. Anyway, it's line 45 and 46 in the first code I posted, in the Timer sub.
| | Moderator | | Join Date: Oct 2006 Location: Australia
Posts: 7,748
| | | re: webcam problem Quote:
Originally Posted by rfoshaug I've just discovered .... I've edited it and put those two lines in italics. At least they will look slightly different, so when people get to this message they should know which lines you mean.
It might be better to just use the Reply function to copy the code from that message and re-post it here without those lines.
|  | Similar Visual Basic 4 / 5 / 6 bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,392 network members.
|