PageRenderTime 36ms CodeModel.GetById 21ms RepoModel.GetById 3ms app.codeStats 0ms

/js/flash/Webcam.as

https://gitlab.com/cmaring1994/Blog
ActionScript | 197 lines | 147 code | 34 blank | 16 comment | 19 complexity | 51ef7977bfe915d7c080779ebe91efa4 MD5 | raw file
  1. package {
  2. /* Webcam.js v1.0 */
  3. /* Webcam library for capturing JPEG/PNG images and sending them to JavaScript */
  4. /* Author: Joseph Huckaby <jhuckaby@effectgames.com> */
  5. /* Based on JPEGCam: http://code.google.com/p/jpegcam/ */
  6. /* Copyright (c) 2012 Joseph Huckaby */
  7. /* Licensed under the MIT License */
  8. import flash.display.LoaderInfo;
  9. import flash.display.Sprite;
  10. import flash.display.StageAlign;
  11. import flash.display.StageScaleMode;
  12. import flash.display.Bitmap;
  13. import flash.display.BitmapData;
  14. import flash.events.*;
  15. import flash.utils.*;
  16. import flash.media.Camera;
  17. import flash.media.Video;
  18. import flash.external.ExternalInterface;
  19. import flash.net.*;
  20. import flash.system.Security;
  21. import flash.system.SecurityPanel;
  22. import flash.media.Sound;
  23. import flash.media.SoundChannel;
  24. import flash.geom.Matrix;
  25. import mx.utils.Base64Encoder;
  26. import com.adobe.images.BitString;
  27. import com.adobe.images.PNGEncoder;
  28. import com.adobe.images.JPGEncoder;
  29. public class Webcam extends Sprite {
  30. private var video:Video;
  31. private var video_width:int;
  32. private var video_height:int;
  33. private var dest_width:int;
  34. private var dest_height:int;
  35. private var camera:Camera;
  36. private var bmpdata:BitmapData;
  37. private var jpeg_quality:int;
  38. private var image_format:String;
  39. private var fps:int;
  40. private var flip_horiz:Boolean;
  41. public function Webcam() {
  42. // class constructor
  43. flash.system.Security.allowDomain("*");
  44. var flashvars:Object = LoaderInfo(this.root.loaderInfo).parameters;
  45. video_width = Math.floor( flashvars.width );
  46. video_height = Math.floor( flashvars.height );
  47. dest_width = Math.floor( flashvars.dest_width );
  48. dest_height = Math.floor( flashvars.dest_height );
  49. jpeg_quality = Math.floor( flashvars.jpeg_quality );
  50. image_format = flashvars.image_format;
  51. fps = Math.floor( flashvars.fps );
  52. flip_horiz = flashvars.flip_horiz == "true";
  53. stage.scaleMode = StageScaleMode.NO_SCALE;
  54. // stage.scaleMode = StageScaleMode.EXACT_FIT; // Note: This breaks HD capture
  55. stage.align = StageAlign.TOP_LEFT;
  56. stage.stageWidth = Math.max(video_width, dest_width);
  57. stage.stageHeight = Math.max(video_height, dest_height);
  58. if (flashvars.new_user) {
  59. Security.showSettings( SecurityPanel.PRIVACY );
  60. }
  61. // Hack to auto-select iSight camera on Mac (JPEGCam Issue #5, submitted by manuel.gonzalez.noriega)
  62. // From: http://www.squidder.com/2009/03/09/trick-auto-select-mac-isight-in-flash/
  63. var cameraIdx:int = -1;
  64. for (var idx = 0, len = Camera.names.length; idx < len; idx++) {
  65. if (Camera.names[idx] == "USB Video Class Video") {
  66. cameraIdx = idx;
  67. idx = len;
  68. }
  69. }
  70. if (cameraIdx > -1) camera = Camera.getCamera( String(cameraIdx) );
  71. else camera = Camera.getCamera();
  72. if (camera != null) {
  73. camera.addEventListener(ActivityEvent.ACTIVITY, activityHandler);
  74. camera.addEventListener(StatusEvent.STATUS, handleCameraStatus, false, 0, true);
  75. video = new Video( Math.max(video_width, dest_width), Math.max(video_height, dest_height) );
  76. video.attachCamera(camera);
  77. addChild(video);
  78. if ((video_width < dest_width) && (video_height < dest_height)) {
  79. video.scaleX = video_width / dest_width;
  80. video.scaleY = video_height / dest_height;
  81. }
  82. if (flip_horiz) {
  83. video.scaleX *= -1;
  84. video.x = video.width + video.x;
  85. }
  86. camera.setQuality(0, 100);
  87. camera.setKeyFrameInterval(10);
  88. camera.setMode( Math.max(video_width, dest_width), Math.max(video_height, dest_height), fps );
  89. // only detect motion once, to determine when camera is "live"
  90. camera.setMotionLevel( 1 );
  91. ExternalInterface.addCallback('_snap', snap);
  92. ExternalInterface.addCallback('_configure', configure);
  93. ExternalInterface.addCallback('_releaseCamera', releaseCamera);
  94. ExternalInterface.call('Webcam.flashNotify', 'flashLoadComplete', true);
  95. }
  96. else {
  97. trace("You need a camera.");
  98. ExternalInterface.call('Webcam.flashNotify', "error", "No camera was detected.");
  99. }
  100. }
  101. public function configure(panel:String = SecurityPanel.CAMERA) {
  102. // show configure dialog inside flash movie
  103. Security.showSettings(panel);
  104. }
  105. private function activityHandler(event:ActivityEvent):void {
  106. trace("activityHandler: " + event);
  107. ExternalInterface.call('Webcam.flashNotify', 'cameraLive', true);
  108. // now disable motion detection (may help reduce CPU usage)
  109. camera.setMotionLevel( 100 );
  110. }
  111. private function handleCameraStatus(e:StatusEvent):void {
  112. switch (e.code) {
  113. case 'Camera.Muted': {
  114. trace("Camera not allowed");
  115. ExternalInterface.call('Webcam.flashNotify', "error", "Access to camera denied");
  116. break;
  117. }
  118. case 'Camera.Unmuted': {
  119. trace("Camera allowed");
  120. break;
  121. }
  122. }
  123. }
  124. public function snap() {
  125. // take snapshot from camera, and upload if URL was provided
  126. trace("in snap(), drawing to bitmap");
  127. // take snapshot, convert to jpeg, submit to server
  128. bmpdata = new BitmapData( Math.max(video_width, dest_width), Math.max(video_height, dest_height) );
  129. bmpdata.draw( video );
  130. if ((video_width > dest_width) && (video_height > dest_height)) {
  131. // resize image downward before submitting
  132. var tmpdata = new BitmapData(dest_width, dest_height);
  133. var matrix = new Matrix();
  134. matrix.scale( dest_width / video_width, dest_height / video_height );
  135. tmpdata.draw( bmpdata, matrix, null, null, null, true ); // smoothing
  136. bmpdata = tmpdata;
  137. } // need resize
  138. trace("converting to " + image_format);
  139. var bytes:ByteArray;
  140. if (image_format == 'png') {
  141. bytes = PNGEncoder.encode( bmpdata );
  142. }
  143. else {
  144. var encoder:JPGEncoder;
  145. encoder = new JPGEncoder( jpeg_quality );
  146. bytes = encoder.encode( bmpdata );
  147. }
  148. trace("raw image length: " + bytes.length);
  149. var be = new Base64Encoder();
  150. be.encodeBytes( bytes );
  151. var bstr = be.toString();
  152. trace("b64 string length: " + bstr.length);
  153. return bstr;
  154. }
  155. public function releaseCamera() {
  156. trace("in releaseCamera(), turn off camera");
  157. video.attachCamera(null);
  158. video.clear();
  159. camera = null;
  160. removeChild(video);
  161. }
  162. }
  163. }