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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DriveListBox for Cameras

Status
Not open for further replies.
Nov 15, 2000
322
US
VB6.
Application has a form that allows users to import images. It uses a DriveListBox control to set the source of the images. This has been working fine, but I know it's only a matter of time before users begin upgrading their cameras and XP stops giving the drive letters.

Any suggestions on another control that can be used that will show the Digital Cameras that XP has detected and not just actual drive letters? I have something resembling a budget for this, so it doesn't have to be freeware.

If not, any tips on how to make XP see a camera as a drive letter? I saw a thread on here regarding an SDK from the camera manufacturer. I don't think this will work because my app is "in the wild" and I have no control over the brand or models of cameras that will be used.

Monkeylizard
Sometimes just a few hours of trial and error debugging can save minutes of reading manuals.
 
If you are getting the camera using the USB port you can import it directly and save it to a path & filename you choose with the commondialog control.
If this what you want to do I can post the code. It is quite simple and even caprures frames from my Sony movie camera as long as I have the Sony USB driver installed.
 
Here is one method written by member strongm. I don't know the exact thread.....

This is generic code, and should work with most - if not all - USB webcams. You'll need a form with 3 command buttons, a listbox (to show all the webcams that the PC knows about), a picturebox (to dsplay what the webcam is capturing, and a common dialog control. Then just drop in this code:




Const WM_CAP As Integer = &H400

Const WM_CAP_DRIVER_CONNECT As Long = WM_CAP + 10
Const WM_CAP_DRIVER_DISCONNECT As Long = WM_CAP + 11
Const WM_CAP_EDIT_COPY As Long = WM_CAP + 30

Const WM_CAP_SET_PREVIEW As Long = WM_CAP + 50
Const WM_CAP_SET_PREVIEWRATE As Long = WM_CAP + 52
Const WM_CAP_SET_SCALE As Long = WM_CAP + 53
Const WS_CHILD As Long = &H40000000
Const WS_VISIBLE As Long = &H10000000
Const SWP_NOMOVE As Long = &H2
Const SWP_NOSIZE As Integer = 1
Const SWP_NOZORDER As Integer = &H4
Const HWND_BOTTOM As Integer = 1

Dim iDevice As Long ' Current device ID
Dim hHwnd As Long ' Handle to preview window

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Function DestroyWindow Lib "user32" (ByVal hndw As Long) As Boolean
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 Integer, ByVal hWndParent As Long, ByVal nID As Long) As Long
Private Declare Function capGetDriverDescriptionA Lib "avicap32.dll" (ByVal wDriver As Long, ByVal lpszName As String, ByVal cbName As Long, ByVal lpszVer As String, ByVal cbVer As Long) As Boolean

Private Sub cmdSave_Click()
Dim bm As Image

' Copy image to clipboard
SendMessage hHwnd, WM_CAP_EDIT_COPY, 0, 0
ClosePreviewWindow

picCapture.Picture = Clipboard.GetData

CommonDialog1.CancelError = True
CommonDialog1.FileName = "Webcam1"
CommonDialog1.Filter = "Bitmap |*.bmp"

On Error GoTo NoSave
CommonDialog1.ShowSave

SavePicture picCapture.Image, CommonDialog1.FileName
NoSave:
cmdStop.Enabled = False
cmdSave.Enabled = False
cmdStart.Enabled = True
End Sub

Private Sub cmdStart_Click()
iDevice = lstDevices.ListIndex
OpenPreviewWindow
End Sub

Private Sub cmdStop_Click()
ClosePreviewWindow
cmdStop.Enabled = False
cmdSave.Enabled = False
cmdStart.Enabled = True
End Sub

Private Sub Form_Load()
LoadDeviceList

If lstDevices.ListCount > 0 Then
lstDevices.Selected(0) = True
Else
cmdStart.Enabled = False
lstDevices.AddItem ("No Device Available")
End If

cmdStop.Enabled = False
cmdSave.Enabled = False
End Sub

Private Sub LoadDeviceList()
Dim strName As String
Dim strVer As String
Dim iReturn As Boolean
Dim x As Long

x = 0
strName = Space(100)
strVer = Space(100)

' Load name of all available devices into lstDevices
Do
' Get Driver name and version
iReturn = capGetDriverDescriptionA(x, strName, 100, strVer, 100)
' If there was a device add device name to the list
If iReturn Then lstDevices.AddItem Trim$(strName)
x = x + 1
Loop Until iReturn = False
End Sub

Private Sub OpenPreviewWindow()

' Open Preview window in picturebox
hHwnd = capCreateCaptureWindowA(iDevice, WS_VISIBLE Or WS_CHILD, 0, 0, 640, 480, picCapture.hwnd, 0)

' Connect to device
If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) Then

'Set the preview scale
SendMessage hHwnd, WM_CAP_SET_SCALE, True, 0

'Set the preview rate in milliseconds
SendMessage hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0

'Start previewing the image from the camera
SendMessage hHwnd, WM_CAP_SET_PREVIEW, True, 0

' Resize window to fit in picturebox
SetWindowPos hHwnd, HWND_BOTTOM, 0, 0, picCapture.ScaleWidth, picCapture.ScaleHeight, SWP_NOMOVE Or SWP_NOZORDER

cmdSave.Enabled = True
cmdStop.Enabled = True
cmdStart.Enabled = False
Else

' Error connecting to device close window
DestroyWindow hHwnd

cmdSave.Enabled = False
End If
End Sub

Private Sub ClosePreviewWindow()
' Disconnect from device
SendMessage hHwnd, WM_CAP_DRIVER_DISCONNECT, iDevice, 0

' close window
DestroyWindow hHwnd
End Sub

Private Sub Form_Unload(Cancel As Integer)
If cmdStop.Enabled Then
ClosePreviewWindow
End If
End Sub
 
The thread you got it from, ted, is thread708-1318952

However, that was a simply me repeating something I posted 3 years ago (the search engine was down at the time, so I didn't have the original thread reference - but I do now: thread222-853019. This earlier thread contains some additional links that may be of interest)
 
Thanks strongm for the advice
I have a curious problem with all the webcam examples I have found that you might be able to shed some light on.
When I put 2 different webcams on the one computer, you always get a dialog box (appears to be part of avicap32.dll)that invites you to select the second camera. It finds the first camera automatically but not the second.
I can make both cameras work in 2 different windows OK but only after selecting the second camera on this dialog box.
Do you know any way of bypassing this dialog so I can immediately see both cameras after the app runs without any intervention?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top