package wonsama.util.image

{

       import flash.display.IBitmapDrawable;

       import flash.net.URLRequest;

       import flash.net.URLVariables;

      

       import mx.graphics.ImageSnapshot;

       import mx.graphics.codec.PNGEncoder;

      

       /**

        * 화면 캡춰를 도와주는 유틸 클래스

        * */

       public final class WCapture

       {

            

             private static const _targetUrl:String = "http://localhost:8080/blazeds/web/imageProcess.jsp";

            

             /**

              * 지정한 UIComponent 화면 캡춰한다.

              * 기본적으로 설정된 서버 주소를 참조

              * @param target 스샷을 찍을 대상체(UIComponent여야 함에 유의)

              * @param targetUrl 값을 전송할 URL

              * */

             public static function takePicture(target:IBitmapDrawable,targetUrl:String=_targetUrl):void{

                   

                    var onSnapShot:ImageSnapshot = ImageSnapshot.captureImage(target);

                    ImageSnapshot.defaultEncoder = mx.graphics.codec.PNGEncoder;       //JPEGEncoder 화질이 떨어짐.75%부터 동작유의

                    var url:URLRequest = new URLRequest(targetUrl);

                                       

                    var params:URLVariables = new URLVariables();

                    params.imageData = ImageSnapshot.encodeImageAsBase64(onSnapShot);       //64Base 인코딩한 것을 파라미터로 설정한다.

                   

                    url.method = "POST";

                    url.data = params;

                    flash.net.navigateToURL(url,'_self');

             }

      

}

=====> 아래글은 참조 원문입니다. 감사합니다 쿠노기 님 ^^

[출처] 간단한 이미지 캡쳐 소스 - Flex 3.0 [JSP] (FlexComponent) |작성자 쿠노기

============================================================================

ImageCapture.mxml

============================================================================

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
  initialize="System.useCodePage=true" width="669" height="393">
 <mx:Script>
  <![CDATA[

   import mx.collections.ArrayCollection;
   import mx.graphics.ImageSnapshot;
   import mx.graphics.codec.*;


   [Bindable]
       public var expenses:ArrayCollection = new ArrayCollection([
          {Month:"Jan", Profit:2000, Expenses:1500},
          {Month:"Feb", Profit:1000, Expenses:200},
          {Month:"Mar", Profit:1500, Expenses:500}
       ]);
      
   private function doCapture():void{
    ImageSnapshot.defaultEncoder =  PNGEncoder;   //defaultEncoder 의 codec 타입을 jpeg로 했더니 화질이 좀 깨져버리네용...
    var onSnapShot:ImageSnapshot = ImageSnapshot.captureImage(myChartVBox);    //걍 챠트를 캡펴했더니 배경이 검게 나와서리 VBOX에 챠트를 넣고 VBOX를 캡쳐 했습니당..
    encodeType.text = onSnapShot.contentType;
    myTextarea.text = ImageSnapshot.encodeImageAsBase64(onSnapShot);
    //myTextarea.text = onSnapShot.contentType;

   }

   private function  sendChartImageToJsp():void{
    doCapture();
    var url:URLRequest = new URLRequest("http://{서버}/decoding.jsp");
    var params:URLVariables = new URLVariables();
    params.test = myTextarea.text;
    url.method = "POST";
    url.data = params;
    navigateToURL(url,'_self');
   }
  ]]>
 </mx:Script>
 <mx:Button x="10" y="337" label="캡쳐" click="doCapture()"/>
 <mx:TextArea x="10" y="191" id="myTextarea" width="558" height="108"/>
 <mx:TextInput x="10" y="307" width="558" id="encodeType"/>
 <mx:Button x="66" y="337" label="Send" click="sendChartImageToJsp()"/>
 <mx:VBox x="10" y="10" width="558" height="173" backgroundColor="#FFFFFF" id="myChartVBox">
  <mx:ColumnChart id="myChart" dataProvider="{expenses}" width="558" height="173" showDataTips="true">
      <mx:horizontalAxis>
         <mx:CategoryAxis  categoryField="Month"/>
      </mx:horizontalAxis>
      <mx:series>
         <mx:ColumnSeries xField="Month" yField="Profit" displayName="Profit" />
         <mx:ColumnSeries xField="Month" yField="Expenses" displayName="Expenses" />
      </mx:series>
   </mx:ColumnChart>
 </mx:VBox>
 
</mx:Application>

============================================================================

decoding.jsp    /서버단 jsp 코딩...초 간당....

============================================================================

<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>

<%@ page import="java.io.*" %>
<%@ page import="sun.misc.*" %>                  //sun.misc.BASE64Decoder 사용
<%
String str=request.getParameter("test");
OutputStream outs = null;
try{
// 쿠키 등의 정보를 지우므로 이전에 세팅을 해서는 않된다네요...^^ 
     response.reset(); 
     response.setContentType("application/smnet");
     response.setHeader("Content-Disposition","attachment; filename=capture.png");     //filename에 원하는 저장 파일의 이름을 ..확장자는 .png로 주세요
     response.setHeader("Content-Transfer-Encoding", "binary;");
     response.setHeader("Pragma", "no-cache;");
     response.setHeader("Expires", "-1;");


     BASE64Decoder decode= new BASE64Decoder(); 
     byte[] bt= decode.decodeBuffer(str);
     outs = response.getOutputStream();
     outs.write(bt);
     outs.close();
}finally{
     outs.close();
}
%>


[ JSP 코딩 참조사항 ]
outs = response.getOutputStream();  라인을
OutputStream outs = null; 아래 바로 적어주면 getOutputStream()을 또 호출했습니다. 라는 Exception이 발생하지 않게 됩니다.


+ Recent posts