Skip to content

Instantly share code, notes, and snippets.

@tiiime
Created May 24, 2018 05:40
Show Gist options
  • Save tiiime/8673ddd97320cf18a468140add036641 to your computer and use it in GitHub Desktop.
Save tiiime/8673ddd97320cf18a468140add036641 to your computer and use it in GitHub Desktop.
微信分享图片压缩
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