使用 Apache Batik 将 SVG 图像转换为 JPEG 图像

2024-05-05

我正在尝试将 SVG 图像转换为 JPEG,如下所示https://xmlgraphics.apache.org/batik/using/transcoder.html#createImage https://xmlgraphics.apache.org/batik/using/transcoder.html#createImage例子。这是代码:

public void saveAsjpeg() throws Exception {

    // Create a JPEG transcoder
    JPEGTranscoder t = new JPEGTranscoder();

    // Set the transcoding hints.
    t.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, new Float(.8));

    // Create the transcoder input.
    String svgURI = new File(inputFilePath).toURL().toString();
    TranscoderInput input = new TranscoderInput(svgURI);

    // Create the transcoder output.
    OutputStream ostream = new FileOutputStream(outputFilePath);
    TranscoderOutput output = new TranscoderOutput(ostream);

    // Save the image.
    t.transcode(input, output);

    // Flush and close the stream.
    ostream.flush();
    ostream.close();
    System.exit(0);
}

下面是我的 pom.xml。我正在春季启动项目中尝试:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.4.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.xmlgraphics</groupId>
            <artifactId>batik-transcoder</artifactId>
            <version>1.8</version>
        </dependency>

        <dependency>
            <groupId>org.apache.xmlgraphics</groupId>
            <artifactId>batik-codec</artifactId>
            <version>1.8</version>
        </dependency>

        <dependency>
            <groupId>org.apache.xmlgraphics</groupId>
            <artifactId>batik-svgpp</artifactId>
            <version>1.8</version>
        </dependency>

        <dependency>
            <groupId>org.apache.xmlgraphics</groupId>
            <artifactId>batik-rasterizer</artifactId>
            <version>1.8</version>
        </dependency>

        <dependency>
            <groupId>org.apache.xmlgraphics</groupId>
            <artifactId>batik-squiggle</artifactId>
            <version>1.8</version>
        </dependency>

        <dependency>
            <groupId>org.apache.xmlgraphics</groupId>
            <artifactId>xmlgraphics-commons</artifactId>
            <version>2.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.xmlgraphics</groupId>
            <artifactId>batik-ttf2svg</artifactId>
            <version>1.8</version>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <executions>
                    <execution>
                        <id>attach-javadocs</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>


</project>

我收到以下异常:

org.apache.batik.transcoder.TranscoderException: null
Enclosed Exception:
null
    at org.apache.batik.transcoder.image.ImageTranscoder.transcode(Unknown Source)
    at org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(Unknown Source)
    at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(Unknown Source)
    at SaveToJPEG.saveAsjpeg(SaveToJPEG.java:31)
    at SaveToJPEG.main(SaveToJPEG.java:42)

我这里有几个问题:

  1. 为什么异常堆栈跟踪显示“未知来源”并且异常信息如此之少?我用谷歌搜索了这个并读到如果罐子没有附加源,则异常可能无法提供信息。我已将插件代码放入 pom 中以添加源代码。但这是行不通的。
  2. 蜡染代码中没有将 svg 图像转换为 jpeg 的错误是什么?

我不知道是谁发布了这个神器,但它为我解决了这个问题

<dependency>
    <groupId>fr.avianey.apache-xmlgraphics</groupId>
    <artifactId>batik</artifactId>
    <version>1.8</version>
</dependency>

这些类包含在此工件中

  • org.apache.batik.ext.awt.image.codec.imageio.ImageIOPNGImageWriter
  • org.apache.batik.ext.awt.image.codec.imageio.ImageIOTIFFImageWriter
  • org.apache.batik.ext.awt.image.codec.imageio.ImageIOJPEGImageWriter
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Apache Batik 将 SVG 图像转换为 JPEG 图像 的相关文章

随机推荐

  • 即使引用了 Typescript 也找不到名称

    我有一个用打字稿编写的有角度的项目 这在 VS 下对我来说效果很好 现在我在 webstorm 下尝试使用 Node JS 进行同样的操作 我在progressor ts 文件中有一个progressor 类 export class Pr
  • Restful服务参数不匹配异常

    我有服务 POST Path post Consumes application json public Response createProductInJSON Product product String result Product
  • 获取 BLOB 的二进制内容

    我知道 为了将 BLOB 对象转换为 Javascript 中的可读格式 URL 我应该使用 createObjectURL 方法 对吧 例子 var blob new Blob Example type text plain url wi
  • TestCafe - 浏览器在测试之间总是以干净的状态启动。如何覆盖它以便浏览器记住缓存、用户设置和存储

    测试之间的浏览器始终以干净的状态打开 登录在我的应用程序中被记住 因为身份验证仍然存在 但由于浏览器始终以干净的状态打开 我必须在所有夹具的 Before 挂钩中执行登录 有什么方法可以打开浏览器以便记住用户设置 缓存 本地和会话存储吗 T
  • 从 pandas 数据帧创建 BigQuery 表,无需显式指定架构

    我有一个 pandas 数据框 想从中创建一个 BigQuery 表 我知道有很多帖子询问这个问题 但到目前为止我能找到的所有答案都需要明确指定每列的架构 例如 from google cloud import bigquery as bq
  • Prisma 1 到 2 迁移问题:P4001 内省数据库为空

    因此 我一直在尝试在现有数据库上运行内省过程 如下所示 npx prisma introspect 并收到以下错误跟踪 0 info it worked if it ends with ok 1 verbose cli C Program
  • 无法在 Android MediaRecorder 中设置手动视频大小

    我正在Android中使用MediaRecorder来录制视频 我当前的参数是 mMediaRecorder setAudioSource MediaRecorder AudioSource DEFAULT mMediaRecorder s
  • 在 unnest_wider 之后命名提升向量中的列

    在使用 tidyr 1 0 的一些新功能时 我遇到了一些令人头疼的问题 我用过boxplot stats获取我想用来绘制的箱线图值向量 我已经成功地完成了此操作 但我相信有更好的方法来命名新的未嵌套向量的列 这是当前设置 library t
  • 重复符号_OBJC_CLASS_$_LoginController

    我知道这个问题以前已经被问过很多次了 但到目前为止还没有解决我的问题 我知道当您在项目中获得文件的多个副本时 会发生此错误 我尝试清理构建 删除任何登录控制器 m文件输入编译源然后构建 很好 没有错误 当我在编译源中添加 LoginCont
  • 控制台界面教程和提示 (pdcurses)

    我正在寻找有关使用 PDCurses 库的教程 不幸的是 只有文本文档 这更像是函数参考 pdcurses 是否与 ncurses 足够相似以使用 ncurses 教程 关于制作控制台 UI 的任何提示 附言 PDCurses mingw3
  • Vagrant - Homestead 设置多个站点

    我已经使用 homestead yaml 映射了文件夹等 ip 192 168 10 10 folders map Users User Desktop folder Homestead First to home vagrant Firs
  • 使用EF Core调用存储过程并关闭连接

    我有一个使用 EF Core 的 ASP NET Core 2 2 应用程序 我有一个服务类 通常使用DbContext用于任何 CRUD 操作 然而 在其中一种方法中 Assign下面的方法 我需要使用存储过程 所以我使用以下代码 注意D
  • 在重复键上仅更新 Null 或空值

    我有一个 mysql 查询来合并主键 IMO 上的两个表 查询工作正常 但我遇到的问题是在重复键更新时 我只想更新 wp second 表的那些没有值的字段 简而言之 在重复键上 wp second 值仅应在 null 或空时更新 这是我到
  • polyfit numpy 的反向输出

    我使用了 numpy 的 polyfit 并获得了两个数组 x 和 y 的非常好的拟合 使用七阶多项式 我的关系是这样的 y x p 0 x 7 p 1 x 6 p 2 x 5 p 3 x 4 p 4 x 3 p 5 x 2 p 6 x 1
  • UCM 中的复合基线是什么以及何时使用它?

    UCM 中的综合基线是什么 什么时候会用到 主要是当我们有多个组件的时候才会使用它吗 关于 复合基线 的参考文献是 在 UCM 中使用复合基线的最佳实践 http www ibm com developerworks rational li
  • 在 pandas dataframe python 列中搜索单词

    我有两个文本列 我想查找一列中的单词是否存在于另一列中 我编写了下面的代码 它运行得很好 但它会检测字符串中的任何位置是否存在单词 例如 它将在 ham 中查找 ha 我想改用正则表达式 但我被困住了 我遇到了这个post https st
  • bash 用变量值替换字符串中的变量名

    这有点奇怪 我有以下字符串 我有一个名为 REDIRECT 的变量设置为 https working MYDOMAIN blah blah 我需要将 MYDOMAIN 替换为分配给 MYDOMAIN 的变量的实际值 不确定 bash 还是
  • Java机器学习库可以商用吗? [关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 有谁知道我可以将其用于商业产品的优秀 Java 机器学习库吗 不幸的是 Weka 和 Rapidmin
  • 验证远程图像实际上是 ruby​​ 中的图像文件?

    我试图弄清楚如何验证我输入载波的内容实际上是图像 我获取图像网址的来源并没有返回所有实时网址 有些图像已不复存在 不幸的是 它并没有真正返回正确的状态代码或任何内容 因为我正在使用一些代码来检查远程文件是否存在并且它通过了该检查 因此 现在
  • 使用 Apache Batik 将 SVG 图像转换为 JPEG 图像

    我正在尝试将 SVG 图像转换为 JPEG 如下所示https xmlgraphics apache org batik using transcoder html createImage https xmlgraphics apache