|
|
|
@ -9,6 +9,7 @@ import android.graphics.BitmapFactory |
|
|
|
|
import android.graphics.Color |
|
|
|
|
import android.graphics.Matrix |
|
|
|
|
import com.google.android.renderscript.Toolkit |
|
|
|
|
import java.io.FileInputStream |
|
|
|
|
import java.io.IOException |
|
|
|
|
import kotlin.math.* |
|
|
|
|
|
|
|
|
@ -27,10 +28,13 @@ object BitmapUtils { |
|
|
|
|
*/ |
|
|
|
|
@Throws(IOException::class) |
|
|
|
|
fun decodeBitmap(path: String, width: Int, height: Int): Bitmap { |
|
|
|
|
|
|
|
|
|
val fis = FileInputStream(path) |
|
|
|
|
return fis.use { |
|
|
|
|
val op = BitmapFactory.Options() |
|
|
|
|
// inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight; |
|
|
|
|
op.inJustDecodeBounds = true |
|
|
|
|
BitmapFactory.decodeFile(path, op) |
|
|
|
|
BitmapFactory.decodeFileDescriptor(fis.fd, null, op) |
|
|
|
|
//获取比例大小 |
|
|
|
|
val wRatio = ceil((op.outWidth / width).toDouble()).toInt() |
|
|
|
|
val hRatio = ceil((op.outHeight / height).toDouble()).toInt() |
|
|
|
@ -43,15 +47,22 @@ object BitmapUtils { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
op.inJustDecodeBounds = false |
|
|
|
|
return BitmapFactory.decodeFile(path, op) |
|
|
|
|
BitmapFactory.decodeFileDescriptor(fis.fd, null, op) |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Throws(IOException::class) |
|
|
|
|
fun decodeBitmap(path: String, width: Int): Bitmap { |
|
|
|
|
|
|
|
|
|
val fis = FileInputStream(path) |
|
|
|
|
|
|
|
|
|
return fis.use { |
|
|
|
|
val op = BitmapFactory.Options() |
|
|
|
|
// inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight; |
|
|
|
|
op.inJustDecodeBounds = true |
|
|
|
|
BitmapFactory.decodeFile(path, op) |
|
|
|
|
|
|
|
|
|
BitmapFactory.decodeFileDescriptor(fis.fd, null, op) |
|
|
|
|
//获取比例大小 |
|
|
|
|
val wRatio = ceil((op.outWidth / width).toDouble()).toInt() |
|
|
|
|
//如果超出指定大小,则缩小相应的比例 |
|
|
|
@ -59,7 +70,9 @@ object BitmapUtils { |
|
|
|
|
op.inSampleSize = wRatio |
|
|
|
|
} |
|
|
|
|
op.inJustDecodeBounds = false |
|
|
|
|
return BitmapFactory.decodeFile(path, op) |
|
|
|
|
BitmapFactory.decodeFileDescriptor(fis.fd, null, op) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** 从path中获取Bitmap图片 |
|
|
|
@ -68,12 +81,17 @@ object BitmapUtils { |
|
|
|
|
*/ |
|
|
|
|
@Throws(IOException::class) |
|
|
|
|
fun decodeBitmap(path: String): Bitmap { |
|
|
|
|
val fis = FileInputStream(path) |
|
|
|
|
return fis.use { |
|
|
|
|
val opts = BitmapFactory.Options() |
|
|
|
|
opts.inJustDecodeBounds = true |
|
|
|
|
BitmapFactory.decodeFile(path, opts) |
|
|
|
|
|
|
|
|
|
BitmapFactory.decodeFileDescriptor(fis.fd, null, opts) |
|
|
|
|
opts.inSampleSize = computeSampleSize(opts, -1, 128 * 128) |
|
|
|
|
opts.inJustDecodeBounds = false |
|
|
|
|
return BitmapFactory.decodeFile(path, opts) |
|
|
|
|
BitmapFactory.decodeFileDescriptor(fis.fd, null, opts) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|