Skip to content

CameraDetection

***UPDATE***
This project is now on GitHub. Please look there for current copy.
https://github.com/cataclysmicrewind/CameraDetection
************

AS Version: AS3
FP Version: 9+

CameraDetection is a class that discovers the first Camera object that works and has activity. If you need to use a Camera object in any of your projects, this class will simplify your code with a simple, easy to use, GC friendly class. With one event to listen for, the class will tell you if there are no Cameras, if the user denied permission to the Camera or a successful Camera was found.

You can specify how long the class should look at each Camera object before determining if the Camera is usable. *Camera objects generally require at least 300ms to start responding appropriately, which is why this is available.

Example
Documentation
For a Download, please go to the GitHub Repository

import ktu.media.CameraDetection;
import ktu.events.CameraDetectionEvent;
import ktu.media.CameraDetectionResult;

var video:Video = new Video();
addChild(video);

var cd:CameraDetection = new CameraDetection ();
cd.addEventListener(CameraDetectionEvent.RESOLVE, onResolve);
cd.begin();

function onResolve(e:CameraDetectionEvent):void {
switch (e.code) {
case CameraDetectionResult.SUCCESS :
video.attachCamera(e.camera);
break;
case CameraDetectionResult.NO_SUCCESS :
trace("None of the cameras in the list repsonded");
break;
case CameraDetectionResult.NO_PERMISSION :
trace("Camera access denied");
break;
case CameraDetectionResult.NO_CAMERAS :
trace("There are no cameras connected to the computer");
break;
}
}

#Note - I've implemented Grant Skinner's IDisposable interface. The interface is included in the download.

changelog:
12.17.10 - see post
07.02.10 - fixed bug, added documentation, cleaned up code
06.15.10 - added secondary check for permissions access

27 Comments

Leave a Comment
  1. Saeid / May 8 2010 @ 19.39

    I think it would be useful to elaborate a bit on why you have added Grant Skinner’s IDisposable interface and what exactly it is and does. I never heard of that IDisposable thing and only came here to use your CameraDetection class and now I’m wondering do I need to use that GrankSkinner thing too? If I don’t, why it’s in the download? If I do, what is it that it does?

    Cheers,
    Saeid

  2. Ktu / May 8 2010 @ 20.28

    IDisposable is an interface. The interface has one method in it: dispose(). Any object that implements IDisposable must have the .dispose(). It’s a very simple interface but Grant’s interface allows the developer to have a standardized way of removing objects that you have created. This way, when you are ready to stop using a variable and allow it to be garbage collected, you can call a method that will accomplish that. Grants Memory Management Talk [go to page 30]

    I included the interface in the CameraDetection class because the CameraDetection class uses a Video object, a Camera object, and a Timer object. These objects can sometimes be tricky to remove properly and I wanted a way to make sure that these objects could be removed from memory in a way that any developer could make that happen. If for some reason an event occurs that would make you stop trying to find the right camera amidst progress, you could always call the .dispose() and stop the process. I believe that all objects could benefit from a destroy/destruct/dispose function. Because we rely on GC to do its job, we need to make sure we do our part to let GC work with us.

  3. Saeid / May 8 2010 @ 20.40

    Thanks for the info and for sharing your CameraDetection class. It saved me from writing one and now it’s integrated into my Webcam Record app at:

    http://www.mohadjers.com/clients/mm/startersite/sample_webcam.php

    Have to test it on several machines with different number of camera first, but it seems it’s doing the job perfectly so far.

    Thanks a lot.

  4. Ktu / May 9 2010 @ 23.45

    Thanks for using CameraDetection and your comments, and thanks for sharing.
    Please let me know if you find any bugs.
    Best

  5. Saeid / May 10 2010 @ 19.42

    No bugs so far, just a compiler warning!

    Warning: parameter ‘cancelable’ has no type declaration.

    public function CameraEvent (type:String = null, camera:Camera = null, code:String = null, bubbles = false, cancelable = false ):void {

  6. Ben / Jun 15 2010 @ 00.33

    This seems to be exactly the thing I need, but I’m having a hell of a time getting it to do what I expect. I imported the classes, and basically used the exact example function above in my code. I can step through and see that it calls getDefaultCamera, but at no point is the onCamStatus event being dispatched. The permission dialog pops up, and I click the accept (or deny) button, but onResolve is never called.
    Everything I see suggests this is just the right library for my problem, so I assume I’m just making some obvious mistake, but I can’t for the life of me figure out it could be.

  7. Ktu / Jun 15 2010 @ 05.55

    Ben,
    it doesn’t surprise me that someone has ran into an issue with permissions. I’ve heard that the settings dialog (permissions request) can sometimes be flaky. I can’t say that this is the exact problem that you are having, but I’m sure we can figure it out.

    Instead of trying to solve this through comments, lets do this through email. I will send an email to the address you provided sometime today. Then when we find a solution I’ll post it up.
    Ktu

  8. Ktu / Jun 16 2010 @ 00.03

    Ben,
    I have updated my class. I have done almost the same thing that you did.
    I forced the Security dialog, and added a timer to listen to the camera.muted property. I also updated some logic and cleaned up the code a lot.
    Definitely a good addition. Thanks Ben.
    Ktu

  9. Manu / Jun 22 2010 @ 07.03

    Hey,

    I ran into a different problem, things seems to work fine for me except when I select the “Remember” checkbox in the setting popup. It throws an error at line number 152, which I guess you added for Mr. Ben’s solution. Definitely great code, but, this is something a user might do. Could you please come up with a solution for me too? :)

    Thanks in advance.

  10. Ktu / Jul 2 2010 @ 10.31

    I apologize for the problems and also the delay in response. I have fixed the issue, thank you for finding out that it was a weak event listener. All events are strongly typed so make sure that if you need to stop the process before the resolve event is dispatched that you call dispose(). Otherwise, all known bugs are fixed. Thanks Manu!

  11. Nick / Jul 3 2010 @ 00.06

    Hey Ktu,

    Thanks for a great library! It’s working pretty well for me, however, I’ve run into a couple issues. It seems that the NO_PERMISSION and NO_CAMERAS events aren’t ever being dispatched. I assumed that NO_PERMISSION would fire if the user clicks “Deny” and then closes the Settings window, but that’s not happening. Similarly, I assumed NO_CAMERAS would fire if another application was using the webcam, but that’s not happening either.

    Any idea what’s up?

    Thanks,
    -Nick

  12. hulien / Sep 2 2010 @ 09.37

    Hi, I am using your Class with FB4.
    And I always get the same error.

    1119: Access of possibly undefined property CAMERA_RESOLVED through a reference with static type Class.

    But works find using Flash cs4.

    Are you aware of this error?

  13. thegroove / Sep 15 2010 @ 04.06

    Hi there,

    Permissions check isn’t working correctly when trying the simple_example within the downloaded package.

    However, when trying the example.swf on this site, the permission check does work fine.

    There seems to be something missing in the setup within the “download” zip.

    Any chance you might look into this and update the zip?

    Great work!
    thegroove

  14. karyjuice / Nov 29 2010 @ 16.48

    Thank you !!!

    You just saved me from wasting one day writing the same thing you wrote !

    Works perfectly !

  15. karyjuice / Nov 29 2010 @ 16.53

    One thing I had to modify though in CameraDetection.as

    Line 210:

    Had to change “Security.showSettings(SecurityPanel.PRIVACY);” to “_video.attachCamera(_camera)”

    Reason: I wanted to have the Allow/Deny popup instead of the settings popup (2 clicks instead of one!)

  16. Brian Krausz / Nov 30 2010 @ 15.16

    Great tool, thank you very much! One question/bug though:

    In constructTimer (line 350) you add the timer event listeners with weak references. Is there a reason for this? When I call begin(); from within a function, and I’ve already set/remembered camera permission, the detection fails silently. This is because when my function returns the CameraDetection object has no active references and gets garbage collected immediately. Seems silly to require that I maintain a global reference to the object just so it doesn’t get GCed.

  17. kalle / Dec 8 2010 @ 05.11

    hey,
    i experiance an issue with the event listener:

    if i load the flash in an browser and allow the camera, it works fine. if i also check the “remember/save decision” in the permission dialog. and reload the page, the event listnener did not get fired anymore….

    otherwise works great :)

  18. Ktu; / Dec 9 2010 @ 10.32

    Thanks for the kind words.
    I have heard of this problem. Not a lot of people have encountered it, but I have been unable to find a suitable workaround.

    The panels that popup that are related to the settings dialog are terrible to interact with. You pretty much can’t. If anyone finds a method of determining if the menu is displayed or not that would be a help to me. Hopefully when my current project at work is over I’ll be able to revisit this.

  19. Vadim / Dec 13 2010 @ 05.47

    Thanks for great library!
    But I have the same problem as Kalle have: when I’ve trying to get camera second time – the event listener for timer doesn’t dispatched anymore (method tick() doesn’t fired).
    I would be very grateful for the help.

  20. Ktu; / Dec 16 2010 @ 21.58

    I’ve been doing some testing. When I choose remember with allow set. The class appears to work just fine. It only bails when the user selects Deny and remember.

    Could one of you explain in more detail the scenario that it is failing?

  21. Vadim / Dec 17 2010 @ 03.03

    Ok, my scenario:
    1. Run any example (that uses your code) first time, click only on “Allow” in security popup and close it. Video works OK.
    2. Don’t close or refresh page, only close video popup. Run example second time, security popup not appears anymore, and nothing happened.

  22. Vincenzo / Jan 11 2011 @ 13.28

    The program shows errors when there are no cameras installed.
    You should add an error check in the default camera getter function.

    --- CameraDetection.as	2010-12-17 23:16:02.000000000 -0800
    +++ CameraDetection.as.new	2011-01-11 10:11:27.097285069 -0800
    @@ -180,6 +180,10 @@
     			if (Capabilities.os.substr (0, 6) == MAC_OS)
     				_isMac = true;
     			getDefaultCamera ();
    +			if (!_camera) {
    +			    return;
    +			}
    +
     			if (_camera.muted == false) {
     				havePermissions ();
     			} else {
    @@ -220,8 +224,10 @@
     		 */
     		private function getDefaultCamera ():void {
     			if (_isMac) _camera = getMacDefaultCamera ();
    -			else 		_camera = Camera.getCamera ();
    -			_defaultCameraName = _camera.name;
    +			else 		_camera = Camera.getCamera ();
    +			if (_camera) {
    +			    _defaultCameraName = _camera.name;
    +			}
     		}
     		/** @private
     		 *  gets the defualt camera for mac laptops
    
  23. Phong Linh / Apr 5 2011 @ 02.35

    I’ve played with the example and have a Blue of Deatch crash. I can not say exactly what cause that crash because after I’ve pressed Allow in ‘Permisson Panel’, my computer restarted immediately. My computer also crash when I click the button ‘Turn On/Off’ in the example rapidly.

  24. Ktu; / Apr 5 2011 @ 18.06

    my gut reaction is that this is not related to my code.
    It sounds like a driver problem to me. I tested clicking the button rapidly, and although it was a bit sluggish, it performed as expected.

    You could try updating drivers for your camera, and also consider that a recent change in your operating system might have caused the problem. You could also try other applications that don’t use my code, but use the camera and see what happens to your system then.

    Unless others can reproduce I won’t look into this further. Sorry you are having this issue.

  25. Phong Linh / Apr 5 2011 @ 22.19

    Thank you for your answer. I will test further and update the driver. If find something I’ll info you. I think there’s something wrong with default driver of Windows 7. My webcam is a Philips SPC500NC. I also test in my Dell laptop and nothing special happen.

  26. J / Jul 10 2011 @ 17.44

    It gave me blue screen of death as well. I did not even click the button rapidly. I turned it off and then on once and it worked perfectly. The second time it froze up Flash then a second later gave me blue screen of death.

Trackbacks

  1. Major CameraDetection Update « Cataclysmic Rewind Projects

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.