amcharts java另存圖(servlet)

來源:互聯網
上載者:User

package servlet;

import java.awt.Graphics2D;  
import java.awt.RenderingHints;  
import java.awt.image.BufferedImage;  
import java.io.IOException;  
 
import javax.imageio.ImageIO;  
import javax.servlet.ServletException;  
import javax.servlet.ServletOutputStream;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
 
import com.sun.image.codec.jpeg.JPEGCodec;  
import com.sun.image.codec.jpeg.JPEGEncodeParam;  
import com.sun.image.codec.jpeg.JPEGImageEncoder;  
 
/** 
 * 這個Servlet處理圖表頁面傳來的匯出圖片請求 
 *  
 * @author 廖瀚卿 
 *  
 */ 
@SuppressWarnings("serial")  
public class ExportImage extends HttpServlet {  
 
    public void doGet(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
        this.doPost(request, response);  
    }  
 
    public void doPost(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
        // 頁面flash的寬度和高度  
        int width = Integer.parseInt(request.getParameter("width"));  
        int height = Integer.parseInt(request.getParameter("height"));  
 
        BufferedImage result = new BufferedImage(width, height,  
                BufferedImage.TYPE_INT_RGB);  
        // 頁面是將一個個像素作為參數傳遞進來的,所以如果圖表越大,處理時間越長  
        for (int y = 0; y < height; y++) {  
            int x = 0;  
            String[] row = request.getParameter("r" + y).split(",");  
            for (int c = 0; c < row.length; c++) {  
                String[] pixel = row[c].split(":"); // 十六進位顏色數組  
                int repeat = pixel.length > 1 ? Integer.parseInt(pixel[1]) : 1;  
                for (int l = 0; l < repeat; l++) {  
                    result.setRGB(x, y, Integer.parseInt(pixel[0], 16));  
                    x++;  
                }  
            }  
        }  
        response.setContentType("image/jpeg");  
        response.addHeader("Content-Disposition",  
                "attachment; filename=/"amchart.jpg/"");  
        Graphics2D g = result.createGraphics();  
        // 處理圖形平滑度  
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,  
                RenderingHints.VALUE_ANTIALIAS_ON);  
        g.drawImage(result, 0, 0, width, height, null);  
        g.dispose();  
 
        ServletOutputStream f = response.getOutputStream();  
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(f);  
        JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(result);  
        param.setQuality((float) (100 / 100.0), true);// 設定圖片品質,100最大,預設70  
        encoder.encode(result, param);  
        ImageIO.write(result, "JPEG", response.getOutputStream());// 輸出圖片  
        f.close();  
    }  
 
   
    public static void main(String[] a){
     System.out.println("aaa");
    }

 

//--------------------------------------------------------------------------------------------

//------------頁面代碼--------------------------------------------------------------------

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>ampie</title>

        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
        <title>2008&#24180;&#31532;29&#23626;&#21271;&#20140;&#22885;&#36816;&#20250;&#37329;&#29260;&#27036;</title> 
        <script type="text/javascript" src="ampie/swfobject.js"></script> 
        <script type="text/javascript"> 
             function exportImage() {  
                 var flashMovie = document.getElementById('ampie');  
                 if (flashMovie) {  
                    flashMovie.exportImage('ExportImage');   
                 }  
              }   
        </script> 
</head> 

 

<body>
<!-- saved from url=(0013)about:internet -->
<!-- ampie script-->
<script type="text/javascript" src="../ampie/swfobject.js"></script>
 <div id="flashcontent">
  <strong>You need to upgrade your Flash Player</strong>
 </div>

 <script type="text/javascript">
  // <![CDATA[  
  var so = new SWFObject("../ampie/ampie.swf", "ampie", "100%", "100%", "8", "#000000");
  so.addVariable("path", "../ampie/");
  so.addVariable("settings_file", escape("ampie/ampie3/ampie_settings.xml"));
  so.addVariable("data_file", escape("ampie/ampie3/ampie_data.xml"));
  so.addVariable("preloader_color", "#ffffff");
  so.write("flashcontent");
  // ]]>
 </script>
<!-- end of ampie script -->

 

<hr noshade size="1"> 
<input type="button" value="&#23548;&#20986;&#22270;&#29255;" onclick="exportImage();" />
</body>
</html>

 

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.