Skip to content

Instantly share code, notes, and snippets.

@mandrachek
Last active August 8, 2020 16:31
Show Gist options
  • Save mandrachek/566bd773e7cda8f4dab09bd919692390 to your computer and use it in GitHub Desktop.
Save mandrachek/566bd773e7cda8f4dab09bd919692390 to your computer and use it in GitHub Desktop.
package com.android.ide.common.vectordrawable
import java.awt.image.BufferedImage
import java.io.File
import java.io.InputStream
import javax.imageio.ImageIO
/**
* Converts a VectorDrawable to a BufferedImage.
* Please note that this has to live inside the com.android.ide.common.vectordrawable package so that
* it can use the internal VdParser class. This is available in the com.android.tools:sdk-common package, which is
* a dependency of apkanalyzer
* @param inputStream input stream for reading the VectorDrawable
*/
fun vdToBufferedImage(inputStream: InputStream): BufferedImage {
val errorLog = StringBuilder()
val tree = VdParser.parse(inputStream, errorLog)
// println(errorLog.toString())
/*
* TODO: The baseWidth and baseHeight are are used for the image width and height. This is the MDPI size.
* Consider allowing passing of density and scaling the image size to match.
*/
val bufferedImage = BufferedImage(tree.baseWidth.toInt(),tree.baseHeight.toInt(),BufferedImage.TYPE_INT_ARGB)
tree.drawIntoImage(bufferedImage)
return bufferedImage
}
/**
* Write a vector image to a png.
* @param inputStream input stream for reading the VectorDrawable
* @param path path to the file to write to
*/
fun vdToPng(inputStream: InputStream, path: String) {
ImageIO.write(vdToBufferedImage(inputStream),"PNG", File(path))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment