PageRenderTime 93ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/upload/windid/res/swf/avatar/face/Crop.as

https://gitlab.com/wuhang2003/phpwind
ActionScript | 474 lines | 339 code | 47 blank | 88 comment | 22 complexity | 0c3c9cee21db7c927c26a58cbf54e0d8 MD5 | raw file
  1. package
  2. {
  3. import com.adobe.images.JPGEncoder;
  4. import com.adobe.utils.IntUtil;
  5. import com.phpwind.avatar.EditWindow;
  6. import fl.controls.Slider;
  7. import flash.display.Bitmap;
  8. import flash.display.BitmapData;
  9. import flash.display.Loader;
  10. import flash.display.MovieClip;
  11. import flash.display.Sprite;
  12. import flash.events.DataEvent;
  13. import flash.events.Event;
  14. import flash.events.IOErrorEvent;
  15. import flash.events.MouseEvent;
  16. import flash.geom.Rectangle;
  17. import flash.net.FileFilter;
  18. import flash.net.FileReference;
  19. import flash.events.ErrorEvent;
  20. import flash.events.Event;
  21. import flash.events.IOErrorEvent;
  22. import flash.net.URLLoader;
  23. import flash.net.URLLoaderDataFormat;
  24. import flash.net.URLRequest;
  25. import flash.net.URLRequestHeader;
  26. import flash.net.URLRequestMethod;
  27. import flash.net.URLVariables;
  28. import flash.net.navigateToURL;
  29. import flash.utils.ByteArray;
  30. import flash.ui.ContextMenu;
  31. import flash.ui.ContextMenuItem;
  32. import org.bytearray.gif.player.GIFPlayer;
  33. import fl.motion.MotionEvent;
  34. import flash.media.Camera;
  35. import flash.media.Video;
  36. import flash.sampler.StackFrame;
  37. import flash.external.ExternalInterface;
  38. import com.phpwind.util.UploadPostHelper;
  39. public class Crop extends MovieClip
  40. {
  41. public static const RETURN_ERROR:String = '图片上传出现错误,请重新选择';
  42. public static const IO_ERROR:String = '图片上传出现IO错误,请检查网路';
  43. public static const UPLOAD_WAIT:String = '图片上传中,请等待...';
  44. private var isGIF:Boolean;
  45. private var loader:Loader;
  46. private var editwindow:EditWindow;
  47. private var imageCanvas:Rectangle;
  48. private var bigPreview:Bitmap;
  49. private var smallPreview:Bitmap;
  50. private var normalPreview:Bitmap;
  51. private var file:FileReference;
  52. private var maxSize:uint;
  53. private var video:Video;
  54. private var camera:Camera;
  55. public function Crop()
  56. {
  57. var contextMenu_:ContextMenu = new ContextMenu();
  58. contextMenu_.hideBuiltInItems();
  59. var menu:ContextMenuItem = new ContextMenuItem("Powered by phpwind", false, false);
  60. //menu.addEventListener(Event.OPEN,goPHPWind);
  61. contextMenu_.customItems.push(menu);
  62. this.contextMenu = contextMenu_;
  63. this.stop();
  64. file = new FileReference();
  65. file.addEventListener(Event.SELECT, selectHandler);//选择图片
  66. file.addEventListener(Event.CANCEL, cancelHandler);//取消选择
  67. file.addEventListener(Event.COMPLETE, loadFile);//载入完成
  68. getimg.addEventListener(MouseEvent.CLICK, upload);//点击选择按钮
  69. cameraButton.addEventListener(MouseEvent.CLICK,openCamera);
  70. localUploadButton.addEventListener(MouseEvent.CLICK,swichLocalUpload);
  71. loader = new Loader();
  72. loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadImg);//载入图片
  73. //load default image
  74. var mvParams:Object = stage.loaderInfo.parameters;
  75. var avatar:String = String(mvParams["avatar"]);
  76. //avatar = 'http://localhost/NextWind/www/attachment/avatar/000/00/00/2_middle.jpg';
  77. if(avatar!== 'undefined') {
  78. var imageLoader1:Loader = new Loader();
  79. var imageLoader2:Loader = new Loader();
  80. var imageLoader3:Loader = new Loader();
  81. var urlRequest:URLRequest = new URLRequest(avatar);
  82. //disabled flash cache 跨子域图片不显示
  83. //urlRequest.requestHeaders.push(new URLRequestHeader("pragma", "no-cache"));
  84. //urlRequest.requestHeaders.push(new URLRequestHeader("Expires", "Thu, 01 Jan 1970 00:00:00 GMT, -1"));
  85. //urlRequest.requestHeaders.push(new URLRequestHeader("Cache-Control", "no-cache, no-store, must-revalidate"));
  86. imageLoader1.load(urlRequest);
  87. imageLoader2.load(urlRequest);
  88. imageLoader3.load(urlRequest);
  89. imageLoader1.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded1);
  90. imageLoader2.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded2);
  91. imageLoader3.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded3);
  92. function imageLoaded1(e:Event):void {
  93. imageLoader1.width = imgPreview200_200_.width - 1;
  94. imageLoader1.height = imgPreview200_200_.height -1;
  95. imageLoader1.x = 1;
  96. imageLoader1.y = 1;
  97. // 把 imageLoader 加入到loaderImage_mc显示清单中
  98. imgPreview200_200_.addChild(imageLoader1);
  99. }
  100. function imageLoaded2(e:Event):void {
  101. imageLoader2.width = imgPreview120_120_.width - 1;
  102. imageLoader2.height = imgPreview120_120_.height -1;
  103. imageLoader2.x = 1;
  104. imageLoader2.y = 1;
  105. // 把 imageLoader 加入到loaderImage_mc显示清单中
  106. imgPreview120_120_.addChild(imageLoader2);
  107. }
  108. function imageLoaded3(e:Event):void {
  109. imageLoader3.width = imgPreview48_48_.width - 1;
  110. imageLoader3.height = imgPreview48_48_.height -1;
  111. imageLoader3.x = 1;
  112. imageLoader3.y = 1;
  113. // 把 imageLoader 加入到loaderImage_mc显示清单中
  114. imgPreview48_48_.addChild(imageLoader3);
  115. }
  116. }
  117. }
  118. private function goPHPWind(e:Event)
  119. {
  120. navigateToURL(new URLRequest('http://www.phpwind.net'),'_blank');
  121. }
  122. private function loadImg(e:Event):void
  123. {
  124. var bitmap:GIFPlayer = new GIFPlayer(true);;
  125. if (isGIF){
  126. bitmap.loadBytes(file.data);
  127. }else{
  128. var _tmp:Bitmap = Bitmap(loader.content);
  129. bitmap.bitmapData = _tmp.bitmapData;
  130. }
  131. gotoEditWindow(bitmap,isGIF);
  132. //swapChildren(editwindow,tip);
  133. }
  134. //切称到编辑界面
  135. private function gotoEditWindow(bitmap:GIFPlayer,isGIF:Boolean):void
  136. {
  137. if(this.currentFrame==1)
  138. {
  139. this.gotoAndStop(2);
  140. zoomIn.addEventListener(MouseEvent.CLICK, zoomInHandler);
  141. zoomOut.addEventListener(MouseEvent.CLICK, zoomOutHandler);
  142. clockwise.addEventListener(MouseEvent.CLICK, clockwiseHandler);
  143. anticlockwise.addEventListener(MouseEvent.CLICK, anticlockwiseHandler);
  144. reupload.addEventListener(MouseEvent.CLICK, upload);
  145. save.addEventListener(MouseEvent.CLICK, saveImg);
  146. }
  147. //判断旋转是否显示
  148. clockwise.visible = anticlockwise.visible = !isGIF;
  149. //建立预览框
  150. //200*200
  151. if(bigPreview)
  152. this.removeChild(bigPreview);
  153. bigPreview = new Bitmap(new BitmapData(200,200),'never',true);
  154. bigPreview.x = 350;
  155. bigPreview.y = 54;
  156. addChild(bigPreview);
  157. //120*120
  158. if(normalPreview)
  159. this.removeChild(normalPreview);
  160. normalPreview = new Bitmap(new BitmapData(118,118),'never',true);
  161. normalPreview.x = 575;
  162. normalPreview.y = 54;
  163. addChild(normalPreview);
  164. //48*48
  165. if(smallPreview)
  166. this.removeChild(smallPreview);
  167. smallPreview = new Bitmap(new BitmapData(46,46),'never',true);
  168. smallPreview.x = 575;
  169. smallPreview.y = 250;
  170. addChild(smallPreview);
  171. //载入到窗口
  172. if(editwindow){
  173. this.removeChild(editwindow);
  174. editwindow = null;
  175. }
  176. //主图显示区域
  177. imageCanvas = new Rectangle(0,54,299,299);
  178. editwindow = new EditWindow(
  179. bitmap,
  180. imageCanvas,
  181. bigPreview,
  182. smallPreview,
  183. normalPreview,
  184. slider,
  185. isGIF
  186. );
  187. addChild(editwindow);
  188. }
  189. //切换到摄像头界面
  190. private function openCamera(e:MouseEvent):void
  191. {
  192. this.gotoAndStop(3);
  193. localUploadButton.addEventListener(MouseEvent.CLICK,swichLocalUpload);//更换实例要得新绑定
  194. if(bigPreview) {
  195. //隐藏第一帧里的三个预览框
  196. bigPreview.visible = false;
  197. smallPreview.visible = false;
  198. normalPreview.visible = false;
  199. }
  200. //如果是从第二个编辑框点过来,编辑框移除
  201. if(editwindow) {
  202. removeChild(editwindow);
  203. editwindow = null;
  204. }
  205. if(video) {
  206. return;
  207. }
  208. if (!Camera.isSupported) {
  209. return;
  210. }
  211. cameraLoading.visible = true;
  212. camera = Camera.getCamera();
  213. if (camera == null) {
  214. cameraLoading.visible = false;
  215. return;
  216. }
  217. cameraErrorInfo.visible = false;
  218. //创建显示摄像头的容器
  219. video = new Video(400, 320);
  220. // 把视频放进去
  221. //video.smoothing = true;
  222. camera.setQuality(0, 100);
  223. camera.setMode(400, 320, 24, false);
  224. video.attachCamera(camera);
  225. video.x = 160;
  226. video.y = 50;
  227. addChild(video);
  228. captrueButton.visible = true;
  229. captrueButton.addEventListener(MouseEvent.CLICK,captureCamera);
  230. return;
  231. }
  232. //摄像头捕获
  233. private function captureCamera(e:MouseEvent):void
  234. {
  235. e.target.visible = false;
  236. var bitmapData:BitmapData = new BitmapData(video.width, video.height);
  237. bitmapData.draw(video);
  238. video.attachCamera(null);
  239. video.visible = false;
  240. var bitmap:GIFPlayer = new GIFPlayer(true);
  241. bitmap.bitmapData = bitmapData;
  242. /*var bit:BitmapData=new BitmapData(video.width,video.height);
  243. bit.draw(video);
  244. var bitmap:Bitmap=new Bitmap(bit);*/
  245. if(editwindow){
  246. removeChild(editwindow);
  247. editwindow = null;
  248. }
  249. removeChild(video);
  250. video = null;
  251. this.gotoAndStop(1);
  252. localUploadButton.addEventListener(MouseEvent.CLICK,swichLocalUpload);//更换实例要得新绑定
  253. cameraButton.addEventListener(MouseEvent.CLICK,openCamera);//更换实例要得新绑定
  254. gotoEditWindow(bitmap,false);
  255. }
  256. //切换到本地上传模式
  257. private function swichLocalUpload(e:MouseEvent):void
  258. {
  259. if(video) {
  260. video.attachCamera(null);
  261. removeChild(video);
  262. video = null;
  263. }
  264. if(editwindow){
  265. removeChild(editwindow);
  266. editwindow = null;
  267. }
  268. this.gotoAndStop(1);
  269. if(bigPreview) {
  270. bigPreview.visible = false;
  271. smallPreview.visible = false;
  272. normalPreview.visible = false;
  273. }
  274. cameraButton.removeEventListener(MouseEvent.CLICK,openCamera);
  275. cameraButton.addEventListener(MouseEvent.CLICK,openCamera);//更换实例要得新绑定
  276. getimg.removeEventListener(MouseEvent.CLICK,upload);
  277. getimg.addEventListener(MouseEvent.CLICK, upload);//点击选择按钮
  278. return;
  279. }
  280. private function zoomInHandler(e:MouseEvent):void
  281. {
  282. editwindow.zoomIn();
  283. }
  284. private function zoomOutHandler(e:MouseEvent):void
  285. {
  286. editwindow.zoomOut();
  287. }
  288. private function clockwiseHandler(e:MouseEvent):void
  289. {
  290. editwindow.clockwise();
  291. }
  292. private function anticlockwiseHandler(e:MouseEvent):void
  293. {
  294. editwindow.anticlockwise();
  295. }
  296. /**
  297. * 选择图片
  298. */
  299. private function upload(e:MouseEvent):void
  300. {
  301. hideTip();
  302. var filetype:Array = [new FileFilter('All Image Files(*.jpg;*.jpeg;*.gif;*.png)','*.jpg;*.jpeg;*.gif;*.png')];
  303. file.browse(filetype);
  304. }
  305. /**
  306. * 载入本地的图
  307. */
  308. private function loadFile(e:Event):void
  309. {
  310. if (file.type == '.gif'){
  311. isGIF = true;
  312. //隐藏两个按钮
  313. loader.loadBytes(file.data);
  314. }else{
  315. isGIF = false;
  316. loader.loadBytes(file.data);
  317. }
  318. }
  319. /***
  320. * 选择文件
  321. */
  322. private function selectHandler(e:Event):void
  323. {
  324. file.load();
  325. save && (save.enabled = true);
  326. }
  327. /**
  328. * 取消选择
  329. */
  330. private function cancelHandler(e:Event):void
  331. {
  332. }
  333. /***
  334. * 上传图片
  335. */
  336. private function saveImg(e:MouseEvent):void
  337. {
  338. var jpgeEncoder_:JPGEncoder;
  339. var bit2:ByteArray;
  340. if(isGIF){
  341. bit2 = editwindow.outputGIF();
  342. } else {
  343. jpgeEncoder_ = new JPGEncoder(90);
  344. bit2 = jpgeEncoder_.encode(bigPreview.bitmapData);
  345. }
  346. //var saveFace:String = stage.loaderInfo.parameters.postAction as String;
  347. var mvParams:Object = stage.loaderInfo.parameters;
  348. var uid:String = String(mvParams["uid"]);
  349. var token:String = String(mvParams["token"]);
  350. var _requestURL:String = String(mvParams["requestURL"]);
  351. var queryStr_:String = "uid=" + uid +"&token=" + token;
  352. var urlVariables_:URLVariables = new URLVariables(queryStr_);
  353. var urlRequest_:URLRequest = new URLRequest(_requestURL);
  354. urlRequest_.method = URLRequestMethod.POST;
  355. urlRequest_.data = urlVariables_;
  356. urlRequest_.contentType = "multipart/form-data; boundary=" + UploadPostHelper.getBoundary();
  357. urlRequest_.data = UploadPostHelper.getPostData("image.jpg", bit2, urlVariables_);
  358. var _urlLoader:URLLoader = new URLLoader();
  359. _urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
  360. _urlLoader.addEventListener(Event.COMPLETE, onUploadComplete);
  361. _urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
  362. _urlLoader.load(urlRequest_);
  363. }
  364. protected function onIOError(event_:IOErrorEvent):void
  365. {
  366. //dispatchEvent(new ErrorEvent(ErrorEvent.ERROR, false, false, "图片保存失败12,网络连接错误!"));
  367. //ExternalInterface.call("Wind.Util.resultTip({error : true,msg : '图片保存失败,网络连接错误!'})");
  368. showTip('图片保存失败,网络连接错误!');
  369. }
  370. protected function onUploadComplete(event_:Event):void
  371. {
  372. showTip('上传成功');
  373. /*
  374. dispatchEvent(new Event(Event.COMPLETE));
  375. var url:String = "javascript:uploadSuccess();void(0);";
  376. var urlRequest_:URLRequest = new URLRequest(url);
  377. //var header:URLRequestHeader=new URLRequestHeader("charset","utf-8");
  378. //urlRequest_.requestHeaders.push(header);
  379. navigateToURL(urlRequest_,'_self');
  380. */
  381. }
  382. private function errorHandlers(e:IOErrorEvent):void
  383. {
  384. showTip(Crop.IO_ERROR);
  385. }
  386. //显示错误信息
  387. private function showTip(txt:String):void
  388. {
  389. warning.text = txt;
  390. warning.visible = true;
  391. }
  392. // 隐藏错误信息
  393. private function hideTip():void
  394. {
  395. warning.visible = false;
  396. }
  397. /*private function errorHandlers(e:IOErrorEvent):void
  398. {
  399. showTip(Crop.IO_ERROR);
  400. }
  401. //显示错误信息
  402. private function showTip(txt:String):void
  403. {
  404. if(this.currentFrame == 1)
  405. {
  406. warning.text = txt;
  407. warning.visible = true;
  408. }else{
  409. tip.slider.warning.text = txt;
  410. tip.play();
  411. save.enabled = false;
  412. }
  413. }
  414. // 隐藏错误信息
  415. private function hideTip():void
  416. {
  417. if(this.currentFrame == 1)
  418. {
  419. warning.visible = false;
  420. }else{
  421. tip.gotoAndStop(0);
  422. }
  423. }**/
  424. }
  425. }