zoukankan      html  css  js  c++  java
  • java后台svg转成png

    1.替换img地址

    /** 
         * 替换img地址 
         * @param str --  前台svg字符串 
         * @return 
         */  
        private String transferImgPath(HttpServletRequest request,String str) {  
            System.out.println(str);  
            String requestURL = request.getRequestURL().toString();  
            String requestURI = request.getRequestURI();  
            String httpURL = requestURL.replace(requestURI,"");  
            String ptn = "(?i)href="([^"]*)"[^>]*>";  
            Pattern p = Pattern.compile(ptn, Pattern.DOTALL);  
            Matcher m = p.matcher(str);  
            List<String> list = new ArrayList<String>();  
            while(m.find()) {  
                String imgurl = m.group(1);  
                if(!imgurl.contains("http") && !list.contains(imgurl)) {  
                    str = str.replaceAll(imgurl,httpURL + "/resources/myflow-min/" + imgurl);  
                }  
                list.add(imgurl);  
            }  
      
            return str;  
        }  

    2.将svg字符串转换为png

    /** 
     * 将svg字符串转换为png 
     * 
     * @param svgCode svg代码 
     * @param pngFilePath 保存的路径 
     * @throws TranscoderException svg代码异常 
     * @throws IOException io错误 
     */  
    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();  
                }  
            }  
        }  
    }  
      
    /** 
     * 将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();  
                }  
            }  
        }  
    }  
    3.用的jar包

    batik-all-1.7.jar
    xml-apis-ext.jar

     
  • 相关阅读:
    VC获取系统时间、程序运行时间
    数学题
    最小费用流
    最大流模板
    计划
    算法竞赛入门经典 训练指南 之 图论(完全版持续更新)
    uva 11324 The Largest Clique 强连通分量求缩点构造DAG
    hdu 4288 Coder 一个很水的版本 >_<
    hoj 2939 Coin Question
    成都网络赛 1002 Control 1005 Food
  • 原文地址:https://www.cnblogs.com/oskyhg/p/7256267.html
Copyright © 2011-2022 走看看