diff --git a/jquery.webcam.js b/jquery.webcam.js index ed0c205..535a501 100644 --- a/jquery.webcam.js +++ b/jquery.webcam.js @@ -1,93 +1,84 @@ /** -* jQuery webcam -* Copyright (c) 2010, Robert Eisele (robert@xarg.org) -* Dual licensed under the MIT or GPL Version 2 licenses. -* Date: 09/12/2010 -* -* @author Robert Eisele -* @version 1.0 -* -* @see http://www.xarg.org/project/jquery-webcam-plugin/ -**/ + * jQuery webcam + * Copyright (c) 2010, Robert Eisele (robert@xarg.org) + * Dual licensed under the MIT or GPL Version 2 licenses. + * Date: 09/12/2010 + * + * @author Robert Eisele + * @version 1.0 + * + * @see http://www.xarg.org/project/jquery-webcam-plugin/ + **/ (function ($) { - var webcam = { - - extern: null, // external select token to support jQuery dialogs - append: true, // append object instead of overwriting - - width: 320, - height: 240, - - mode: "callback", // callback | save | stream - - swffile: "jscam.swf", - quality: 85, - - debug: function () {}, - onCapture: function () {}, - onTick: function () {}, - onSave: function () {}, - onLoad: function () {} - }; - - window.webcam = webcam; - - $.fn.webcam = function(options) { - - if (typeof options === "object") { - for (var ndx in webcam) { - if (options[ndx] !== undefined) { - webcam[ndx] = options[ndx]; - } - } - } - - var source = ''; - - if (null !== webcam.extern) { - $(webcam.extern)[webcam.append ? "append" : "html"](source); - } else { - this[webcam.append ? "append" : "html"](source); - } - - (_register = function(run) { - - var cam = document.getElementById('XwebcamXobjectX'); - - if (cam.capture !== undefined) { - - /* Simple callback methods are not allowed :-/ */ - webcam.capture = function(x) { - try { - return cam.capture(x); - } catch(e) {} - } - webcam.save = function(x) { - try { - return cam.save(x); - } catch(e) {} - } - webcam.setCamera = function(x) { - try { - return cam.setCamera(x); - } catch(e) {} - } - webcam.getCameraList = function() { - try { - return cam.getCameraList(); - } catch(e) {} - } - - webcam.onLoad(); - } else if (0 == run) { - webcam.debug("error", "Flash movie not yet registered!"); - } else { - /* Flash interface not ready yet */ - window.setTimeout(_register, 1000 * (4 - run), run - 1); - } - })(3); + var webcam = { + extern: null, // external select token to support jQuery dialogs + append: true, // append object instead of overwriting + + width: 320, + height: 240, + + swffile: "jscam.swf", + quality: 85, + + debug: function () {}, + onCapture: function () {}, + onLoad: function () {} + }; + + window.webcam = webcam; + + $.fn.webcam = function(options) { + if (typeof options === "object") { + for (var ndx in webcam) { + if (options[ndx] !== undefined) { + webcam[ndx] = options[ndx]; + } + } } + var source = ''; + + if (null !== webcam.extern) { + $(webcam.extern)[webcam.append ? "append" : "html"](source); + } else { + this[webcam.append ? "append" : "html"](source); + } + + (_register = function(run) { + + var cam = document.getElementById('XwebcamXobjectX'); + + if (cam.capture !== undefined) { + + /* Simple callback methods are not allowed :-/ */ + webcam.capture = function(x) { + try { + return cam.capture(x); + } catch(e) {} + } + + webcam.setCamera = function(x) { + try { + return cam.setCamera(x); + } catch(e) {} + } + + webcam.getCameraList = function() { + try { + return cam.getCameraList(); + } catch(e) {} + } + + webcam.onLoad(); + } else if (run == 0) { + webcam.debug("error", "Flash movie not yet registered!"); + } else { + /* Flash interface not ready yet */ + window.setTimeout(_register, 1000 * (4 - run), run - 1); + } + })(3); + } + })(jQuery); diff --git a/jscam.swf b/jscam.swf index 7cbd2ef..ef144ad 100644 Binary files a/jscam.swf and b/jscam.swf differ diff --git a/src/jscam.as b/src/jscam.as index aa70172..ab03f37 100644 --- a/src/jscam.as +++ b/src/jscam.as @@ -1,243 +1,116 @@ /** -* jQuery webcam -* Copyright (c) 2010, Robert Eisele (robert@xarg.org) -* Dual licensed under the MIT or GPL Version 2 licenses. -* Date: 09/12/2010 -* -* @author Robert Eisele -* @version 1.0 -* -* @see http://www.xarg.org/project/jquery-webcam-plugin/ -**/ + * jQuery webcam + * Copyright (c) 2010, Robert Eisele (robert@xarg.org) + * Dual licensed under the MIT or GPL Version 2 licenses. + * Date: 09/12/2010 + * + * @author Robert Eisele + * @version 1.0 + * + * @see http://www.xarg.org/project/jquery-webcam-plugin/ + **/ import flash.system.Security; import flash.external.ExternalInterface; import flash.display.BitmapData; -import JPGEncoder; class JSCam { - private static var camera:Camera = null; - private static var buffer:BitmapData = null; - private static var quality:Number = 85; - private static var interval = null; - private static var stream = null; - private static var mode = "callback"; - - public static function main():Void { - - System.security.allowDomain("*"); - - if (_root.mode) { - mode = _root.mode; - } else { - ExternalInterface.call('webcam.debug', "error", "No camera mode present, falling back..."); - } - - if (_root.quality) { - quality = _root.quality; - } - - // From: http://www.squidder.com/2009/03/09/trick-auto-select-mac-isight-in-flash/ - var id:Number = -1; - for (var i = 0, l = Camera.names.length; i < l; i++) { - if (Camera.names[i] == "USB Video Class Video") { - id = i; - break; - } - } - if (id > -1) { - camera = Camera.get(id); - } else { - camera = Camera.get(); - } - - if (null != camera) { - - // http://www.adobe.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary133.html - camera.onStatus = function(info:Object) { - - switch (info.code) { - case 'Camera.Muted': - ExternalInterface.call('webcam.debug', "notify", "Camera stopped"); - break; - case 'Camera.Unmuted' : - ExternalInterface.call('webcam.debug', "notify", "Camera started"); - break; - } - } + private static var camera:Camera = null; + private static var display:MovieClip = null; + private static var buffer:BitmapData = null; + private static var quality:Number = 85; - camera.setQuality(0, 100); - camera.setMode(Stage.width, Stage.height, 24, false); + public static function main():Void { - ExternalInterface.addCallback("capture", null, capture); + System.security.allowDomain("*"); - ExternalInterface.addCallback("save", null, save); + if (_root.quality) + quality = _root.quality; - ExternalInterface.addCallback("setCamera", null, setCamera); - ExternalInterface.addCallback("getCameraList", null, getCameraList); - _root.attachMovie("clip", "video", 1); - _root.video.attachVideo(camera); - _root.video._x = 0; - _root.video._y = 0; + // From: http://www.squidder.com/2009/03/09/trick-auto-select-mac-isight-in-flash/ + var cameraId:Number = -1; + for (var i = 0, l = Camera.names.length; i < l; i++) + if (Camera.names[i] == "USB Video Class Video") { + cameraId = i; + break; + } - } else { - ExternalInterface.call('webcam.debug', "error", "No camera was detected."); - } - } + if (cameraId > -1) + camera = Camera.get(cameraId); + else + camera = Camera.get(); - public static function capture(time:Number):Boolean { + if (camera) { + // http://www.adobe.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary133.html + camera.onStatus = function(info:Object) { + switch (info.code) { + case 'Camera.Muted': + ExternalInterface.call('webcam.debug', "notify", "Camera stopped"); + break; + case 'Camera.Unmuted' : + ExternalInterface.call('webcam.debug', "notify", "Camera started"); + break; + } + } - if (null != camera) { + camera.setQuality(0, 100); + camera.setMode(Stage.width, Stage.height, 24, false); - if (null != buffer) { - return false; - } + ExternalInterface.addCallback("capture", null, capture); + ExternalInterface.addCallback("setCamera", null, setCamera); + ExternalInterface.addCallback("getCameraList", null, getCameraList); - buffer = new BitmapData(Stage.width, Stage.height); - ExternalInterface.call('webcam.debug', "notify", "Capturing started."); + display = _root.attachMovie("clip", "video", 1); + display.video.attachVideo(camera); + display.video._x = 0; + display.video._y = 0; - if ("stream" == mode) { - _stream(); - return true; - } + } else { + ExternalInterface.call('webcam.debug', "error", "No camera was detected."); + } + } - if (!time) { - time = -1; - } else if (time > 10) { - time = 10; - } - _capture(time + 1); + public static function getCameraList():Array { + var list = new Array(); - return true; - } - return false; - } - - private static function _capture(time:Number):Void { - - if (null != interval) { - clearInterval(interval); - } + for (var i=0, l = Camera.names.length; i < l; i++) + list[i] = Camera.names[i]; - if (0 == time) { - buffer.draw(_root.video); - ExternalInterface.call('webcam.onCapture'); - ExternalInterface.call('webcam.debug', "notify", "Capturing finished."); - } else { - ExternalInterface.call('webcam.onTick', time - 1); - interval = setInterval(_capture, 1000, time - 1); - } - } + return list; + } - public static function getCameraList():Array { - var list = new Array(); + public static function setCamera(cameraId:Number):Boolean { + if (0 <= cameraId && cameraId < Camera.names.length) { + camera = Camera.get(cameraId); + camera.setQuality(0, 100); + camera.setMode(Stage.width, Stage.height, 24, false); + return true; + } + return false; + } - for (var i = 0, l = Camera.names.length; i < l; i++) { - list[i] = Camera.names[i]; - } - return list; - } - public static function setCamera(id:Number):Boolean { + public static function capture():Boolean { + if (!camera) + return false; - if (0 <= id && id < Camera.names.length) { - camera = Camera.get(id); - camera.setQuality(0, 100); - camera.setMode(Stage.width, Stage.height, 24, false); + ExternalInterface.call('webcam.debug', "notify", "Capturing started."); - _root.removeMovieClip(); - _root.attachMovie("clip", "video", 1); - _root.video.attachVideo(camera); - _root.video._x = 0; - _root.video._y = 0; + buffer = new BitmapData(Stage.width, Stage.height); + buffer.draw(display.video); - return true; + var pixels = []; + for (var i=0; i < 240; ++i) + for (var j=0; j < 320; ++j) + pixels.push(buffer.getPixel(j, i).valueOf()) - } - return false; - } + ExternalInterface.call("webcam.onCapture", pixels); + ExternalInterface.call('webcam.debug', "notify", "Capturing finished."); - public static function save(file:String):Boolean { - - if ("stream" == mode) { - - return true; - - } else if (null != buffer) { - - if ("callback" == mode) { - - for (var i = 0; i < 240; ++i) { - - var row = ""; - for (var j=0; j < 320; ++j) { - row+= buffer.getPixel(j, i); - row+= ";"; - } - ExternalInterface.call("webcam.onSave", row); - } - - } else if ("save" == mode) { - - if (file) { - - var e = new JPGEncoder(quality); - - var sal = {}; - sal.sendAndLoad = XML.prototype.sendAndLoad; - sal.contentType = "image/jpeg"; - sal.toString = function() { - return e.encode(JSCam.buffer); - } - - var doc = new XML(); - doc.onLoad = function(success) { - ExternalInterface.call("webcam.onSave", "done", this.toString()); - } - - sal.sendAndLoad(file, doc); -/* - ExternalInterface.call('webcam.debug', "error", "No save mode compiled in."); - return false; -*/ - } else { - ExternalInterface.call('webcam.debug', "error", "No file name specified."); - return false; - } - - } else { - ExternalInterface.call('webcam.debug', "error", "Unsupported storage mode."); - } - - buffer = null; - return true; - } - return false; - } - - private static function _stream():Void { - - buffer.draw(_root.video); - - if (null != stream) { - clearInterval(stream); - } - - - for (var i = 0; i < 240; ++i) { - - var row = ""; - for (var j=0; j < 320; ++j) { - row+= buffer.getPixel(j, i); - row+= ";"; - } - ExternalInterface.call("webcam.onSave", row); - } - - stream = setInterval(_stream, 10); - } + return true; + } } diff --git a/src/jscam.xml b/src/jscam.xml index a0734a0..b76dc45 100644 --- a/src/jscam.xml +++ b/src/jscam.xml @@ -1,11 +1,12 @@ - - - - + - + + + -