Flex中如何利用ImageSnapshot类的defaultEncoder属性给截图设置一个默认译码器的例子

By Minidxer | December 31, 2008

和前面Flex中如何利用encodeImageAsBase64()事件将一个ImageSnapshot对象转化为Base-64编码字符串的例子类似的,接下来的例子演示了Flex中如何利用ImageSnapshot类的defaultEncoder属性,给截图设置一个默认译码器。

让我们先来看一下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.         creationComplete="init();">
  7.  
  8.     <mx:Script>
  9.         <![CDATA[
  10.             import mx.graphics.ImageSnapshot;
  11.             import mx.graphics.codec.*;
  12.  
  13.             private function init():void {
  14.                 ImageSnapshot.defaultEncoder = PNGEncoder;
  15.             }
  16.  
  17.             private function captureImg():void {
  18.                 var ohSnap:ImageSnapshot = ImageSnapshot.captureImage(img);
  19.                 textArea.text = ImageSnapshot.encodeImageAsBase64(ohSnap);
  20.                 lbl.text = ohSnap.contentType;
  21.             }
  22.         ]]>
  23.     </mx:Script>
  24.  
  25.     <mx:ApplicationControlBar dock="true">
  26.         <mx:Button id="button"
  27.                 label="Capture Image"
  28.                 click="captureImg();" />
  29.     </mx:ApplicationControlBar>
  30.  
  31.     <mx:Form>
  32.         <mx:FormItem label="source:">
  33.             <mx:Image id="img"
  34.                     source="@Embed('images/flex_logo.jpg')" />
  35.         </mx:FormItem>
  36.         <mx:FormItem label="Base64:">
  37.             <mx:TextArea id="textArea"
  38.                     editable="false"
  39.                     width="320"
  40.                     height="160" />
  41.         </mx:FormItem>
  42.         <mx:FormItem label="contentType:">
  43.             <mx:Label id="lbl" />
  44.         </mx:FormItem>
  45.     </mx:Form>
  46.  
  47. </mx:Application>
代码:Peter deHaan 翻译/整理/编译:中文Flex例子

Topics: Other | No Comments » | Tags: , , , ,

Search Posts