posts - 41, comments - 15, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          java將svg圖片轉換為png格式圖片

          Posted on 2015-10-19 15:17 yuhaibo736 閱讀(319) 評論(0)  編輯  收藏

          有時候在客戶端使用svg畫圖,而在服務器端需要同樣的圖片,在服務器端重新畫一遍是非常費事的。這時候我們就可以利用已有的svg直接通過下面的類轉換成png格式。

          使用這個方法需要引用batic相關的包,maven pom文件如下:

          <!-- svg 生成png格式圖片  -->
          <dependency><groupId>batik</groupId><artifactId>batik-svggen</artifactId><version>1.6</version></dependency>        
          <dependency><groupId>batik</groupId><artifactId>batik-awt-util</artifactId><version>1.6</version></dependency>
          <dependency><groupId>batik</groupId><artifactId>batik-bridge</artifactId><version>1.6</version></dependency>
          <dependency><groupId>batik</groupId><artifactId>batik-css</artifactId><version>1.6</version></dependency>
          <dependency><groupId>batik</groupId><artifactId>batik-dom</artifactId><version>1.6</version></dependency>
          <dependency><groupId>batik</groupId><artifactId>batik-gvt</artifactId><version>1.6</version></dependency>
          <dependency><groupId>batik</groupId><artifactId>batik-parser</artifactId><version>1.6</version></dependency>
          <dependency><groupId>batik</groupId><artifactId>batik-script</artifactId><version>1.6</version></dependency>
          <dependency><groupId>batik</groupId><artifactId>batik-svg-dom</artifactId><version>1.6</version></dependency>
          <dependency><groupId>batik</groupId><artifactId>batik-transcoder</artifactId><version>1.6</version></dependency>
          <dependency><groupId>batik</groupId><artifactId>batik-util</artifactId><version>1.6</version></dependency>
          <dependency><groupId>batik</groupId><artifactId>batik-xml</artifactId><version>1.6</version></dependency>
          <!-- 此處不能使用2.9.1版本,使用2.9.1生成png會失敗 -->
          <dependency><groupId>xerces</groupId><artifactId>xercesImpl</artifactId><version>2.5.0</version></dependency>
          <dependency><groupId>xml-apis</groupId><artifactId>xmlParserAPIs</artifactId><version>2.0.2</version></dependency>
          <dependency><groupId>org.axsl.org.w3c.dom.svg</groupId><artifactId>svg-dom-java</artifactId><version>1.1</version></dependency>
          <dependency><groupId>xml-apis</groupId>    <artifactId>xml-apis</artifactId><version>2.0.0</version></dependency>
          <dependency><groupId>org.w3c.css</groupId> <artifactId>sac</artifactId>    <version>1.3</version></dependency>
          <!-- svg 生成png格式圖片結束  -->
          package com.yhb.web.util;
          import java.io.ByteArrayInputStream;
          import java.io.File;
          import java.io.FileOutputStream;
          import java.io.IOException;
          import java.io.InputStream;
          import java.io.OutputStream;
          import java.io.UnsupportedEncodingException;
          import java.net.URL;
          import org.apache.batik.transcoder.TranscoderException;
          import org.apache.batik.transcoder.TranscoderInput;
          import org.apache.batik.transcoder.TranscoderOutput;
          import org.apache.batik.transcoder.image.PNGTranscoder;
          public final class FileUtil {
          /** 
               *@Description: 將svg字符串轉換為png 
               *@Author: 
               *@param svgCode svg代碼 
               *@param pngFilePath  保存的路徑 
               *@throws IOException io異常 
               *@throws TranscoderException svg代碼異常 
              */  
          public static void convertToPng(String svgCode,String pngFilePath) throws IOException,TranscoderException{  
           
                  File file = new File (pngFilePath);  
            
                  FileOutputStream outputStream = null;  
                  try {  
                      file.createNewFile ();  
                      outputStream = new FileOutputStream (file);  
                      convertToPng (svgCode, outputStream);  
                  } finally {  
                      if (outputStream != null) {  
                          try {  
                              outputStream.close ();  
                          } catch (IOException e) {  
                              e.printStackTrace ();  
                          }  
                      }  
                  }  
              }  
          /** 
               *@Description: 將svgCode轉換成png文件,直接輸出到流中 
               *@param svgCode svg代碼 
               *@param outputStream 輸出流 
               *@throws TranscoderException 異常 
               *@throws IOException io異常 
               */  
              public static void convertToPng(String svgCode,OutputStream outputStream) throws TranscoderException,IOException{  
                  try {  
                  byte[] bytes = svgCode.getBytes ("UTF-8");  
                      PNGTranscoder t = new PNGTranscoder ();  
                      TranscoderInput input = new TranscoderInput (new ByteArrayInputStream (bytes));  
                      TranscoderOutput output = new TranscoderOutput (outputStream);  
                      t.transcode (input, output);  
                      outputStream.flush ();  
                  } finally {  
                      if (outputStream != null) {  
                          try {  
                              outputStream.close ();  
                          } catch (IOException e) {  
                              e.printStackTrace ();  
                          }  
                      }  
                  }  
              }  
          }


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 台江县| 岐山县| 特克斯县| 广元市| 武平县| 城口县| 高淳县| 息烽县| 贵南县| 射阳县| 南丰县| 辉县市| 京山县| 凤山县| 迭部县| 崇左市| 龙门县| 揭阳市| 平阳县| 南京市| 外汇| 呼和浩特市| 青浦区| 阿勒泰市| 潮安县| 杨浦区| 拉萨市| 凤冈县| 左云县| 三亚市| 北票市| 平顶山市| 洛宁县| 西林县| 彝良县| 泾源县| 汝州市| 尼木县| 萨嘎县| 延庆县| 治多县|