Flex中如何利用encodeImageAsBase64()事件将一个ImageSnapshot对象转化为Base-64编码字符串的例子

By Minidxer | December 30, 2008

接下来的例子演示了Flex中如何利用encodeImageAsBase64()事件,将一个ImageSnapshot对象转化为Base-64编码字符串。

让我们先来看一下Demo可以右键View Source或点击这里察看源代码):


下面是完整代码(或点击这里察看):

Download: main.mxml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
  3.         layout="vertical"
  4.         verticalAlign="middle"
  5.         backgroundColor="white">
  6.  
  7.     <mx:Script>
  8.         <![CDATA[
  9.             import flash.events.FocusEvent;
  10.             import flash.system.System;
  11.             import mx.graphics.ImageSnapshot;
  12.  
  13.             private function button_click(evt:MouseEvent):void {
  14.                 var ohSnap:ImageSnapshot = ImageSnapshot.captureImage(img);
  15.                 textArea.text = ImageSnapshot.encodeImageAsBase64(ohSnap);
  16.             }
  17.  
  18.             private function textArea_focusIn(evt:FocusEvent):void {
  19.                 textArea.setSelection(0, textArea.text.length);
  20.             }
  21.         ]]>
  22.     </mx:Script>
  23.  
  24.     <mx:ApplicationControlBar dock="true">
  25.         <mx:Button label="Capture and encode"
  26.                 click="button_click(event);" />
  27.     </mx:ApplicationControlBar>
  28.  
  29.     <mx:Form>
  30.         <mx:FormItem label="source:">
  31.             <mx:Image id="img"
  32.                     source="@Embed('images/flex_logo.jpg')" />
  33.         </mx:FormItem>
  34.         <mx:FormItem label="Base64:">
  35.             <mx:TextArea id="textArea"
  36.                     editable="false"
  37.                     showScrollTips="true"
  38.                     width="320"
  39.                     height="160"
  40.                     focusIn="textArea_focusIn(event);" />
  41.         </mx:FormItem>
  42.         <mx:FormItem>
  43.             <mx:Button label="Copy to clipboard"
  44.                     enabled="{textArea.text.length > 0}"
  45.                     click="System.setClipboard(textArea.text);" />
  46.         </mx:FormItem>
  47.     </mx:Form>
  48.  
  49. </mx:Application>
代码:Peter deHaan 翻译/整理/编译:中文Flex例子

Topics: Other | 1 Comment » | 205 views Tags: , , , ,

Search Posts