Created
May 24, 2018 05:40
-
-
Save tiiime/8673ddd97320cf18a468140add036641 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
private const val LIMIT_SIZE = 120 * 1024 | |
private fun getBitmapForWeChat(bitmap: Bitmap): Bitmap = bitmap.takeIf { | |
bitmap.wechatSize() < LIMIT_SIZE | |
} ?: bitmap.scale(LIMIT_SIZE.toFloat() / bitmap.wechatSize()) | |
private fun Bitmap.scale(scale: Float) = Bitmap.createScaledBitmap(this, (width * scale).toInt(), (height * scale).toInt(), true) | |
private fun Bitmap.wechatSize() = this.width * this.height | |
private fun Bitmap.toByteArray() = ByteArrayOutputStream().apply { | |
this@toByteArray.compress(Bitmap.CompressFormat.PNG, 100, this) | |
}.toByteArray() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment