这是因为SVG文件的宽度和高度信息保存在“viewBox”属性中,而不是像其他图像格式一样保存在标准元数据中。因此需要使用Tika的AutoDetectParser来处理SVG文件,并使用JSoup来解析SVG文件的“viewBox”属性。
以下是Java代码示例:
File file = new File("example.svg");
AutoDetectParser parser = new AutoDetectParser();
BodyContentHandler handler = new BodyContentHandler();
Metadata metadata = new Metadata();
try (InputStream stream = new FileInputStream(file)) {
parser.parse(stream, handler, metadata);
// Extract width and height using JSoup
String svgContent = handler.toString();
Document doc = Jsoup.parse(svgContent);
Elements svgElements = doc.select("svg");
String viewBox = svgElements.attr("viewBox");
String[] viewBoxValues = viewBox.split("\\s+");
int width = Integer.parseInt(viewBoxValues[2]);
int height = Integer.parseInt(viewBoxValues[3]);
System.out.println("Width: " + width);
System.out.println("Height: " + height);
}