Page 1 of 2

Webcam and purebasic

Posted: Mon Apr 25, 2011 11:11 am
by jesperbrannmark
Hi all.
I've been doing a lot of stuff with webcams on Windows. There are three techniques AVICAP, Directshow and WMI and there is amost always problems with it somewhere. I am porting my program to Mac OS and I am trying to get the webcam to work.
What I basicly want to do is to capture one... or two... still images, preferably bigger than 640x480, and save them in the tempfolder.
I found something called Macam, but thats only external cameras and I have a hard time working with it.
I've inlined a webgadget and a flash component that grabs images but it takes up a lot of CPU time and only allows 320x240.
I've also looked at
http://developer.apple.com/library/mac/ ... rence.html
and
http://developer.apple.com/library/mac/ ... ler_m.html

Which are both Cocoa, while Purebasic rely on Carbon library.
I have no idea how to go from here. Anyone can help me get started?

Re: Webcam and purebasic

Posted: Sat May 07, 2011 7:57 pm
by jesperbrannmark
using wilberts applescript wrapper here is a solution... need camspinner (google for it)
this can capture images or moving images from webcam (both internal and external). please already have camspinner running or otherwise you will get an error and have to run the script twice:

Code: Select all

   a.s+"tell application "+Chr(34)+"CamSpinner"+Chr(34)+Chr(13)+Chr(10)
    a.s+"set theFile To grab"+Chr(13)+Chr(10)
    a.s+"return theFile as text"+Chr(13)+Chr(10)
    a.s+"End tell"+Chr(13)+Chr(10)
    MessageRequester("PHOTO","Taking photo now")
    fil.s=COCOA_AppleScript(a.s)
    fil.s=ReplaceString(fil.s,":","/")
    fil.s="/"+Right(fil.s,Len(fil.s)-FindString(fil.s,"/"))
    debug str(FileSize(fil.s))

Re: Webcam and purebasic

Posted: Thu Aug 18, 2011 12:13 pm
by singo
This is the solution I found, hope it helps someone ...

Code: Select all

Procedure takeImage()
  ;ImageSnap is a Public Domain command-line tool that lets you capture still images from an iSight or other video source
  ; http://http://iharder.sourceforge.net/current/macosx/imagesnap/
  ;isightcapture is a similar but now unsupported utitlity
  DeleteFile("snapshot.jpg")
  ;RunProgram("/Applications/isightcapture","snapshot.jpg", "", #PB_Program_Wait | #PB_Program_Hide)
  RunProgram("/Applications/imagesnap","", "", #PB_Program_Wait | #PB_Program_Hide)
EndProcedure  
  
UseJPEGImageDecoder()
OpenWindow(#PB_Any,0,0,640,500,"iSightCapture",#PB_Window_ScreenCentered|#PB_Window_MinimizeGadget)
ImageGadget(0,0,0,640,480,0)
ButtonGadget(1,280,480,80,18,"Snap")
Repeat
  event = WaitWindowEvent()
  If Event=#PB_Event_Gadget 
    If EventGadget()=1 
      takeImage()
      If LoadImage(0,"snapshot.jpg")
        SetGadgetState(0,ImageID(0))
      EndIf
    EndIf
  EndIf
Until event = #PB_Event_CloseWindow

Re: Webcam and purebasic

Posted: Fri Aug 19, 2011 2:03 pm
by jesperbrannmark
I like a lot. Much better than Camstreamer (mainly because of the license)
The program used "ImageSnap" is available at http://iharder.sourceforge.net/current/ ... imagesnap/
and includes xcode project.
The file seem to just be saved in the used directory, maybe would be good to save in tempdir?

Is there anyone out there that *might* know how to convert it to a PB-lib?

Jesper

Re: Webcam and purebasic

Posted: Fri Aug 19, 2011 2:46 pm
by wilbert
You mean without any parameters; only a command to capture a frame from the default device ?

Re: Webcam and purebasic

Posted: Fri Aug 19, 2011 3:08 pm
by jesperbrannmark
For me an ideal purebasic library would have the following functions:
webcam_captureimage(filename.s)
webcam_choosecamera() (or setupcamera or something like that)
webcam_preview_camera(imagegadget id.l)

:wink:

J

Re: Webcam and purebasic

Posted: Sat Aug 20, 2011 6:48 am
by wilbert
I prefer not to create a separate library for it but to add it to my PBMX lib.
Please check if it works. It's a very basic two commands implementation

Lib location (OS X x86 10.5+) :
http://www.w73.nl/pb/libPBMX.zip

Snapshot related commands :
Image = MXSnapshot_TakeSnapshot([width])
MXSnapshot_StopSession()

It takes a snapshot from the default device.
A live preview would be complicated because of PureBasic not supporting Cocoa.
The returned object is a CGImage which is the same as an ImageID so you can use it for an image gadget or draw it onto a PureBasic image using DrawImage.

If someone with OS X x 86 10.5 has tested it, please let me know if it works since I only can test on OS X 10.7 .

Example code :

Code: Select all

Procedure UpdateSnapshot()
  Image = MXSnapshot_TakeSnapshot(320)
  SetGadgetState(0, Image)
  MX_Release(Image)
EndProcedure

If OpenWindow(0, 0, 0, 320, 256, "Snapshot example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ImageGadget(0, 0, 0, 320, 256, 0)
  UpdateSnapShot()
  AddWindowTimer(0, 0, 500)
  Repeat
    Event = WaitWindowEvent()
    If Event = #PB_Event_Timer
      UpdateSnapshot()
    EndIf
  Until Event = #PB_Event_CloseWindow
  MXSnapshot_StopSession()
EndIf

Re: Webcam and purebasic

Posted: Sat Aug 20, 2011 8:45 am
by jesperbrannmark
Wow. I knew if :D
Thats perfect. Works perfect with OS 10.6.8.

Thanks a lot, then we dont need seperate applications and/or applescript to do this..

Jesper

Re: Webcam and purebasic

Posted: Sat Aug 20, 2011 8:53 am
by wilbert
There seems to be a memory leak with the example I added.
When SetGadgetState(0, Image) is commented out, it's very minor but when it is like it is, every time a new image is set for the image gadget, memory is leaked.
I don't know if this is also the case for normal images.

Re: Webcam and purebasic

Posted: Sat Aug 20, 2011 5:21 pm
by WilliamL
Works fine here! (do I really look like that? :shock: )

Re: Webcam and purebasic

Posted: Sat Aug 20, 2011 5:44 pm
by wilbert
Thanks for letting me know William.
I made a little change and uploaded an updated version. I believe the memory leak that I noticed should be gone now.

Re: Webcam and purebasic

Posted: Sat Aug 20, 2011 6:46 pm
by jesperbrannmark
Yes I also had the memory leak on the first version. Strange enough most leakage when i moved the window (or maybe thats because thats when activity monitor updates the program info in the list).
I didn't notice that at first, good thing you saw it. The new lib works very well. I am just downloading lion on my mobile internet (and have been doing all day....) so soon I will be roaring it up and see how it behaves.
The Macbook air camera is max 640x480, but I guess thats all the same for all macs...
The library seem to run fine in threadsafe mode as well, which is great.

This library you just made PBMX lib.... you said you prefered not to make a seperate libary for it.... i think you mean that this library has more functions?

Thanks a lot, I have really been missing these functions and I hope it can be used by a lot of people.
I will probably submit my app to appstore soon... Will let you all know more soon.

Re: Webcam and purebasic

Posted: Sat Aug 20, 2011 7:07 pm
by wilbert
jesperbrannmark wrote:This library you just made PBMX lib.... you said you prefered not to make a seperate libary for it.... i think you mean that this library has more functions?
See this thread http://www.purebasic.fr/english/viewtop ... 19&t=46937 for the full overview. :wink:

Re: Webcam and purebasic

Posted: Tue Sep 06, 2011 8:49 am
by jesperbrannmark
grrr.... It feels like i should know this....
I am trying to get the fantasic webcam sample to actually SAVE the picture

Code: Select all

UseJPEG2000ImageEncoder()
Procedure UpdateSnapshot()
  Image = MXSnapshot_TakeSnapshot(320)
  SaveImage(image,GetTemporaryDirectory()+"test.jpg",#PB_ImagePlugin_JPEG,6)
  SetGadgetState(0, Image)
  MX_Release(Image)
EndProcedure

If OpenWindow(0, 0, 0, 320, 256, "Snapshot example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ImageGadget(0, 0, 0, 320, 256, 0)
  UpdateSnapShot()
  AddWindowTimer(0, 0, 500)
  Repeat
    Event = WaitWindowEvent()
    If Event = #PB_Event_Timer
      UpdateSnapshot()
    EndIf
  Until Event = #PB_Event_CloseWindow
  MXSnapshot_StopSession()
EndIf
But it seems I cant use copyimage or saveimage....

Re: Webcam and purebasic

Posted: Tue Sep 06, 2011 9:06 am
by wilbert
The easiest way, without all kind of API stuff, is to use DrawImage.
DrawImage(ImageID, x, y [, Width, Height])
ImageID is what the snapshot function outputs.
You can create a PB image, draw the snapshot onto it and save it.