|
|
@@ -0,0 +1,41 @@
|
|
|
+package com.nova.brain.glass.helper
|
|
|
+
|
|
|
+import android.graphics.Bitmap
|
|
|
+import android.graphics.BitmapFactory
|
|
|
+import kotlin.math.max
|
|
|
+
|
|
|
+object BitmapDecodeHelper {
|
|
|
+ fun decodeSampledBitmap(path: String, reqWidth: Int, reqHeight: Int): Bitmap? {
|
|
|
+ val boundsOptions = BitmapFactory.Options().apply {
|
|
|
+ inJustDecodeBounds = true
|
|
|
+ }
|
|
|
+ BitmapFactory.decodeFile(path, boundsOptions)
|
|
|
+
|
|
|
+ val decodeOptions = BitmapFactory.Options().apply {
|
|
|
+ inSampleSize = calculateInSampleSize(boundsOptions, reqWidth, reqHeight)
|
|
|
+ inPreferredConfig = Bitmap.Config.RGB_565
|
|
|
+ }
|
|
|
+ return BitmapFactory.decodeFile(path, decodeOptions)
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun calculateInSampleSize(
|
|
|
+ options: BitmapFactory.Options,
|
|
|
+ reqWidth: Int,
|
|
|
+ reqHeight: Int
|
|
|
+ ): Int {
|
|
|
+ val height = options.outHeight
|
|
|
+ val width = options.outWidth
|
|
|
+ if (height <= 0 || width <= 0 || reqWidth <= 0 || reqHeight <= 0) {
|
|
|
+ return 1
|
|
|
+ }
|
|
|
+ var inSampleSize = 1
|
|
|
+ if (height > reqHeight || width > reqWidth) {
|
|
|
+ val halfHeight = height / 2
|
|
|
+ val halfWidth = width / 2
|
|
|
+ while (halfHeight / inSampleSize >= reqHeight && halfWidth / inSampleSize >= reqWidth) {
|
|
|
+ inSampleSize *= 2
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return max(1, inSampleSize)
|
|
|
+ }
|
|
|
+}
|