Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support camera select. #310

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions demos/choose-camera.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<!DOCTYPE html>

<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>WebcamJS Test Page</title>
<style type="text/css">
body {
font-family: Helvetica, sans-serif;
}
h2,
h3 {
margin-top: 0;
}
form {
margin-top: 15px;
}
form > input {
margin-right: 15px;
}
#results {
float: right;
margin: 20px;
padding: 20px;
border: 1px solid;
background: #ccc;
}
</style>
</head>
<body>
<div id="results">Your captured image will appear here...</div>

<h1>WebcamJS Test Page</h1>
<h3>Demonstrates Adobe Flash capture &amp; display</h3>

<div id="my_camera"></div>

<!-- First, include the Webcam.js JavaScript Library -->
<script type="text/javascript" src="../webcam.js"></script>

<!-- Configure a few settings and attach camera -->
<script language="JavaScript">
Webcam.set({
width: 320,
height: 240,
image_format: "jpeg",
jpeg_quality: 90,
});
Webcam.on("init", function () {
Webcam.getCameras(function (cameras) {
if (cameras.length > 0) {
Webcam.setAndInitCamera(cameras[cameras.length - 1].id);
}
});
});
Webcam.attach("#my_camera", true);
</script>

<!-- A button for taking snaps -->
<form>
<input type="button" value="Take Snapshot" onClick="take_snapshot()" />
</form>

<!-- Code to handle taking the snapshot and displaying it locally -->
<script language="JavaScript">
function take_snapshot() {
// take snapshot and get image data
Webcam.snap(function (data_uri) {
// display results in page
document.getElementById("results").innerHTML =
"<h2>Here is your image:</h2>" + '<img src="' + data_uri + '"/>';
});
}
</script>
</body>
</html>
97 changes: 64 additions & 33 deletions flash/Webcam.as
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
private var image_format:String;
private var fps:int;
private var flip_horiz:Boolean;
private var camera_idx:int;

public function Webcam() {
// class constructor
Expand Down Expand Up @@ -68,45 +69,25 @@

// Hack to auto-select iSight camera on Mac (JPEGCam Issue #5, submitted by manuel.gonzalez.noriega)
// From: http://www.squidder.com/2009/03/09/trick-auto-select-mac-isight-in-flash/
var cameraIdx:int = -1;
camera_idx = -1;
for (var idx = 0, len = Camera.names.length; idx < len; idx++) {
if (Camera.names[idx] == "USB Video Class Video") {
cameraIdx = idx;
camera_idx = idx;
idx = len;
}
}
if (cameraIdx > -1) camera = Camera.getCamera( String(cameraIdx) );
if (camera_idx > -1) camera = Camera.getCamera( String(camera_idx) );
else camera = Camera.getCamera();

if (camera != null) {
camera.addEventListener(ActivityEvent.ACTIVITY, activityHandler);
camera.addEventListener(StatusEvent.STATUS, handleCameraStatus, false, 0, true);
video = new Video( Math.max(video_width, dest_width), Math.max(video_height, dest_height) );
video.attachCamera(camera);
addChild(video);

if ((video_width < dest_width) && (video_height < dest_height)) {
video.scaleX = video_width / dest_width;
video.scaleY = video_height / dest_height;
}

if (flip_horiz) {
video.scaleX *= -1;
video.x = video.width + video.x;
}

camera.setQuality(0, 100);
camera.setKeyFrameInterval(10);
camera.setMode( Math.max(video_width, dest_width), Math.max(video_height, dest_height), fps );

// only detect motion once, to determine when camera is "live"
camera.setMotionLevel( 1 );

ExternalInterface.addCallback('_getCameras', getCameras);
ExternalInterface.addCallback('_setCamera', setCamera);
ExternalInterface.addCallback('_initCamera', initCamera);
ExternalInterface.addCallback('_snap', snap);
ExternalInterface.addCallback('_configure', configure);
ExternalInterface.addCallback('_releaseCamera', releaseCamera);

ExternalInterface.call('Webcam.flashNotify', 'flashLoadComplete', true);
ExternalInterface.call('Webcam.flashNotify', 'flashInitComplete', true);
}
else {
trace("You need a camera.");
Expand Down Expand Up @@ -141,6 +122,56 @@
}
}

public function getCameras() {
return Camera.names;
}

public function setCamera(name: String) {
for (var idx = 0, len = Camera.names.length; idx < len; idx++) {
if (Camera.names[idx] == name) {
camera_idx = idx;
return;
}
}
ExternalInterface.call('Webcam.flashNotify', "error", "camera: " + name + " is not found");
trace("camera: " + name + " is not found");
}

public function initCamera() {
releaseCamera();
if (camera_idx > -1) camera = Camera.getCamera(String(camera_idx));
else camera = Camera.getCamera();

if (camera != null) {
camera.addEventListener(ActivityEvent.ACTIVITY, activityHandler);
camera.addEventListener(StatusEvent.STATUS, handleCameraStatus, false, 0, true);
video = new Video(Math.max(video_width, dest_width), Math.max(video_height, dest_height));
video.attachCamera(camera);
addChild(video);

if ((video_width < dest_width) && (video_height < dest_height)) {
video.scaleX = video_width / dest_width;
video.scaleY = video_height / dest_height;
}

if (flip_horiz) {
video.scaleX *= -1;
video.x = video.width + video.x;
}

camera.setQuality(0, 100);
camera.setKeyFrameInterval(10);
camera.setMode(Math.max(video_width, dest_width), Math.max(video_height, dest_height), fps);

// only detect motion once, to determine when camera is "live"
camera.setMotionLevel(1);
ExternalInterface.call('Webcam.flashNotify', 'flashLoadComplete', true);
} else {
trace("You need a camera.");
ExternalInterface.call('Webcam.flashNotify', "error", "No camera was detected.");
}
}

public function snap() {
// take snapshot from camera, and upload if URL was provided
trace("in snap(), drawing to bitmap");
Expand Down Expand Up @@ -185,13 +216,13 @@
}

public function releaseCamera() {

trace("in releaseCamera(), turn off camera");
video.attachCamera(null);
video.clear();
camera = null;
removeChild(video);

if (video != null){
video.attachCamera(null);
video.clear();
camera = null;
removeChild(video);
}
}
}
}
Binary file modified flash/Webcam.fla
Binary file not shown.
Loading