IonicWind Software

IWBasic => General Questions => Topic started by: Boris on May 14, 2008, 02:35:34 AM

Title: Webcam images in Ebasic
Post by: Boris on May 14, 2008, 02:35:34 AM
Hi All.

How do I activate my USB webcam and display the feed in an Ebasic program? Also, how do I capture the image to perform various robot related functions? (Edge finding, Object finding, Face finding etc.)
Title: Re: Webcam images in Ebasic
Post by: pistol350 on May 14, 2008, 09:15:11 AM
Hi Boris!

It's been a long time!  :)

Here is a first example by Sapero that was in the Former Forum.
It uses the avicap32.dll library.
Hope that helps.


$include "windows_.inc"
$use "avicap32.lib"

declare import, capGetDriverDescriptionA(index:int, name:pointer, namelen:int, descr:pointer, descrlen:int),int
declare import, capCreateCaptureWindowA(lpszWindowName:POINTER,dwStyle:UINT,x:INT,y:INT,nWidth:INT,nHeight:INT, hWnd:UINT,nID:INT),UINT
declare import, PathFileExists alias PathFileExistsA(path:POINTER),INT
$command wsprintfA(...)

typedef BOOL INT
typedef HANDLE UINT

CONST WM_CAP_DRIVER_CONNECT       = 0x400 | 10
CONST WM_CAP_DRIVER_DISCONNECT    = 0x400 | 11
CONST WM_CAP_DRIVER_GET_CAPS      = 0x400 | 14
CONST WM_CAP_EDIT_COPY            = 0x400 | 30
CONST WM_CAP_DLG_VIDEOFORMAT      = 0x400 | 41
CONST WM_CAP_DLG_VIDEOSOURCE      = 0x400 | 42
CONST WM_CAP_DLG_VIDEODISPLAY     = 0x400 | 43
CONST WM_CAP_DLG_VIDEOCOMPRESSION = 0x400 | 46
CONST WM_CAP_SET_PREVIEW          = 0x400 | 50
CONST WM_CAP_SET_OVERLAY          = 0x400 | 51
CONST WM_CAP_SET_PREVIEWRATE      = 0x400 | 52
CONST WM_CAP_SET_SCALE            = 0x400 | 53
CONST WM_CAP_FILE_SAVEDIB         = 0x400 | 25

Const TBM_SETPOS     = 0x400+5
Const TBM_SETRANGE   = 0x400+6
Const TBM_SETTICFREQ = 0x400+20
Const TBM_GETPOS     = 0x400
Const TBS_AUTOTICKS  = 0x1
Const TBS_TOOLTIPS   = 0x100

TYPE CAPDRIVERCAPS
    UINT   wDeviceIndex        /* 0 to 9 */
    BOOL   fHasOverlay         /* device supports video overlay */
    BOOL   fHasDlgVideoSource  /* selecting and controlling the video source */
    BOOL   fHasDlgVideoFormat  /* selecting the video format */
    BOOL   fHasDlgVideoDisplay /* controlling the redisplay of video from the capture frame buffer */
    BOOL   fCaptureInitialized /* device has been successfully connected */
    BOOL   fDriverSuppliesPalettes  /* driver can create palettes */
    INT    unused[4]
ENDTYPE

WINDOW d1
UINT hWndCap1
CREATEDIALOG d1,0,0,382,330,0x80CB0080,0,"Caption",&handler
CONTROL d1,@GROUPBOX,"Group",7,9,270,309,0x50000007,1
CONTROL d1,@COMBOBOX,"",14,25,256,80,0x50A10642,144
CONTROL d1,@SYSBUTTON,"Adjust",14,286,85,24,0x50010000,142
CONTROL d1,@SYSBUTTON,"Copy",105,286,85,24,0x50010000,140
CONTROL d1,@SYSBUTTON,"Save",196,286,75,24,0x50010000,143
CONTROL d1,@EDIT,"",14,50,256,226,0x50800000,141  /*parent window for webcam*/
CONTROL d1,@BUTTON,"cancel",27,313,60,16,0x40080000,2

CONTROL d1,@STATIC,"Framerate",290,6,80,14,0x5000010B,7
CONTROLEX d1,"msctls_trackbar32","",292,22,80,20,@TABSTOP|TBS_AUTOTICKS|TBS_TOOLTIPS,0,200
CONTROL d1,@CHECKBOX,"Preview",298,300,70,15,0x50010003,201

CONTROL d1,@SYSBUTTON,"Video source",279,221,101,22,0x50010000,500
CONTROL d1,@SYSBUTTON,"Video format",279,243,101,22,0x50010000,501
CONTROL d1,@SYSBUTTON,"Video display",279,266,101,22,0x50010000,502
CONTROL d1,@SYSBUTTON,"Video compression",279,199,101,22,0x50010000,499

const BTN_ADJUST   = 142 /* free */
const BTN_SAVEFR   = 143
const BTN_COPY     = 140
const BTN_SETVCOMP = 499
const BTN_SETVSRC  = 500 /*BTN_SETSRC+0*/
const BTN_SETVFMT  = 501 /*BTN_SETSRC+1*/
const BTN_SETVDSP  = 502 /*BTN_SETSRC+2*/
const COMBO        = 144
const CAPPARENT    = 141
const FRAMERATE    = 200
const sFRAMERATE   = 7   /*static*/
const PREVIEW      = 201



showdialog d1
waituntil d1=0


sub handler
   STRING name, descr
   INT L,T,W,H
   INT a
   select @MESSAGE
      case @IDINITDIALOG

         CenterWindow d1
         SetControlColor d1, CAPPARENT, 0, 0
         SetState d1, PREVIEW, TRUE
         Addstring d1, COMBO, "None"
         SetControlColor d1, FRAMERATE, 0, _GetSysColor(15)
         SendMessage d1, TBM_SETRANGE, TRUE, 50<<16|1, FRAMERATE
         SendMessage d1, TBM_SETTICFREQ, 8, 0, FRAMERATE
         SendMessage d1, TBM_SETPOS, TRUE, 15, FRAMERATE

         ' enum available capture drivers
         for a = 0 to 9
            if FALSE = capGetDriverDescriptionA(a, name, 255, descr, 255) _
               then breakfor
            addstring d1, COMBO, name
         next a

         ' select the first driver
         setselected d1, COMBO, (a>0)

         ' create preview window
         getsize(d1, L,T,W,H,CAPPARENT)
         hWndCap1 = capCreateCaptureWindowA("capture 1", 0x50000000, 0,0,W,H, getcontrolhandle(d1, CAPPARENT), 20)

         ' and run
         capStart()

      case @IDCONTROL
         select @CONTROLID

            case COMBO
               if @NOTIFYCODE = @CBNSELCHANGE
                  capStart(getselected(d1, COMBO))
               endif

            case @IDCANCEL
               sendmessage d1, @IDCANCEL, 0,0


         endselect
         if @notifycode = 0
            select @CONTROLID
'               case BTN_ADJUST
               case PREVIEW     : capSerPreview()
               case BTN_COPY    : sendmessage hWndCap1, WM_CAP_EDIT_COPY, 0, 0
               case BTN_SAVEFR  : capSaveFrame()
               case BTN_SETVCOMP: sendmessage hWndCap1, WM_CAP_DLG_VIDEOCOMPRESSION, 0, 0
               case BTN_SETVSRC : sendmessage hWndCap1, WM_CAP_DLG_VIDEOSOURCE, 0, 0
               case BTN_SETVFMT : sendmessage hWndCap1, WM_CAP_DLG_VIDEOFORMAT, 0, 0
               case BTN_SETVDSP : sendmessage hWndCap1, WM_CAP_DLG_VIDEODISPLAY, 0, 0
            endselect
         endif


      case @IDCLOSEWINDOW
      case& @IDCANCEL
         capDisonnect()
         closedialog d1

      case @IDHSCROLL
         a=@LPARAM&0xFFFF
         if a then capSetFramerate(a)

      case @IDMOVE
         capSetFramerate(0)
         starttimer d1, 500, 1000

      case @IDTIMER
         if @WPARAM=1000
            stoptimer d1, 1000
            capSetFramerate()
         endif

   endselect
   return
endsub



' index 1-10  or 0 if only stop
sub capStart(opt index=1:int)
   CAPDRIVERCAPS cdc

   if hWndCap1=0 then return

   capDisonnect()
   if index=0 then return
   index--
   capConnect(index)
   capSetFramerate()
   SendMessage hWndCap1, WM_CAP_SET_OVERLAY, TRUE, 0
   capSetScale(FALSE)
   capSerPreview()

   ' setup controls
   EnableControl d1, BTN_SETVSRC, FALSE
   EnableControl d1, BTN_SETVFMT, FALSE
   EnableControl d1, BTN_SETVDSP, FALSE
   if SendMessage(hWndCap1, WM_CAP_DRIVER_GET_CAPS, 44, &cdc)
      EnableControl d1, BTN_SETVSRC, cdc.fHasDlgVideoSource
      EnableControl d1, BTN_SETVFMT, cdc.fHasDlgVideoFormat
      EnableControl d1, BTN_SETVDSP, cdc.fHasDlgVideoDisplay
   endif

   return
endsub




sub capSetFramerate(opt frq_hz=-1:int)
   INT rate

   if hWndCap1=0 then return

   if frq_hz = -1
      frq_hz = sendmessage(d1, TBM_GETPOS,0,0,FRAMERATE)
   endif

   rate=0
   if frq_hz then rate = 1000/frq_hz

   setcaption d1, str$(rate)+STR$(frq_hz)
   SetControlText d1, sFRAMERATE, using("Framerate ##Hz  ", frq_hz)
   SendMessage hWndCap1, WM_CAP_SET_PREVIEWRATE, rate, 0
   return
endsub




sub capConnect(index:int)
   SendMessage hWndCap1, WM_CAP_DRIVER_CONNECT, index, 0
   return
endsub




sub capDisonnect
   SendMessage hWndCap1, WM_CAP_DRIVER_DISCONNECT, 0, 0
   return
endsub


'enables or disables scaling of the preview video images. If scaling is enabled,
'the captured video frame is stretched to the dimensions of the capture window
sub capSetScale(enable:int)
   SendMessage hWndCap1, WM_CAP_SET_SCALE, enable, 0
   return
endsub


sub capSerPreview(opt enable=-1:int)
   if enable=-1
      enable=getstate(d1, PREVIEW)
   endif
   SendMessage hWndCap1, WM_CAP_SET_PREVIEW, enable, 0
   return
endsub



sub capSaveFrame
   ISTRING path[300]
   INT cnt
   UINT path_end

   IF hWndCap1=0 then return

   path = GetStartPath
   path_end = &path + len(path)

   for cnt = 1 to 0x7FFFFFFF
      wsprintfA *<STRING>path_end, "%.5d.bmp", cnt
      if PathFileExists(path) = FALSE
         SendMessage hWndCap1, WM_CAP_FILE_SAVEDIB, 0, &path
         SetCaption d1, "saved as " + *<STRING>path_end
         BreakFor
      endif
   next cnt

   return
endsub
Title: Re: Webcam images in Ebasic
Post by: Boris on May 14, 2008, 12:57:52 PM
Thanks.

Where to get "windows_.inc" ?

And I have an error on this line: $command wsprintfA(...)

Do I need a newer version of Ebasic? (Current version is 1.59)
Title: Re: Webcam images in Ebasic
Post by: pistol350 on May 14, 2008, 01:22:03 PM
Oops!

Sorry, i forgot to rename the header to "windows.inc"
The header is the original "windows.inc" file.
I renamed it so that it won't be overwritten when i install Sapero's header pak.
it should compile fine with version 1.59


PS: for some very weird reason,
when i try to compile that code with my version 1.62 of the compiler, the parser crashes.  ???
It seems that the line causing that crash is line 31

Const TBS_AUTOTICKS  = 0x1

I then tried to use that same line in a test program and it compiled fine.
That really drives you insane  ???

Any suggestions please ?

With Ibasic Pro, It compiles fine without any change to the code.
Title: Re: Webcam images in Ebasic
Post by: Boris on May 14, 2008, 01:47:37 PM
Thanks.

Still getting errors though.

Compiling...
webcam.eba
File: C:\Documents and Settings\Administrator\My Documents\webcam.eba (7) duplicate declaration
File: C:\Documents and Settings\Administrator\My Documents\webcam.eba (94) Warning: undeclared function '_GetSysColor'
File: C:\Documents and Settings\Administrator\My Documents\webcam.eba (263) syntax error - <


The 'duplicate declaration' error is on the '$command wsprintfA(...)' line. If I remove that line, I still get the other two errors.

Any ideas?
Title: Re: Webcam images in Ebasic
Post by: pistol350 on May 14, 2008, 01:55:57 PM
Maybe changing "_GetSysColor" to "GetSysColor" can do the job.
Depending on how it was declared in your "windows.inc" file.
Title: Re: Webcam images in Ebasic
Post by: sapero on May 14, 2008, 02:33:23 PM
This line is crashing the parser:
setselected d1, IDC_CAMERA, (a>0)
Fix:
setselected d1, IDC_CAMERA, 0+(a>0)

Attached working version for windows.inc from the SDK pak.
Title: Re: Webcam images in Ebasic
Post by: Ionic Wind Support Team on May 14, 2008, 02:38:17 PM
Aware of it.  Boolean operators as direct parameter for a function are causing me grief somewhere.  Doesn't always happen though.

Paul.
Title: Re: Webcam images in Ebasic
Post by: Ionic Wind Support Team on May 14, 2008, 02:54:07 PM
Update posted in the subscribers downloads section.
Title: Re: Webcam images in Ebasic
Post by: pistol350 on May 14, 2008, 03:03:30 PM
bug fixed Indeed.
The code compiles fine now  8)
Title: Re: Webcam images in Ebasic
Post by: Andre on June 29, 2008, 06:08:01 AM
I tried to compile the version posted above  by pistol 350 and I get a parsing error that claims that some "xxx" memory cannot be written.

Then I tried to compile the code posted in the zip file by sapero, and the compiler can't find the "commctrl.inc", "vfw.inc" and "shlwapi.inc". Where do I find those?

Or even better, does anyone have a simple little application that displays the webcam output?
Title: Re: Webcam images in Ebasic
Post by: pistol350 on June 29, 2008, 06:31:22 AM
hi Andre!

First of all, the code i posted shoud compile if you have the latest version of Ebasic.
A bug in the version "..." ( ::) i don't remember) of the compiler or parser caused the program not to compile but Paul corrected it.


To get the sample code posted by Sapero compile you need to install his SDK pak.
A link to this pak is attached to every post by Sapero. here is the link :
http://www.ionicwind.com/forums/index.php/topic,633.msg20824.html#msg20824

BTW, i advise you to go to your "includes" folder and rename the header "windows.inc" to whatever you want,
doing so will prevent it from being overwritten by the other "windows.inc" contained into the install pak.

After installing the pak, the code above should compile fine.
Title: Re: Webcam images in Ebasic
Post by: Andre on June 29, 2008, 09:08:24 AM
Thank you pistol350,

I had version 1.62 running, I thought it was enough, but after I did a full installation of version 1.63  the code is compiling fine.

Before posting my question, I spent about an hour trying to find Sapero's SDK pak, using the search engine. If it wasn't for your help I could have spent a couple of days or even never find it. It's so well hidden under our noses! ;D

Title: Re: Webcam images in Ebasic
Post by: Andre on June 29, 2008, 02:38:53 PM
Oops.

I'm happy with my results using pistol350's code but, just for the record, trying to compile sapero's code gives the following errors:


Compiling...
webcam.eba
File: commctrl.inc (367) duplicate declaration - HIMAGELIST
File: commctrl.inc (368) duplicate declaration - BOOL
File: commctrl.inc (370) duplicate declaration - int
File: commctrl.inc (371) duplicate declaration - BOOL
File: commctrl.inc (373) duplicate declaration - int
File: commctrl.inc (375) duplicate declaration - int
File: commctrl.inc (376) duplicate declaration - COLORREF
File: commctrl.inc (377) duplicate declaration - COLORREF
File: commctrl.inc (378) duplicate declaration - BOOL
File: commctrl.inc (406) duplicate declaration - BOOL
File: commctrl.inc (407) duplicate declaration - BOOL
File: commctrl.inc (408) duplicate declaration - int
File: commctrl.inc (409) duplicate declaration - BOOL
File: commctrl.inc (410) duplicate declaration - BOOL
File: commctrl.inc (411) duplicate declaration - BOOL
File: commctrl.inc (412) duplicate declaration - HICON
File: commctrl.inc (413) duplicate declaration - HIMAGELIST
File: commctrl.inc (414) duplicate declaration - HIMAGELIST
File: commctrl.inc (424) duplicate declaration - BOOL
File: commctrl.inc (425) duplicate declaration - BOOL
File: commctrl.inc (426) duplicate declaration
File: commctrl.inc (427) duplicate declaration - BOOL
File: commctrl.inc (428) duplicate declaration - BOOL
File: commctrl.inc (429) duplicate declaration - BOOL
File: commctrl.inc (430) duplicate declaration - BOOL
File: commctrl.inc (431) duplicate declaration - BOOL
File: commctrl.inc (432) duplicate declaration - HIMAGELIST
File: commctrl.inc (465) duplicate declaration - BOOL
File: commctrl.inc (466) duplicate declaration - BOOL
File: commctrl.inc (467) duplicate declaration - BOOL
File: commctrl.inc (468) duplicate declaration - HIMAGELIST
File: commctrl.inc (469) duplicate declaration - HIMAGELIST
Error(s) in compiling "C:\Arquivos de programas\Ionic Wind\EBDev\Programas\webcam.eba"

Title: Re: Webcam images in Ebasic
Post by: sapero on June 29, 2008, 04:21:50 PM
Andre, the line numbers are pointing to ImageList_** function declarations. You must have included these functions, maybe in a custom .incc file?
Show us what do you $include.
Title: Re: Webcam images in Ebasic
Post by: Andre on June 29, 2008, 05:20:31 PM
Hi sapero,

I'm not sure I understand what you mean. I have not yet written any code. I tried to compile the very sourcecode you posted in the zip file, that begins with:

$include "windows.inc"
$include "commctrl.inc"
$include "vfw.inc"
$include "shlwapi.inc"


The include files I have in the Ebasic folder are the ones that come with the compiler, the ones in your SDK pack and one for a port.dll.

Do I miss something?

Title: Re: Webcam images in Ebasic
Post by: pistol350 on June 29, 2008, 05:58:06 PM
Hi Andre!

disable  $include "commctrl.inc" and add these lines below
and the program should compile fine.

$define TBM_GETPOS              (0x400)
$define TBM_SETPOS              (0x400+5)
$define TBM_SETRANGE            (0x400+6)
$define TBM_SETTICFREQ          (0x400+20)
Title: Re: Webcam images in Ebasic
Post by: Andre on June 29, 2008, 06:07:24 PM
Thank you again pistol350.
Indeed it compiled and run fine with the changes you suggested. Now I'll spend a few weeks trying to understand what is going on ;).
Title: Re: Webcam images in Ebasic
Post by: Boris on July 22, 2008, 05:14:26 AM
Thanks Pistol350. That got it working for me too.