前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >AS3给图片加水印文字

AS3给图片加水印文字

作者头像
meteoric
发布2018-11-16 17:03:28
6500
发布2018-11-16 17:03:28
举报
文章被收录于专栏:游戏杂谈

<需要flash player 10+支持>

通过FileReference获取选中的图片,调用它的load方法,将图片存入到内存中。使用Loader(flash.display.Loader)类的loadBytes方法,加载图片。

在点击“加水印”的按钮时,建立两个BitmapData,然后里面分别存放图片和需要添加的文字,之后将两个BitmapData转换为Bitmap添加至舞台,再转成一张新的Bitmap。

保存图片功能则较为简单了,直接使用FileReference的save方法

实现的并不完善,缺少例如:选择字体大小、颜色、间距等功能,未提供平铺或是移动文字位置的功能…

示例代码如下<仅供参考>:

代码语言:javascript
复制
1: <?xml version="1.0" encoding="utf-8"?>

       2: <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 

       3:                 layout="absolute" 

       4:                 minWidth="400" minHeight="600" 

       5:                 creationComplete="inited()" 

       6:                 backgroundColor="#ffffff">

       7:     

       8:     <mx:Style source="theme/vistaremix/vistaremix.css" />

       9:     

      10:     <mx:Script>

      11:         <![CDATA[

      12:             import mx.controls.Alert;

      13:             import mx.core.UIComponent;

      14:             import mx.events.PropertyChangeEvent;

      15:             import mx.graphics.codec.PNGEncoder;

      16:             

      17:             private var file:FileReference;

      18:             private var loader:Loader;

      19:             

      20:             private function inited():void

      21:             {

      22:                 loader = new Loader();

      23:                 loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderDataCompleteHandler);

      24:                 loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loaderProgressHandler);

      25:                 

      26:                 var ui:UIComponent = new UIComponent;

      27:                 

      28:                 addChild(ui);

      29:                 

      30:                 ui.addChild(loader);

      31:                 

      32:                 loader.visible = false;

      33:             }

      34:             

      35:             private function selectPictureHandler():void

      36:             {

      37:                 file = new FileReference();

      38:                 file.addEventListener(Event.SELECT, fileSelectHandler);

      39:                 file.browse(getFileFilterArr());

      40:             }

      41:             

      42:             private function getFileFilterArr():Array

      43:             {

      44:                 return [new FileFilter("Images(*.jpg;*.jpeg;*.png;*.gif)", "*.jpg;*.jpeg;*.png;*.gif")];

      45:             }

      46:             

      47:             private function fileSelectHandler(evt:Event):void

      48:             {

      49:                 file.removeEventListener(Event.SELECT, fileSelectHandler);

      50:                 file.addEventListener(Event.COMPLETE, fileLoadCompleteHandler);

      51:                 file.load();

      52:                 

      53:                 filePathTxt.text = file.name;

      54:             }

      55:             

      56:             private function fileLoadCompleteHandler(evt:Event):void

      57:             {

      58:                 file.removeEventListener(Event.COMPLETE, fileLoadCompleteHandler);

      59:                 loader.loadBytes(file.data);

      60:             }

      61:             

      62:             private function madePictureHandler():void

      63:             {

      64:                 if (loader.content == null)

      65:                 {

      66:                     Alert.show("请先选择要处理的图片","提示");

      67:                     

      68:                     return; 

      69:                 }

      70:                 

      71:                 var ui:UIComponent = new UIComponent();

      72:                 

      73:                 var txt:TextField=new TextField(); 

      74:                 var imgBitmapData:BitmapData = (loader.content as Bitmap).bitmapData.clone();

      75:                 

      76:                 var format:TextFormat = new TextFormat();

      77:                 format.size = 22; 

      78:                 format.bold = true;

      79:                 format.color=0xcc9900;

      80:                 format.align = TextFormatAlign.CENTER;

      81:                 txt.defaultTextFormat = format; 

      82:                 txt.htmlText = contentTxt.text || "形象装"; 

      83:                 txt.width = 170;

      84:                 txt.height = 50;

      85:                 

      86: //                txt.filters = [getTextGlowFilter()];

      87:                 

      88:                 var tempBitMapData:BitmapData = new BitmapData(txt.width, txt.height, true, 0x00000000); 

      89:                 tempBitMapData.draw(txt); 

      90:                 

      91:                 var txtShap:Shape = new Shape(); 

      92:                 txtShap.graphics.beginBitmapFill(tempBitMapData); 

      93:                 txtShap.graphics.drawRect(0,0, tempBitMapData.width, tempBitMapData.height); 

      94:                 txtShap.graphics.endFill(); 

      95:                 txtShap.x=(imgBitmapData.width-tempBitMapData.width) / 2; 

      96:                 txtShap.y=(imgBitmapData.height-tempBitMapData.height) / 2; 

      97: //                txtShap.alpha = .7;

      98:                 

      99: //                txtShap.rotation = 45 + 270;

     100:                 

     101:                 ui.addChild(new Bitmap(imgBitmapData)); 

     102:                 ui.addChild(txtShap); 

     103:                 

     104:                 var newBitmapData:BitmapData = new BitmapData(imgBitmapData.width, imgBitmapData.height, true, 0x00000000); 

     105:                 newBitmapData.draw(ui);

     106:                 

     107:                 var newBitmap:Bitmap = new Bitmap();

     108:                 newBitmap.bitmapData = newBitmapData;

     109:                 

     110:                 previewImg.source = newBitmap;            

     111:             }

     112:             

     113:             private function getTextGlowFilter(col:uint=0x000000,blur:Number=2):GlowFilter

     114:             {

     115:                 var color:uint             = col

     116:                 var alpha:Number        = 1;

     117:                 var blurX:Number        = blur

     118:                 var blurY:Number        = blur

     119:                 var strength:Number     = 255;

     120:                 var quality:Number      = 1     

     121:                 return new GlowFilter(color,alpha,blurX,blurY,strength,quality);      

     122:             }

     123:             

     124:             private function savePictureHandler():void

     125:             {

     126:                 if (loader.content == null || previewImg.source == null)

     127:                 {

     128:                     Alert.show("请先选择要处理的图片","提示");

     129:                     

     130:                     return; 

     131:                 }

     132:                 

     133:                 var pngenc:PNGEncoder = new PNGEncoder();

     134:                 

     135:                 var bitmapData:BitmapData = (previewImg.source as Bitmap).bitmapData.clone();

     136:                 

     137:                 var imgByteArray:ByteArray = pngenc.encode(bitmapData);

     138:                 

     139:                 var _file:FileReference = new FileReference();

     140:                 _file.addEventListener(Event.COMPLETE, downloadCompleteHandler);

     141:                 _file.save(imgByteArray, "_" + filePathTxt.text);

     142:             }

     143:             

     144:             private function downloadCompleteHandler(evt:Event):void

     145:             {

     146:                 Alert.show("图片已保存成功","提示");

     147:             }

     148:             

     149:             private function loaderDataCompleteHandler(evt:Event):void

     150:             {

     151:                 var bitmapData:BitmapData = new BitmapData(loader.width, loader.height, true, 0x00FFFFFF);

     152:                 

     153:                 bitmapData.draw(loader.content);

     154:                 

     155:                 var bitmap:Bitmap = new Bitmap();

     156:                 bitmap.bitmapData = bitmapData;

     157:                 

     158:                 previewImg.source = bitmap;

     159:             }

     160:             

     161:             private function loaderProgressHandler(evt:ProgressEvent):void

     162:             {

     163:                 var percent:Number = evt.bytesLoaded / evt.bytesTotal;

     164:                 

     165:                 percent *= 100;

     166:                 

     167:                 var msg:String = "正在加载:" + percent + "%";

     168:                 

     169:                 trace(msg);                

     170:             }

     171:             

     172:         ]]>

     173:     </mx:Script>

     174:     

     175:     

     176:     <mx:VBox width="100%" height="100%" paddingLeft="20" verticalGap="5">

     177:         

     178:         <mx:HBox width="100%" paddingTop="20" horizontalGap="5">

     179:             

     180:             <mx:TextInput id="filePathTxt" width="300" editable="false" />

     181:             

     182:             <mx:Button label="选择图片" id="selectBtn" click="selectPictureHandler()" />

     183:             

     184:         </mx:HBox>

     185:         

     186:         <mx:HBox width="100%" horizontalGap="5" verticalAlign="top">

     187:             

     188:             <mx:TextArea id="contentTxt" width="300" />

     189:             

     190:             <mx:Button label="加水印" id="madeBtn" click="madePictureHandler()" />

     191:             

     192:             <mx:Button label="保存图片" id="saveBtn" click="savePictureHandler()" />

     193:             

     194:         </mx:HBox>

     195:         

     196:         <mx:Canvas width="350" height="500" id="imgContainer" left="20" top="60" 

     197:                    borderColor="#3399cc" borderThickness="1" borderStyle="solid" >

     198:             

     199:             <mx:Image id="previewImg" horizontalAlign="0" verticalCenter="0" />

     200:             

     201:         </mx:Canvas>

     202:         

     203:     </mx:VBox>

     204:     

     205: </mx:Application>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2012-06-29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档