使用 batik 库,将 SVG 转化为 PNG
作者:互联网
依赖
implementation("org.apache.xmlgraphics:fop:2.6")
// https://mvnrepository.com/artifact/org.apache.xmlgraphics/batik-codec
implementation("org.apache.xmlgraphics:batik-codec:1.14")
代码
@JvmStatic
fun main(args: Array<String>) {
val svg = this::class.java.getResource("/vscode.svg")!!
.toURI()
.toString()
.toInput()
val output = TranscoderOutput(ByteArrayOutputStream())
svg.transcode(output)
FileOutputStream("1.png").write((output.outputStream as ByteArrayOutputStream).toByteArray())
}
private fun TranscoderInput.transcode(output: TranscoderOutput) {
PNGTranscoder().transcode(this, output)
}
private fun String.toInput(): TranscoderInput {
val doc = SAXSVGDocumentFactory(
XMLResourceDescriptor
.getXMLParserClassName()
).createDocument(this)
return TranscoderInput(doc)
}
运行结果
运行后会在当前目录下生成一个 1.png
的图片
标签:TranscoderInput,val,xmlgraphics,SVG,batik,transcode,apache,output,PNG 来源: https://www.cnblogs.com/Hakurei-Reimu-Zh/p/15735826.html