java處理word公式(wmf格式轉換成svg)(svg格式轉換成png)
阿新 • • 發佈:2018-11-08
wmf格式轉換成svg
把wmf格式轉成svg格式,這樣就能在瀏覽器中顯示了
需要第三方jar包:wmf2svg-0.9.6.jar
//wmf格式的圖片轉換成svg格式 private void convert(String file,String dest) throws Exception{ InputStream in = new FileInputStream(file); WmfParser parser = new WmfParser(); final SvgGdi gdi = new SvgGdi(false); parser.parse(in, gdi); Document doc = gdi.getDocument(); OutputStream out = new FileOutputStream(dest); if (dest.endsWith(".svgz")) { out = new GZIPOutputStream(out); } output(doc, out); } private void output(Document doc, OutputStream out) throws Exception { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,"-//W3C//DTD SVG 1.0//EN"); transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"); transformer.transform(new DOMSource(doc), new StreamResult(out)); ByteArrayOutputStream bos = new ByteArrayOutputStream(); transformer.transform(new DOMSource(doc), new StreamResult(bos)); out.flush(); out.close(); }
svg格式轉換成png
匯入maven依賴
<dependency> <groupId>com.kenai.nbpwr</groupId> <artifactId>org-apache-batik-all</artifactId> <version>1.7-201003011305</version> </dependency> <!-- https://mvnrepository.com/artifact/xml-apis/xml-apis-ext --> <dependency> <groupId>xml-apis</groupId> <artifactId>xml-apis-ext</artifactId> <version>1.3.04</version> </dependency> <dependency> <groupId>org.apache.xmlgraphics</groupId> <artifactId>batik-codec</artifactId> <version>1.7</version> </dependency>
public class Test { /** * 將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(); } } } } // 讀取svg public static String readToString(String fileName) { String encoding = "UTF-8"; File file = new File(fileName); Long filelength = file.length(); byte[] filecontent = new byte[filelength.intValue()]; try { FileInputStream in = new FileInputStream(file); in.read(filecontent); in.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { return new String(filecontent, encoding); } catch (UnsupportedEncodingException e) { System.err.println("The OS does not support " + encoding); e.printStackTrace(); return null; } } public static void main(String[] args) { String svgCode = readToString("C:\\Users\\Administrator\\Desktop\\1.svg"); try { convertToPng(svgCode,"D:/1/1.png"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TranscoderException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }