• 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

     
  • 相关阅读:
    设计模式之里氏替换原则
    设计模式之依赖倒置原则讲解
    条款10 若不想使用编译器自动生成的函数,就该明确拒绝
    Django---常用字段和参数
    Python中abc
    Python中鸭子类型
    Python多继承的正确打开方式:mixins机制
    python新式类和经典类的区别
    Django---drf权限、频率、过滤、排序、异常处理
    删库跑路技巧 删库跑路命令
  • 原文地址:https://www.cnblogs.com/oskyhg/p/7256267.html
Copyright © 2020-2023  润新知