Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WebCam picture saving help 1

Status
Not open for further replies.

griffitd

Programmer
Aug 20, 2002
189
0
0
GB
Hi I have a project that connects to a webcam.

When ever I click the save picture button i get a blank pic.

Can anyone help please.

Module:
Option Explicit

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


Form:
Option Explicit

Const ws_child As Long = &H40000000
Const ws_visible As Long = &H10000000

Const WM_USER = 1024
Const wm_cap_driver_connect = WM_USER + 10
Const wm_cap_set_preview = WM_USER + 50
Const WM_CAP_SET_PREVIEWRATE = WM_USER + 52
Const WM_CAP_DRIVER_DISCONNECT As Long = WM_USER + 11
Const WM_CAP_DLG_VIDEOFORMAT As Long = WM_USER + 41

Private hwdc As Long
Private startcap As Boolean

Private Sub cmdCapture_Click()
Dim temp As Long

hwdc = capCreateCaptureWindow("Dixanta Vision System", _
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 cmdSave_Click()
SavePicture Picture1.Image, "c:\Test.bmp"
MsgBox "Picture Saved as: c:\test.bmp"
End Sub

Private Sub cmdVideoFormat_Click()
Dim temp As Long

If startcap = True Then
temp = SendMessage(hwdc, WM_CAP_DLG_VIDEOFORMAT, 0&, 0&)
Else
MsgBox "Please Connect First"
End If
End Sub


Form has the 4 buttons and 1 picture box


Thanks
 
Picture1 is not actually used by the cam preview (except as the source of an hWnd from which it creates its own hwnd).

So you either need to write your own picture saving routine using any given hWnd as a source, or you need to copy the image to a picture control. The latter is fractionally easier, so you might chagte your cmdSave_Click code as follows:
Code:
[blue][b]Const WM_CAP As Integer = &H400
Const WM_CAP_EDIT_COPY As Long = WM_CAP + 30[/b]

Private Sub cmdSave_Click()
    [b]SendMessage hwdc, WM_CAP_EDIT_COPY, 0, 0
    Picture1.Picture = Clipboard.GetData[/b]
    SavePicture Picture1.Image, "c:\Test.bmp"
    MsgBox "Picture Saved as: c:\test.bmp"
End Sub[/blue]

See my code in thread222-853019 for a longer example
 
I'm not a fan of programs hijacking user's resources (like the clipboard) but it does provide a quick alternative. There are also those other techniques using a bit more code.

But a recent webcam should have WIA drivers and unless you need to support Win9x you should have WIA 1.0 or preferably 2.0 to work with. There are a bunch of image capture and processing operations in WIA, which in many ways was the successor to the old Kodak stuff in Win9x. This makes it easier to grab the webcam bitmap and convert it to a compressed form such as JPEG fairly easily.

Sorry, no webcam handy here and no prewritten WIA sample to offer. Most of the WIA samples on hand are for scanners.
 
Thanks for the replies.

Got it working

Cheers
 
Hi

im now using
Call SendMessage(mCapHwnd, WM_CAP_DLG_VIDEOFORMAT, 0, 0)
to display thye video settings.

can i set these in code?
can i determine the location of the pop up window

Thanks
 
>can i set these in code?

Which ones would you want to set? Almost all are simply reports of the current status. For example, you wouldn't want or need to set the number of frames processed during the current streaming capture

Those that you can influence (image size, for example - see below)are set elsewhere

>can i determine the location of the pop up window

Er ... did you actually look at my example code linked earlier? It demonstrates how to open the preview window anywhere (and any size) you like, how to change the preview rate, how to scale the preview. It also includes somewhat better decalrations than the ones you are using, as the parameters are named more meaningfully and thus perhaps help in identifying what they actually are. For example:

Private Declare Function capCreateCaptureWindowA Lib "avicap32.dll" (ByVal lpszWindowName As String, ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal nID As Long) As Long

versus your

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


Investigating WM_CAP_SET_VIDEOFORMAT will allow you to change the actual resolution/image sizxe the camera captures at (for an example of how you might use this see my example down the bottom of thread222-1445606)

Tony, I'm not sure what additional material your link provides beyond that already in given in this thread (particulalry since its insistence on arbitarily renaming a bunch of SDK constants makes it difficult to compare to other examples or to look them up on MSDN)
 
Mike - To be honest, I had not looked through the whole thread in any detail. I noted the OP's question and glanced at the rest & threw the comment in there as the link had been useful to me a couple of years ago when I was doing some similar stuff. Had I paid sufficient attention to see what you'd already added, I would not have bothered.

BTW I plan to archive the referenced thread as your code looks useful (as always) and, though I'm not doing any webcam stuff right now, it might come in handy in the future. So thanks! ->*

Tony
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top