PDA

View Full Version : Internet Camera connection and picture capturing



ioptic
11-01-2009, 11:49
Hi all!

I wanted to capture a simple bmp picture from an USB internet camer and found myself hitting a wall of secrecy and can't do's.

Here is what I have so far learned:

USB is shared between many devices- much like the internet packets are send and received. Every packet must therefore have a senders id as well as a receipients id. The the packets are redirected to be received by the USB divise or by some software which is waiting for packets with its Id on it.

The date which is send in packets must eherefore have a header, date and a fotter EOF.

The trick is finding driver which can decifer the data back into a meaningful picture. The camera manufacturers, whilst using perhaps a Soni or Kodak chip can elect the send the data in any form they like, via the USB port. The chip, just line an ols fashion camera takes a snapshop and then sends it through the port. This then requires a driver on the computer side to capture the data and to render it into a picture.

Does anyone know if the data comming through the USB can be seen by thinbasic (with or without a driver) and if so, can it be turned into a sensible picture ?
I thought that eversince 2000 there was an common usb video pictute interface ?

Best regards
Rob
-o-o-

ErosOlmi
11-01-2009, 12:44
I think the way to go is to use standard Microsoft Windows AVICAP32.DLL dll unless you need to use specific and/or high professional cameras but at that point an interpreter language like thinBasic cannot be the right choice and you need to go with a compiled language like C or others.

Anyhow, some time ago I was experimenting with an USB webcam and Microsoft AVICAP32 and got the following script. I do not know if it works in all cases because I just tested it with my integrated laptop camera so you need to test it and see.

More info can be found here: http://msdn.microsoft.com/en-us/library/ms713779(VS.85).aspx

Here the script:


USES "UI"

'------------------------------------------------------------------------------
' VFW Constants used in the sample
'------------------------------------------------------------------------------
%WM_CAP_START = %WM_USER
%WM_CAP_SET_CALLBACK_STATUS = %WM_CAP_START + 3
%WM_CAP_SET_CALLBACK_YIELD = %WM_CAP_START + 4
%WM_CAP_SET_CALLBACK_FRAME = %WM_CAP_START + 5
%WM_CAP_SET_CALLBACK_VIDEOSTREAM = %WM_CAP_START + 6
%WM_CAP_DRIVER_CONNECT = %WM_CAP_START + 10
%WM_CAP_DRIVER_DISCONNECT = %WM_CAP_START + 11
%WM_CAP_DLG_VIDEOFORMAT = %WM_CAP_START + 41
%WM_CAP_DLG_VIDEOSOURCE = %WM_CAP_START + 42
%WM_CAP_SET_PREVIEW = %WM_CAP_START + 50
%WM_CAP_SET_PREVIEWRATE = %WM_CAP_START + 52
%WM_CAP_GET_STATUS = %WM_CAP_START + 54
%WM_CAP_SEQUENCE = %WM_CAP_START + 62
%WM_CAP_SEQUENCE_NOFILE = %WM_CAP_START + 63
%WM_CAP_SET_SEQUENCE_SETUP = %WM_CAP_START + 64
%WM_CAP_GET_SEQUENCE_SETUP = %WM_CAP_START + 65
'------------------------------------------------------------------------------
' VFW Structures used in the sample
'------------------------------------------------------------------------------
TYPE VIDEOHDR
lpData AS BYTE PTR
dwBufferLength AS DWORD
dwBytesUsed AS DWORD
dwTimeCaptured AS DWORD
dwUser AS DWORD
dwFlags AS DWORD
dwReserved(4) AS DWORD PTR
END TYPE
TYPE CAPSTATUS
uiImageWidth AS DWORD
uiImageHeight AS DWORD
fLiveWindow AS LONG
fOverlayWindow AS LONG
fScale AS LONG
ptScroll AS POINTAPI
fUsingDefaultPalette AS LONG
fAudioHardware AS LONG
fCapFileExists AS LONG
dwCurrentVideoFrame AS DWORD
dwCurrentVideoFramesDropped AS DWORD
dwCurrentWaveSamples AS DWORD
dwCurrentTimeElapsedMS AS DWORD
hPalCurrent AS LONG
fCapturingNow AS LONG
dwReturn AS DWORD
wNumVideoAllocated AS DWORD
wNumAudioAllocated AS DWORD
END TYPE
TYPE CAPTUREPARMS
dwRequestMicroSecPerFrame AS DWORD
fMakeUserHitOKToCapture AS LONG
wPercentDropForError AS DWORD
fYield AS LONG
dwIndexSize AS DWORD
wChunkGranularity AS DWORD
fUsingDOSMemory AS LONG
wNumVideoRequested AS DWORD
fCaptureAudio AS LONG
wNumAudioRequested AS DWORD
vKeyAbort AS DWORD
fAbortLeftMouse AS LONG
fAbortRightMouse AS LONG
fLimitEnabled AS LONG
wTimeLimit AS DWORD
fMCIControl AS LONG
fStepMCIDevice AS LONG
dwMCIStartTime AS DWORD
dwMCIStopTime AS DWORD
fStepCaptureAt2x AS LONG
wStepCaptureAverageFrames AS DWORD
dwAudioBufferSize AS DWORD
fDisableWriteCache AS LONG
AVStreamMaster AS DWORD
END TYPE

'------------------------------------------------------------------------------
' VFW Function used in the sample
'------------------------------------------------------------------------------
DECLARE FUNCTION capCreateCaptureWindow _
LIB "AVICAP32.DLL" ALIAS _
"capCreateCaptureWindowA" ( lpszWindowName AS ASCIIZ, _
BYVAL dwStyle AS DWORD, _
BYVAL x AS LONG, _
BYVAL y AS LONG, _
BYVAL nWidth AS LONG, _
BYVAL nHeight AS LONG, _
BYVAL hWnd AS DWORD, _
BYVAL nID AS LONG) AS LONG

'------------------------------------------------------------------------------
' VFW include END
'------------------------------------------------------------------------------



%IDFORMAT = 101
%IDSOURCE = 102
%IDEND = 103

GLOBAL hDlg AS LONG
GLOBAL lwndC AS LONG
GLOBAL CapSt AS CAPSTATUS
GLOBAL HalfHeight AS DWORD
global lIndex as long

'------------------------------------------------------------------------------
' Main
'------------------------------------------------------------------------------
FUNCTION TBMAIN() AS LONG
DIALOG NEW pixels, 0, "thinBasic webcam usage basic example", -1, -1, 644, 524, _
%DS_MODALFRAME OR _
%WS_CAPTION OR _
%WS_SYSMENU _
, 0 TO hDlg

CONTROL ADD BUTTON , hDlg, %IDFORMAT , "Format" , 5, 490, 60, 24, %WS_TABSTOP, CALL cbFormatButton
CONTROL ADD BUTTON , hDlg, %IDSOURCE , "Source" , 70, 490, 60, 24, %WS_TABSTOP, CALL cbSourceButton
CONTROL ADD BUTTON , hDlg, %IDEND , "End" , 140, 490, 60, 24, %WS_TABSTOP, CALL cbEndButton
DIALOG SHOW MODAL hDlg CALL cbDialog
END FUNCTION

'------------------------------------------------------------------------------
'DDT Dialog CallBack
'------------------------------------------------------------------------------
CALLBACK FUNCTION cbDialog() as long
SELECT CASE CBMSG
CASE %WM_INITDIALOG
lwndC = capCreateCaptureWindow( "VFW Capture Window", _
%WS_CHILD OR %WS_VISIBLE, _
2, 2, 640, 480, hDlg, 200)

IF lwndC <> %NULL THEN
'---Index of the capture driver. The index can range from 0 through 9.
lIndex = 0
'---Connects a capture window to a capture driver
SendMessage lwndC, %WM_CAP_DRIVER_CONNECT, lIndex, 0

'---Sets the frame display rate in preview mode. Rate, in milliseconds, at which new frames are captured and displayed
SendMessage lwndC, %WM_CAP_SET_PREVIEWRATE, 30, 0

'---enables or disables preview mode. In preview mode, frames are transferred from the capture hardware
'---to system memory and then displayed in the capture window using GDI functions
SendMessage lwndC, %WM_CAP_SET_PREVIEW, %True, 0

SendMessage lwndC, %WM_CAP_GET_STATUS, sizeof(CAPSTATUS), VARPTR(CapSt)
HalfHeight = CapSt.uiImageHeight /2
END IF
CASE %WM_CLOSE
IF lwndC <> %NULL THEN
'---disconnects a capture driver from a capture window
SendMessage lwndC, %WM_CAP_DRIVER_DISCONNECT, %NULL, %NULL
END IF
END SELECT
END FUNCTION

'------------------------------------------------------------------------------
'Format Button CallBack
'------------------------------------------------------------------------------
CALLBACK FUNCTION cbFormatButton() as long
If CBMSG = %WM_COMMAND Then
'---Check control specific message
if CBCTLMSG = %BN_CLICKED then
SendMessage lwndC, %WM_CAP_DLG_VIDEOFORMAT, 0, 0
SendMessage lwndC, %WM_CAP_GET_STATUS, sizeof(CAPSTATUS), VARPTR(CapSt)
HalfHeight = CapSt.uiImageHeight /2
dialog redraw cbhndl
end if
end if
END FUNCTION

'------------------------------------------------------------------------------
'Capture Button CallBack
'------------------------------------------------------------------------------
CALLBACK FUNCTION cbSourceButton() as long
If CBMSG = %WM_COMMAND Then
'---Check control specific message
if CBCTLMSG = %BN_CLICKED then
SendMessage lwndC, %WM_CAP_DLG_VIDEOSOURCE, 0, 0
SendMessage lwndC, %WM_CAP_GET_STATUS, sizeof(CAPSTATUS), VARPTR(CapSt)
HalfHeight = CapSt.uiImageHeight /2
end if
end if
END FUNCTION

'------------------------------------------------------------------------------
'End Button CallBack
'------------------------------------------------------------------------------
CALLBACK FUNCTION cbEndButton() as long
If CBMSG = %WM_COMMAND Then
'---Check control specific message
if CBCTLMSG = %BN_CLICKED then
DIALOG END CBHNDL
end if
end if
END FUNCTION

Michael Hartlef
11-01-2009, 12:53
That is a nice script. Thanks for sharing Eros.

ioptic
12-01-2009, 00:05
Super! Thanks

Can't wait to try it

regards
ROb
-o-o-

ioptic
14-01-2009, 12:23
Hi Erros,

just got around to try and run the code- IT's always very easy to read- the sign of a good programmer :)... but it gives me a blank screen only, on xp. Can you suggest any regular program to test if the camera is supported/working or the op version you used. I was looking for a univerally supported version. Perhaps I need to sniff out the OP version and then finetune the setting to suit. This camera came without a driver- ? so I am somewhat ctuck :) What do you think ?

Another issue is saving pics. Are they in memory, ie ould printscreen capture them or are they only being displayed in memory?

best regards
Rob
-o-o-

ErosOlmi
14-01-2009, 12:43
Rob,

check the following lines:


'---Index of the capture driver. The index can range from 0 through 9.
lIndex = 0
'---Connects a capture window to a capture driver
SendMessage lwndC, %WM_CAP_DRIVER_CONNECT, lIndex, 0


Try to change lIndex variable. It can assumes values from 0 to 9 and that index indicates the device you want to connect to. Maybe you have more than 1 video devices.
I think it is possible to call a function that list all the possible video input devices but I have to check MS documentation.

Regarding saving the DC content inot a BMP file it should be quite easy and there must be some code here in forum but right now I do not have the time (I'm at work and this week was very busy week) but I will do later as soon as I will have some spare time.

Let me know about changing the lIndex
Eros

ioptic
31-01-2009, 09:58
Hi Eros,

thanks again for the driver code.

Here is a compatibillity list:-

win 2000 - no
xp - yes under 400 (used lIndex = 3, probably becuase it had other twain driver - did not have a clean install of xp hand )
vista - yes - 640x480

It appears that windows stared supporting the internet video beginning with Xp. 2000 needs a driver before anything happens.
I suspected usb 1 or 2- but this is not related to OP versions web-video camera display issues.

Is it possible to capture the image in BMP ? ie. tried print screen and it was captured which may mean that is is located in the
win clipboard memory ?

best regards
Rob
-o-o-