|
|
@ -27,7 +27,7 @@ object BitmapUtils { |
|
|
|
* @return |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@Throws(IOException::class) |
|
|
|
@Throws(IOException::class) |
|
|
|
fun decodeBitmap(path: String, width: Int, height: Int): Bitmap { |
|
|
|
fun decodeBitmap(path: String, width: Int, height: Int? = null): Bitmap { |
|
|
|
|
|
|
|
|
|
|
|
val fis = FileInputStream(path) |
|
|
|
val fis = FileInputStream(path) |
|
|
|
return fis.use { |
|
|
|
return fis.use { |
|
|
@ -35,44 +35,35 @@ object BitmapUtils { |
|
|
|
// inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight; |
|
|
|
// inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight; |
|
|
|
op.inJustDecodeBounds = true |
|
|
|
op.inJustDecodeBounds = true |
|
|
|
BitmapFactory.decodeFileDescriptor(fis.fd, null, op) |
|
|
|
BitmapFactory.decodeFileDescriptor(fis.fd, null, op) |
|
|
|
//获取比例大小 |
|
|
|
op.inSampleSize = calculateInSampleSize(op, width, height) |
|
|
|
val wRatio = ceil((op.outWidth / width).toDouble()).toInt() |
|
|
|
|
|
|
|
val hRatio = ceil((op.outHeight / height).toDouble()).toInt() |
|
|
|
|
|
|
|
//如果超出指定大小,则缩小相应的比例 |
|
|
|
|
|
|
|
if (wRatio > 1 && hRatio > 1) { |
|
|
|
|
|
|
|
if (wRatio > hRatio) { |
|
|
|
|
|
|
|
op.inSampleSize = wRatio |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
op.inSampleSize = hRatio |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
op.inJustDecodeBounds = false |
|
|
|
op.inJustDecodeBounds = false |
|
|
|
BitmapFactory.decodeFileDescriptor(fis.fd, null, op) |
|
|
|
BitmapFactory.decodeFileDescriptor(fis.fd, null, op) |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Throws(IOException::class) |
|
|
|
/** |
|
|
|
fun decodeBitmap(path: String, width: Int): Bitmap { |
|
|
|
*计算 InSampleSize。缺省返回1 |
|
|
|
|
|
|
|
* @param options BitmapFactory.Options, |
|
|
|
val fis = FileInputStream(path) |
|
|
|
* @param width 想要显示的图片的宽度 |
|
|
|
|
|
|
|
* @param height 想要显示的图片的高度 |
|
|
|
return fis.use { |
|
|
|
* @return |
|
|
|
val op = BitmapFactory.Options() |
|
|
|
*/ |
|
|
|
// inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight; |
|
|
|
private fun calculateInSampleSize( |
|
|
|
op.inJustDecodeBounds = true |
|
|
|
options: BitmapFactory.Options, |
|
|
|
|
|
|
|
width: Int? = null, |
|
|
|
BitmapFactory.decodeFileDescriptor(fis.fd, null, op) |
|
|
|
height: Int? = null |
|
|
|
//获取比例大小 |
|
|
|
): Int { |
|
|
|
val wRatio = ceil((op.outWidth / width).toDouble()).toInt() |
|
|
|
//获取比例大小 |
|
|
|
//如果超出指定大小,则缩小相应的比例 |
|
|
|
val wRatio = width?.let { ceil((options.outWidth / it).toDouble()).toInt() } ?: -1 |
|
|
|
if (wRatio > 1) { |
|
|
|
val hRatio = height?.let { ceil((options.outHeight / it).toDouble()).toInt() } ?: -1 |
|
|
|
op.inSampleSize = wRatio |
|
|
|
//如果超出指定大小,则缩小相应的比例 |
|
|
|
} |
|
|
|
return when { |
|
|
|
op.inJustDecodeBounds = false |
|
|
|
wRatio > 1 && hRatio > 1 -> max(wRatio, hRatio) |
|
|
|
BitmapFactory.decodeFileDescriptor(fis.fd, null, op) |
|
|
|
wRatio > 1 -> wRatio |
|
|
|
|
|
|
|
hRatio > 1 -> hRatio |
|
|
|
|
|
|
|
else -> 1 |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** 从path中获取Bitmap图片 |
|
|
|
/** 从path中获取Bitmap图片 |
|
|
@ -118,17 +109,7 @@ object BitmapUtils { |
|
|
|
// inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight; |
|
|
|
// inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight; |
|
|
|
op.inJustDecodeBounds = true |
|
|
|
op.inJustDecodeBounds = true |
|
|
|
BitmapFactory.decodeResource(context.resources, resId, op) //获取尺寸信息 |
|
|
|
BitmapFactory.decodeResource(context.resources, resId, op) //获取尺寸信息 |
|
|
|
//获取比例大小 |
|
|
|
op.inSampleSize = calculateInSampleSize(op, width, height) |
|
|
|
val wRatio = ceil((op.outWidth / width).toDouble()).toInt() |
|
|
|
|
|
|
|
val hRatio = ceil((op.outHeight / height).toDouble()).toInt() |
|
|
|
|
|
|
|
//如果超出指定大小,则缩小相应的比例 |
|
|
|
|
|
|
|
if (wRatio > 1 && hRatio > 1) { |
|
|
|
|
|
|
|
if (wRatio > hRatio) { |
|
|
|
|
|
|
|
op.inSampleSize = wRatio |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
op.inSampleSize = hRatio |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
op.inJustDecodeBounds = false |
|
|
|
op.inJustDecodeBounds = false |
|
|
|
return BitmapFactory.decodeResource(context.resources, resId, op) |
|
|
|
return BitmapFactory.decodeResource(context.resources, resId, op) |
|
|
|
} |
|
|
|
} |
|
|
@ -154,17 +135,7 @@ object BitmapUtils { |
|
|
|
// inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight; |
|
|
|
// inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight; |
|
|
|
op.inJustDecodeBounds = true |
|
|
|
op.inJustDecodeBounds = true |
|
|
|
BitmapFactory.decodeStream(inputStream, null, op) //获取尺寸信息 |
|
|
|
BitmapFactory.decodeStream(inputStream, null, op) //获取尺寸信息 |
|
|
|
//获取比例大小 |
|
|
|
op.inSampleSize = calculateInSampleSize(op, width, height) |
|
|
|
val wRatio = ceil((op.outWidth / width).toDouble()).toInt() |
|
|
|
|
|
|
|
val hRatio = ceil((op.outHeight / height).toDouble()).toInt() |
|
|
|
|
|
|
|
//如果超出指定大小,则缩小相应的比例 |
|
|
|
|
|
|
|
if (wRatio > 1 && hRatio > 1) { |
|
|
|
|
|
|
|
if (wRatio > hRatio) { |
|
|
|
|
|
|
|
op.inSampleSize = wRatio |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
op.inSampleSize = hRatio |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
inputStream = context.assets.open(fileNameInAssets) |
|
|
|
inputStream = context.assets.open(fileNameInAssets) |
|
|
|
op.inJustDecodeBounds = false |
|
|
|
op.inJustDecodeBounds = false |
|
|
|
BitmapFactory.decodeStream(inputStream, null, op) |
|
|
|
BitmapFactory.decodeStream(inputStream, null, op) |
|
|
|