<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-19  評(píng)論-128  文章-1  trackbacks-0
    圖片剪切功能:

    效果圖:






    flex代碼:

     

    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="init()" xmlns:local="astion.*">
     
    <mx:Script>
      
    <![CDATA[
       import mx.controls.Image;
       import mx.graphics.ImageSnapshot;
       import flash.net.FileReference;
       import mx.graphics.codec.JPEGEncoder;
       import mx.managers.PopUpManager;
       import mx.containers.TitleWindow;
       import mx.controls.Alert;
       import mx.events.CloseEvent;
       import mx.core.IFlexDisplayObject;
       import mx.utils.*;
       import mx.core.Application;
       import astion.Dot;
       import astion.ScaleBox;
       
       public static const LINE_WIDTH:Number = 1;//縮放邊框?qū)挾?br />    private var file:FileReference;
       public var IMAGE_URL:String="http://localhost:8080/cutPicuter/aa/aa.jpg";
       private var loader:Loader;
       private var bmp:Bitmap;
                private var stream:URLStream;
                public var realPath:String="D:\myWorkSpace\cutPicuter\WebRoot\aa\aa.jpg";
       
       //初始化數(shù)據(jù)
       private function init():void{
        this.loader = new Loader();
                    this.stream = new URLStream();
                    this.loader.contentLoaderInfo.addEventListener(Event.COMPLETE,this.onComplete);
                    this.loader.load(new URLRequest(encodeURI(this.IMAGE_URL)));//解決中文亂碼
                    this.stream.load(new URLRequest(encodeURI(this.IMAGE_URL)));
                    this.stream.addEventListener(Event.COMPLETE,this.onLoaded);
       }
       private function onLoaded(e:Event):void
                {                                
                    var bytearray:ByteArray = new ByteArray();    
                    this.stream.readBytes(bytearray);
                    
                    if(this.stream.connected)
                        this.stream.close();
                        
                    this.loader.loadBytes(bytearray);
                }
                private function onComplete(e:Event):void
                {
                    try
                    {
                        this.bmp = this.loader.content as Bitmap;
                        var showImage:Image= new Image();
                        showImage.source=this.loader.content;
                        canvas.addChild(showImage);
                        canvas.setChildIndex(box,1);
                        canvas.setChildIndex(showImage,0);
                    }
                    catch(e:Error)
                    {
                        
                    }
                }
       
       //截圖,顯示縮放選擇框
       private function doCapture():void{
        box.x = 100;
        box.y = 100;
        box.visible = true;
       }
       
       //獲取縮放選擇框內(nèi)的圖像
       private function getImg():BitmapData{
        //截取整個(gè)區(qū)域
        box.scaleEnable = false;
        var bmp:BitmapData = ImageSnapshot.captureBitmapData(canvas);
        box.scaleEnable = true;
        
        //矩形為要截取區(qū)域                
                    var re:Rectangle = new Rectangle(box.x+LINE_WIDTH,box.y+LINE_WIDTH,box.boxWidth-LINE_WIDTH,box.boxHeight-LINE_WIDTH); 
                    var bytearray:ByteArray = new ByteArray();   
                    //截取出所選區(qū)域的像素集合                        
                    bytearray = bmp.getPixels(re); 
                    
                    
                    var imgBD:BitmapData = new BitmapData(box.boxWidth-LINE_WIDTH,box.boxHeight-LINE_WIDTH);       
                    //當(dāng)前的bytearray.position為最大長(zhǎng)度,要設(shè)為從0開(kāi)始讀取       
                    bytearray.position=0;            
                    var fillre:Rectangle = new Rectangle(0,0,box.boxWidth-LINE_WIDTH,box.boxHeight-LINE_WIDTH);
                    //將截取出的像素集合存在新的bitmapdata里,大小和截取區(qū)域一樣
                    imgBD.setPixels(fillre,bytearray);
                    
                    return imgBD;
       }
       
       //預(yù)覽圖片
       private function doScan():void{
        var t:TitleWindow = new TitleWindow();
        t.showCloseButton=true;
        t.addEventListener(CloseEvent.CLOSE,closeWindow);
        t.width = box.boxWidth+t.getStyle("borderThickness");
        t.height =box.boxHeight+t.getStyle("borderThickness")+t.getStyle("headerHeight");
        var img:Image = new Image();
        img.width = box.boxWidth;
        img.height = box.boxHeight; 
        img.source = new Bitmap(getImg());
        t.addChild(img);
        PopUpManager.addPopUp(t,this,true);
        PopUpManager.centerPopUp(t);
       }
       
       private function closeWindow(e:CloseEvent):void{            
                    var t:TitleWindow = e.currentTarget as TitleWindow;                    
                    PopUpManager.removePopUp(t);                
                }
                
                //保存圖片到本地
       private function downloadPicture():void{
        file=new FileReference();
        file.addEventListener(Event.COMPLETE,downloadComplete);
        file.save(new JPEGEncoder(80).encode(getImg()),"default.jpg");
       }
       
       private function downloadComplete(event:Event):void{
        Alert.show("成功保存圖片到本地!","提示");
       }
       
       //保存圖片到服務(wù)器即覆蓋原來(lái)的圖片
       private function save():void{
        Alert.show("是否保存剪切圖片?","提示",3, this, function(event:CloseEvent):void {
              if (event.detail==Alert.YES){
               var request:URLRequest = new URLRequest("http://localhost:8080/cutPicuter/servlet/FileManagerSaveFileServlet?realPath="+encodeURIComponent(StringUtil.trim(realPath)));
         request.method=URLRequestMethod.POST;
         request.contentType = "application/octet-stream";
         request.data = new JPEGEncoder(80).encode(getImg());
         var loader:URLLoader = new URLLoader();
         loader.load(request);
         loader.addEventListener(Event.COMPLETE,saveResult);

              }});
       }
       
       private function saveResult(event:Event):void{
        Application.application.reLoadFolderFiles(realPath.substr(0,realPath.lastIndexOf("\\")));
        Alert.show("保存剪切圖片成功","提示");
       }
      
    ]]>
     
    </mx:Script>
     
    <mx:HBox x="0" y="0">
            
    <mx:LinkButton label="剪裁" click="doCapture();" icon="@Embed('assets/cut.png')"/>
            
    <mx:LinkButton label="預(yù)覽" click="doScan();" icon="@Embed('assets/ok.png')"/>
            
    <mx:VRule height="22"/>
            
    <mx:LinkButton label="保存"  click="save()"  icon="@Embed('assets/save.png')"/>
            
    <mx:LinkButton label="另存為" click="downloadPicture();" icon="@Embed('assets/saveAs.png')"/>
        
    </mx:HBox>
     
    <mx:Canvas id="canvas" y="23" x="1">
     
    <local:ScaleBox id="box" visible="false" y="0" x="0" width="100" height="100"/>
     
    </mx:Canvas>
    </mx:Application>



    java代碼:

     

    package com;


    import java.io.DataOutputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    /**
     * Servlet implementation class FileManagerSaveFileServlet
     
    */
    public class FileManagerSaveFileServlet extends HttpServlet {
     
     
    private int len=0;//處理流
     private int mm=0;//重命名
     private String fileName="";//文件原名
     private String extName="";//文件擴(kuò)展名
     private String tempFileName="";//文件名加擴(kuò)展名
     
     
    public void doGet(HttpServletRequest request, HttpServletResponse response)    
     
    throws ServletException, IOException {    
     processRequest(request, response);    
     }    
       
     
    public void doPost(HttpServletRequest request, HttpServletResponse response)    
      
    throws ServletException, IOException {    
     processRequest(request, response);    
     }    
     
     
    public void processRequest(HttpServletRequest request, HttpServletResponse response)

        
    throws ServletException, IOException {
      request.setCharacterEncoding(
    "utf-8");
      String realPath
    =request.getParameter("realPath");
      
    //System.out.println("FMSFS-->realPath:"+realPath);
      response.setContentType("application/octet-stream");
      InputStream is 
    = request.getInputStream();
      
    try {
      
    int size = 0;
      
    byte[] tmp = new byte[100000];
      
      tempFileName
    =realPath.substring(realPath.lastIndexOf("\\")+1);//切割獲得文件名加擴(kuò)展名
      fileName=tempFileName.substring(0,tempFileName.lastIndexOf("."));//切割獲得文件名
      
    //確保獲得真實(shí)的文件名如:1(1)可以獲得真實(shí)為1,
      if(fileName.indexOf("(")!=-1){
       fileName
    =fileName.substring(0,fileName.indexOf("("));
      }
      
      extName
    =tempFileName.substring(tempFileName.lastIndexOf("."));//切割獲得擴(kuò)展名
      
      
    //調(diào)用遞歸方法
      fileName+=reNameFile(realPath.substring(0,realPath.lastIndexOf("\\")+1),fileName,extName);
      
    // 創(chuàng)建一個(gè)文件夾用來(lái)保存發(fā)過(guò)來(lái)的圖片;
      File f = new File(realPath.substring(0,realPath.lastIndexOf("\\")+1)+fileName+extName);
      DataOutputStream dos 
    = new DataOutputStream(new FileOutputStream(f));
      
    while ((len = is.read(tmp)) != -1) {
      dos.write(tmp, 
    0, len);
      size 
    += len;
      }
      dos.flush();
      dos.close();
      } 
    catch (IOException e) {
      e.printStackTrace();
      }
     }
     
     
    //遞歸來(lái)重命名文件名
     String str="";
     
    public String reNameFile(String realPath,String filename,String extName){
      File file 
    =new File(realPath+"\\"+filename+extName);
      str
    ="";
            
    if(file.exists()){
             mm
    ++;
             str
    ="_"+mm;
             reNameFile(realPath,fileName
    +str,extName);
            }
    else{
             
    if(mm!=0){
          str
    ="_"+mm;
             }
            }
      
    return str;
     }
    }

     


     

    源碼: flex圖片剪切示例


    原創(chuàng)人員:Denny

    posted on 2010-09-01 09:55 obpm 閱讀(8605) 評(píng)論(6)  編輯  收藏 所屬分類: 控件 、Flex

    評(píng)論:
    # re: flex圖片剪切示例--預(yù)覽、保存到本地、保存到服務(wù)器(附源碼) 2010-09-09 21:15 | fk01
    請(qǐng)問(wèn)樓主,你這個(gè)拖動(dòng)的時(shí)候選擇框怎么控制在的范圍啊  回復(fù)  更多評(píng)論
      
    # re: flex圖片剪切示例--預(yù)覽、保存到本地、保存到服務(wù)器(附源碼) 2010-09-10 16:02 | axsz4251
    很棒的學(xué)習(xí)資料,感謝提供!  回復(fù)  更多評(píng)論
      
    # re: flex圖片剪切示例--預(yù)覽、保存到本地、保存到服務(wù)器(附源碼) 2011-10-25 16:33 | mymoon
    前臺(tái)的Application.application.reLoadFolderFiles方法已經(jīng)被官方屏蔽了,不能用了,還有別的方法嗎?  回復(fù)  更多評(píng)論
      
    # re: flex圖片剪切示例--預(yù)覽、保存到本地、保存到服務(wù)器(附源碼)[未登錄](méi) 2011-11-09 11:17 | tom
    樓主,是不是還少個(gè)類啊 local:ScaleBox   回復(fù)  更多評(píng)論
      
    # re: flex圖片剪切示例--預(yù)覽、保存到本地、保存到服務(wù)器(附源碼) 2011-11-18 09:33 | 慕容若雪
    是不是少點(diǎn)東西啊,樓主  回復(fù)  更多評(píng)論
      
    # re: flex圖片剪切示例--預(yù)覽、保存到本地、保存到服務(wù)器(附源碼) 2011-11-18 09:34 | 慕容若雪
    加我QQ:119449201

    還有些不明白的地方想向你請(qǐng)教  回復(fù)  更多評(píng)論
      
    主站蜘蛛池模板: 毛片免费视频在线观看| 亚洲一本之道高清乱码| 女性自慰aⅴ片高清免费| 无码av免费一区二区三区试看| 亚洲国产精品成人综合色在线| 亚洲一区二区三区四区在线观看| 亚洲精品无码99在线观看| 成年美女黄网站色大免费视频| 日日麻批免费40分钟无码| 国产精品一区二区三区免费| 国产精品亚洲专区无码牛牛| 亚洲国产综合精品中文第一| 亚洲精品视频在线观看免费| 婷婷精品国产亚洲AV麻豆不片 | 国产亚洲成av人片在线观看| 国产在线不卡免费播放| 成人a免费α片在线视频网站| **一级毛片免费完整视| 人妻无码一区二区三区免费| a级成人免费毛片完整版| 国产精品九九久久免费视频 | 午夜高清免费在线观看| 国产福利视精品永久免费 | 亚洲中文无码a∨在线观看| 久久亚洲精品国产精品| 亚洲AV无码精品色午夜果冻不卡| 亚洲午夜久久久久久久久久| 亚洲人成人网站在线观看| 亚洲高清视频一视频二视频三| 国产免费爽爽视频免费可以看| 国产精品无码素人福利免费| 全免费a级毛片免费**视频| 四虎影视大全免费入口| 日韩一级视频免费观看| 国产jizzjizz视频免费看| 丁香亚洲综合五月天婷婷| 亚洲国产av一区二区三区| 亚洲无线码一区二区三区| 亚洲av永久无码精品古装片| 亚洲免费在线视频| 亚洲另类春色校园小说|