Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何加载和解析SVG文档 背景_Java_File_Svg_Batik - Fatal编程技术网

Java 如何加载和解析SVG文档 背景

Java 如何加载和解析SVG文档 背景,java,file,svg,batik,Java,File,Svg,Batik,与读取和解析SVG路径相关的问题还有很多: 问题 SVGpath元素包含一个(d)。有时需要从SVG文件中加载、解析和提取路径信息 问题: 如何从SVG文件中加载、解析和提取SVG路径信息?概述 使用(或)加载和解析SVG文件。该解决方案显示了将SVG文件转换为MetaPost的初步阶段的Java代码。这将为如何使用Java从SVG文件加载、解析和提取内容提供一个总体思路 图书馆 您将需要以下库: batik-anim.jar batik-awt-util.jar batik-brid

与读取和解析SVG路径相关的问题还有很多:

问题 SVG
path
元素包含一个(
d
)。有时需要从SVG文件中加载、解析和提取路径信息

问题: 如何从SVG文件中加载、解析和提取SVG路径信息?

概述 使用(或)加载和解析SVG文件。该解决方案显示了将SVG文件转换为MetaPost的初步阶段的Java代码。这将为如何使用Java从SVG文件加载、解析和提取内容提供一个总体思路

图书馆 您将需要以下库:

batik-anim.jar
batik-awt-util.jar
batik-bridge.jar
batik-css.jar
batik-dom.jar
batik-ext.jar
batik-gvt.jar
batik-parser.jar
batik-script.jar
batik-svg-dom.jar
batik-svggen.jar
batik-util.jar
batik-xml.jar
xml-apis-ext.jar
加载SVG文件 主应用程序将SVG文件加载到DOM中,然后将DOM转换为SVG DOM。
initSVGDOM()
方法调用非常重要。如果不调用
initSVGDOM()
,从DOM中提取SVG DOM元素的方法将不可用

import java.io.File;
import java.io.IOException;

import java.net.URI;

import org.apache.batik.bridge.BridgeContext;
import org.apache.batik.bridge.DocumentLoader;
import org.apache.batik.bridge.GVTBuilder;
import org.apache.batik.bridge.UserAgent;
import org.apache.batik.bridge.UserAgentAdapter;
import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.dom.svg.SVGOMSVGElement;
import org.apache.batik.util.XMLResourceDescriptor;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;


/**
 * Responsible for converting all SVG path elements into MetaPost curves.
 */
public class SVGMetaPost {
  private static final String PATH_ELEMENT_NAME = "path";
  
  private Document svgDocument;
  
  /**
   * Creates an SVG Document given a URI.
   *
   * @param uri Path to the file.
   * @throws Exception Something went wrong parsing the SVG file.
   */
  public SVGMetaPost( String uri ) throws IOException {
    setSVGDocument( createSVGDocument( uri ) );
  }

  /**
   * Finds all the path nodes and converts them to MetaPost code.
   */
  public void run() {
    NodeList pathNodes = getPathElements();
    int pathNodeCount = pathNodes.getLength();

    for( int iPathNode = 0; iPathNode < pathNodeCount; iPathNode++ ) {
      MetaPostPath mpp = new MetaPostPath( pathNodes.item( iPathNode ) );
      System.out.println( mpp.toCode() );
    }
  }
  
  /**
   * Returns a list of elements in the SVG document with names that
   * match PATH_ELEMENT_NAME.
   * 
   * @return The list of "path" elements in the SVG document.
   */
  private NodeList getPathElements() {
    return getSVGDocumentRoot().getElementsByTagName( PATH_ELEMENT_NAME );
  }
  
  /**
   * Returns an SVGOMSVGElement that is the document's root element.
   * 
   * @return The SVG document typecast into an SVGOMSVGElement.
   */
  private SVGOMSVGElement getSVGDocumentRoot() {
    return (SVGOMSVGElement)getSVGDocument().getDocumentElement();
  }

  /**
   * This will set the document to parse. This method also initializes
   * the SVG DOM enhancements, which are necessary to perform SVG and CSS
   * manipulations. The initialization is also required to extract information
   * from the SVG path elements.
   *
   * @param document The document that contains SVG content.
   */
  public void setSVGDocument( Document document ) {
    initSVGDOM( document );
    this.svgDocument = document;
  }

  /**
   * Returns the SVG document parsed upon instantiating this class.
   * 
   * @return A valid, parsed, non-null SVG document instance.
   */
  public Document getSVGDocument() {
    return this.svgDocument;
  }
  
  /**
   * Enhance the SVG DOM for the given document to provide CSS- and SVG-specific
   * DOM interfaces.
   * 
   * @param document The document to enhance.
   * @link http://wiki.apache.org/xmlgraphics-batik/BootSvgAndCssDom
   */
  private void initSVGDOM( Document document ) {
    UserAgent userAgent = new UserAgentAdapter();
    DocumentLoader loader = new DocumentLoader( userAgent );
    BridgeContext bridgeContext = new BridgeContext( userAgent, loader );
    bridgeContext.setDynamicState( BridgeContext.DYNAMIC );

    // Enable CSS- and SVG-specific enhancements.
    (new GVTBuilder()).build( bridgeContext, document );
  }

  /**
   * Use the SAXSVGDocumentFactory to parse the given URI into a DOM.
   * 
   * @param uri The path to the SVG file to read.
   * @return A Document instance that represents the SVG file.
   * @throws Exception The file could not be read.
   */
  private Document createSVGDocument( String uri ) throws IOException {
    String parser = XMLResourceDescriptor.getXMLParserClassName();
    SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory( parser );
    return factory.createDocument( uri );
  }

  /**
   * Reads a file and parses the path elements.
   * 
   * @param args args[0] - Filename to parse.
   * @throws IOException Error reading the SVG file.
   */
  public static void main( String args[] ) throws IOException {
    URI uri = new File( args[0] ).toURI();
    SVGMetaPost converter = new SVGMetaPost( uri.toString() );
    converter.run();
  }
}
建造 编译将因环境而异。类似于以下内容的脚本应该会有所帮助:

#!/bin/bash
mkdir -p ./build
javac -cp ./lib/* -d ./build ./source/*.java
确保将所有
.jar
文件放入
/lib
目录。将源文件放入
/source
目录

跑 创建脚本(或批处理文件)以执行程序:

#!/bin/bash
java -cp ./lib/*:./build SVGMetaPost $1
输出 当针对包含有效SVG路径的文件运行时,将生成:

$ ./run.sh stripe/trigon.svg 
% path8078-6
M 864.1712 779.3069
C 864.1712 779.3069 868.04065 815.6211 871.4032 833.4621
C 873.4048 844.08203 874.91724 855.0544 879.0846 864.82227
C 884.24023 876.9065 895.2377 887.9899 900.0184 897.3661
C 904.7991 906.7422 907.3466 918.3257 907.3466 918.3257
C 907.3466 918.3257 892.80817 887.6536 864.1712 887.3086
C 835.53424 886.9637 820.9958 918.3257 820.9958 918.3257
C 820.9958 918.3257 823.6176 906.59644 828.32404 897.3661
C 833.0304 888.1356 844.10223 876.9065 849.2578 864.82227
C 853.4252 855.05444 854.9376 844.08203 856.93915 833.4621
C 860.3017 815.6211 864.17114 779.3069 864.17114 779.3069
z
从这里可以清楚地看到如何将SVG路径数据读入相应的SVG对象

补遗 请注意,从SVG转换为的最简单方法是:

  • 将SVG转换为PDF(例如,使用或)
  • 使用将PDF转换为MetaPost

  • 我还发现MetaPost和MetaPostComment的依赖性错误,这些类来自哪里?它们不重要如果您在查找类文件时遇到问题,请查看或提出新问题。未列出
    MetaPostComment
    类。我开发它可能是为了我自己的目的,没有将它包括在本例中。在batik中没有
    org.apache.batik.dom.svg.SAXSVGDocumentFactory
    org.apache.batik.dom.svg.svggomsgelement
    。你用什么版本的蜡染?@josef-见和。这个班可能已经搬走了?此外,如果您正在进行Android开发,您可能需要使用。
    $ ./run.sh stripe/trigon.svg 
    % path8078-6
    M 864.1712 779.3069
    C 864.1712 779.3069 868.04065 815.6211 871.4032 833.4621
    C 873.4048 844.08203 874.91724 855.0544 879.0846 864.82227
    C 884.24023 876.9065 895.2377 887.9899 900.0184 897.3661
    C 904.7991 906.7422 907.3466 918.3257 907.3466 918.3257
    C 907.3466 918.3257 892.80817 887.6536 864.1712 887.3086
    C 835.53424 886.9637 820.9958 918.3257 820.9958 918.3257
    C 820.9958 918.3257 823.6176 906.59644 828.32404 897.3661
    C 833.0304 888.1356 844.10223 876.9065 849.2578 864.82227
    C 853.4252 855.05444 854.9376 844.08203 856.93915 833.4621
    C 860.3017 815.6211 864.17114 779.3069 864.17114 779.3069
    z