微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Flex复制图片到剪贴板

经过两天的艰苦琢磨,终于搞出来了。

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
			   xmlns:s="library://ns.adobe.com/flex/spark"
			   xmlns:mx="library://ns.adobe.com/flex/mx"
			   minWidth="955" minHeight="600" initialize="application1_initializeHandler(event)">
	<fx:Declarations>
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->
	</fx:Declarations>
	<fx:Script>
		<![CDATA[
			import flash.desktop.Clipboard;
			import flash.desktop.ClipboardFormats;
			import flash.display.Sprite;
			import flash.net.URLRequest;
			import flash.system.System;
			import flash.system.fscommand;
			
			import mx.controls.Alert;
			import mx.controls.Image;
			import mx.events.FlexEvent;
			import mx.graphics.ImageSnapshot;
			import mx.graphics.codec.JPEGEncoder;
			import mx.utils.Base64Encoder;
			
			 
			[Bindable]
			private var imageData:ByteArray;
		 //调用js来把图片复制到剪贴板
			private static var function_setToJs:String="function(value){" +
				" var img=document.createElement('img');" +
				" var imgsrc='data:image/jpg;base64,'+value;" +
				" img.src=imgsrc;" +
				" img.contentEditable = 'true';" +
				" document.body.appendChild(img);" +
				" img.style.display='none';" +
				" var controlRange;" +
				" if (document.body.createControlRange) {" +
				" controlRange = document.body.createControlRange();" +
				" controlRange.addElement(img);" +
				" controlRange.execCommand('copy');}" +
				" img.contentEditable = 'false';" +
				"}";
			
			private function cutMap(e:MouseEvent):void {
				this.cutmap.visible=false;
				var img:BitmapData   = ImageSnapshot.captureBitmapData(this);
				var jpg:JPEGEncoder=new JPEGEncoder(50);
				imageData=jpg.encode(img);
				//				snap=ImageSnapshot.encodeImageAsBase64(ImageSnapshot.captureImage(this,new JPEGEncoder))
				this.savemap.visible=true;
				this.copymap.visible=true;
				
			}
			private function seveMap(e:MouseEvent):void{
				
				var fr:FileReference=new FileReference();
				fr.save(imageData,"map.jpg");
				this.cutmap.visible=true;
				this.savemap.visible=false;
				this.copymap.visible=false;
				
			}
			
			protected function application1_initializeHandler(event:FlexEvent):void
			{
				// Todo Auto-generated method stub
				this.cutmap.visible=true;
				this.savemap.visible=false;
				this.copymap.visible=false;
				
			}
			protected function copyMap(event:MouseEvent):void{
				var base64:Base64Encoder = new Base64Encoder();
				base64.encodeBytes(imageData);
				var base:String=base64.toString();
				ExternalInterface.call(function_setToJs,base);  
			}
			
			
		]]>
	</fx:Script>
	<mx:VBox width="100%" height="100%">
		<mx:HBox width="100%">
			<mx:Button id="cutmap" x="80" width="50" label="截图" buttonMode="true"
					   click="cutMap(event)" fontSize="12"/> 
			<mx:Button id="savemap" width="50" label="另存为" buttonMode="true" click="seveMap(event)"
					   fontSize="12"/> 
			<mx:Button id="copymap" width="50" label="复制" buttonMode="true" fontSize="12" click="copyMap(event)"/> 
		</mx:HBox>		
		
	</mx:VBox>
	
</s:Application>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐