Java background SVG turned into PNG

Source: Internet
Author: User

1. Replace the IMG address

/**
	 * Replace img address
	 * @param str-  foreground SVG string
	 * @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. Convert an SVG string to png

    /** * Converts SVG strings to PNG * * @param svgcode SVG code * @param pngfilepath saved path * @throws Transcodere Xception SVG Code Exception * @throws ioexception IO error/public static void Converttopng (String svgcode, String Pngfil

        Epath) 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 (); /** * Converts svgcode to PNG file, output directly to stream * * @param svgcode SVG code * @param OutputStream output Stream * @throws transcoderexception exception * @throws IOException IO Exception * */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 Bag for use

Batik-all-1.7.jar
Xml-apis-ext.jar

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.