Webcam and purebasic

Mac OSX specific forum
jesperbrannmark
Enthusiast
Enthusiast
Posts: 536
Joined: Mon Feb 16, 2009 10:42 am
Location: sweden
Contact:

Webcam and purebasic

Post 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?
jesperbrannmark
Enthusiast
Enthusiast
Posts: 536
Joined: Mon Feb 16, 2009 10:42 am
Location: sweden
Contact:

Re: Webcam and purebasic

Post 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))
User avatar
singo
User
User
Posts: 35
Joined: Mon Apr 23, 2007 4:50 am
Location: Nabiac NSW Australia

Re: Webcam and purebasic

Post 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
Singo
Win10, Win7, Debian x86 & OSX ~ PB 5.70 LTS
Minimbah NSW Australia
jesperbrannmark
Enthusiast
Enthusiast
Posts: 536
Joined: Mon Feb 16, 2009 10:42 am
Location: sweden
Contact:

Re: Webcam and purebasic

Post 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
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Webcam and purebasic

Post by wilbert »

You mean without any parameters; only a command to capture a frame from the default device ?
jesperbrannmark
Enthusiast
Enthusiast
Posts: 536
Joined: Mon Feb 16, 2009 10:42 am
Location: sweden
Contact:

Re: Webcam and purebasic

Post 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
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Webcam and purebasic

Post 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
Last edited by wilbert on Wed May 31, 2017 3:13 pm, edited 2 times in total.
jesperbrannmark
Enthusiast
Enthusiast
Posts: 536
Joined: Mon Feb 16, 2009 10:42 am
Location: sweden
Contact:

Re: Webcam and purebasic

Post 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
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Webcam and purebasic

Post 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.
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Webcam and purebasic

Post by WilliamL »

Works fine here! (do I really look like that? :shock: )
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Webcam and purebasic

Post 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.
jesperbrannmark
Enthusiast
Enthusiast
Posts: 536
Joined: Mon Feb 16, 2009 10:42 am
Location: sweden
Contact:

Re: Webcam and purebasic

Post 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.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Webcam and purebasic

Post 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:
jesperbrannmark
Enthusiast
Enthusiast
Posts: 536
Joined: Mon Feb 16, 2009 10:42 am
Location: sweden
Contact:

Re: Webcam and purebasic

Post 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....
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Webcam and purebasic

Post 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.
Post Reply