Last active
August 8, 2020 16:31
-
-
Save mandrachek/566bd773e7cda8f4dab09bd919692390 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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