ElggWebcamPlugin.hx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import flash.events.MouseEvent;
  2. import flash.Lib;
  3. import flash.display.Bitmap;
  4. import flash.display.BitmapData;
  5. import flash.display.MovieClip;
  6. import flash.media.Video;
  7. import flash.media.Camera;
  8. import flash.events.StatusEvent;
  9. import flash.external.ExternalInterface;
  10. import flash.media.Sound;
  11. import flash.net.URLRequest;
  12. import flash.geom.Rectangle;
  13. import flash.utils.ByteArray;
  14. import haxe.crypto.Base64;
  15. import flash.text.TextField;
  16. import flash.text.TextFieldAutoSize;
  17. import feffects.Tween;
  18. class ElggWebcamPlugin {
  19. public static function main() {
  20. var cam = new Webcam();
  21. }
  22. }
  23. class Webcam {
  24. var mc:MovieClip;
  25. var vid:Video;
  26. var cam:Camera = Camera.getCamera();
  27. var vidContainer:MovieClip;
  28. var hasSnap:Bool = false;
  29. var videoWidth = Lib.current.stage.stageWidth;
  30. var videoHeight = Lib.current.stage.stageHeight;
  31. var siteURL = ExternalInterface.call('elgg.get_site_url');
  32. var shutterSound:Sound;
  33. public function new() {
  34. if (cam != null) {
  35. cam.addEventListener(StatusEvent.STATUS, camInit);
  36. cam.setMode(320, 240, 30);
  37. cam.setQuality(0, 100);
  38. mc = Lib.current;
  39. vid = new Video(cam.width, cam.height);
  40. vid.attachCamera(cam);
  41. // have to have a container to attach a click event.
  42. vidContainer = new MovieClip();
  43. vidContainer.addChild(vid);
  44. vidContainer.width = videoWidth;
  45. vidContainer.height = videoHeight;
  46. mc.addChild(vidContainer);
  47. shutterSound = new Sound(new URLRequest(siteURL + "/mod/webcam/haxe/shutter.mp3"));
  48. // pause / unpause on click
  49. vidContainer.addEventListener(MouseEvent.CLICK, function(e:MouseEvent) {
  50. if (!hasSnap) {
  51. shutterSound.play();
  52. var bitmapData: BitmapData = new BitmapData(cam.width, cam.height);
  53. var bitmap: Bitmap = new Bitmap(bitmapData);
  54. // place over playing video
  55. bitmap.x = 0;
  56. bitmap.y = 0;
  57. bitmap.width = videoWidth;
  58. bitmap.height = videoHeight;
  59. bitmap.name = "snap";
  60. mc.addChild(bitmap);
  61. bitmapData.draw(vid);
  62. hasSnap = true;
  63. var byteArray:ByteArray = new ByteArray();
  64. byteArray = bitmapData.encode(new Rectangle(0, 0, cam.width, cam.height), new flash.display.JPEGEncoderOptions());
  65. var base64 = Base64.encode(haxe.io.Bytes.ofData(byteArray));
  66. ExternalInterface.call('elgg.avatar.saveBase64Input', base64, '.elgg-form-avatar-upload');
  67. ExternalInterface.call('elgg.avatar.saveBase64Input', base64, '.elgg-form-register');
  68. } else {
  69. mc.removeChild(mc.getChildByName("snap"));
  70. ExternalInterface.call('elgg.avatar.removeBase64Input');
  71. hasSnap = false;
  72. }
  73. });
  74. } else {
  75. var text = new TextField();
  76. text.text = "No cameras found.";
  77. text.autoSize = TextFieldAutoSize.LEFT;
  78. mc = Lib.current;
  79. mc.addChild(text);
  80. }
  81. }
  82. public function camInit(event:StatusEvent) {
  83. if (event.code == "Camera.Muted") {
  84. var text = new TextField();
  85. text.text = "Permission to access camera was denied.";
  86. text.autoSize = TextFieldAutoSize.LEFT;
  87. mc = Lib.current;
  88. mc.addChild(text);
  89. } else {
  90. // @todo this doens't work.
  91. // cam.setMode(320, 240, 30);
  92. // cam.setQuality(0, 100);
  93. }
  94. cam.removeEventListener(StatusEvent.STATUS, camInit);
  95. }
  96. }