|
|
@ -3,10 +3,13 @@ |
|
|
|
package io.legado.app.utils |
|
|
|
package io.legado.app.utils |
|
|
|
|
|
|
|
|
|
|
|
import android.content.Context |
|
|
|
import android.content.Context |
|
|
|
import android.graphics.* |
|
|
|
import android.graphics.Bitmap |
|
|
|
import android.graphics.Bitmap.Config |
|
|
|
import android.graphics.Bitmap.Config |
|
|
|
import android.view.View |
|
|
|
import android.graphics.BitmapFactory |
|
|
|
|
|
|
|
import android.graphics.Color |
|
|
|
|
|
|
|
import android.graphics.Matrix |
|
|
|
import com.google.android.renderscript.Toolkit |
|
|
|
import com.google.android.renderscript.Toolkit |
|
|
|
|
|
|
|
import java.io.FileInputStream |
|
|
|
import java.io.IOException |
|
|
|
import java.io.IOException |
|
|
|
import kotlin.math.* |
|
|
|
import kotlin.math.* |
|
|
|
|
|
|
|
|
|
|
@ -25,23 +28,51 @@ object BitmapUtils { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@Throws(IOException::class) |
|
|
|
@Throws(IOException::class) |
|
|
|
fun decodeBitmap(path: String, width: Int, height: Int): Bitmap { |
|
|
|
fun decodeBitmap(path: String, width: Int, height: Int): Bitmap { |
|
|
|
val op = BitmapFactory.Options() |
|
|
|
|
|
|
|
// inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight; |
|
|
|
val fis = FileInputStream(path) |
|
|
|
op.inJustDecodeBounds = true |
|
|
|
return fis.use { |
|
|
|
BitmapFactory.decodeFile(path, op) |
|
|
|
val op = BitmapFactory.Options() |
|
|
|
//获取比例大小 |
|
|
|
// inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight; |
|
|
|
val wRatio = ceil((op.outWidth / width).toDouble()).toInt() |
|
|
|
op.inJustDecodeBounds = true |
|
|
|
val hRatio = ceil((op.outHeight / height).toDouble()).toInt() |
|
|
|
BitmapFactory.decodeFileDescriptor(fis.fd, null, op) |
|
|
|
//如果超出指定大小,则缩小相应的比例 |
|
|
|
//获取比例大小 |
|
|
|
if (wRatio > 1 && hRatio > 1) { |
|
|
|
val wRatio = ceil((op.outWidth / width).toDouble()).toInt() |
|
|
|
if (wRatio > hRatio) { |
|
|
|
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 |
|
|
|
|
|
|
|
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.decodeFileDescriptor(fis.fd, null, op) |
|
|
|
|
|
|
|
//获取比例大小 |
|
|
|
|
|
|
|
val wRatio = ceil((op.outWidth / width).toDouble()).toInt() |
|
|
|
|
|
|
|
//如果超出指定大小,则缩小相应的比例 |
|
|
|
|
|
|
|
if (wRatio > 1) { |
|
|
|
op.inSampleSize = wRatio |
|
|
|
op.inSampleSize = wRatio |
|
|
|
} else { |
|
|
|
|
|
|
|
op.inSampleSize = hRatio |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
op.inJustDecodeBounds = false |
|
|
|
|
|
|
|
BitmapFactory.decodeFileDescriptor(fis.fd, null, op) |
|
|
|
} |
|
|
|
} |
|
|
|
op.inJustDecodeBounds = false |
|
|
|
|
|
|
|
return BitmapFactory.decodeFile(path, op) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** 从path中获取Bitmap图片 |
|
|
|
/** 从path中获取Bitmap图片 |
|
|
@ -50,12 +81,17 @@ object BitmapUtils { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@Throws(IOException::class) |
|
|
|
@Throws(IOException::class) |
|
|
|
fun decodeBitmap(path: String): Bitmap { |
|
|
|
fun decodeBitmap(path: String): Bitmap { |
|
|
|
val opts = BitmapFactory.Options() |
|
|
|
val fis = FileInputStream(path) |
|
|
|
opts.inJustDecodeBounds = true |
|
|
|
return fis.use { |
|
|
|
BitmapFactory.decodeFile(path, opts) |
|
|
|
val opts = BitmapFactory.Options() |
|
|
|
opts.inSampleSize = computeSampleSize(opts, -1, 128 * 128) |
|
|
|
opts.inJustDecodeBounds = true |
|
|
|
opts.inJustDecodeBounds = false |
|
|
|
|
|
|
|
return BitmapFactory.decodeFile(path, opts) |
|
|
|
BitmapFactory.decodeFileDescriptor(fis.fd, null, opts) |
|
|
|
|
|
|
|
opts.inSampleSize = computeSampleSize(opts, -1, 128 * 128) |
|
|
|
|
|
|
|
opts.inJustDecodeBounds = false |
|
|
|
|
|
|
|
BitmapFactory.decodeFileDescriptor(fis.fd, null, opts) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -67,9 +103,7 @@ object BitmapUtils { |
|
|
|
fun decodeBitmap(context: Context, resId: Int): Bitmap? { |
|
|
|
fun decodeBitmap(context: Context, resId: Int): Bitmap? { |
|
|
|
val opt = BitmapFactory.Options() |
|
|
|
val opt = BitmapFactory.Options() |
|
|
|
opt.inPreferredConfig = Config.RGB_565 |
|
|
|
opt.inPreferredConfig = Config.RGB_565 |
|
|
|
//获取资源图片 |
|
|
|
return BitmapFactory.decodeResource(context.resources, resId, opt) |
|
|
|
val `is` = context.resources.openRawResource(resId) |
|
|
|
|
|
|
|
return BitmapFactory.decodeStream(`is`, null, opt) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -80,13 +114,10 @@ object BitmapUtils { |
|
|
|
* @return |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
fun decodeBitmap(context: Context, resId: Int, width: Int, height: Int): Bitmap? { |
|
|
|
fun decodeBitmap(context: Context, resId: Int, width: Int, height: Int): Bitmap? { |
|
|
|
|
|
|
|
|
|
|
|
var inputStream = context.resources.openRawResource(resId) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val op = BitmapFactory.Options() |
|
|
|
val op = BitmapFactory.Options() |
|
|
|
// inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight; |
|
|
|
// inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight; |
|
|
|
op.inJustDecodeBounds = true |
|
|
|
op.inJustDecodeBounds = true |
|
|
|
BitmapFactory.decodeStream(inputStream, null, op) //获取尺寸信息 |
|
|
|
BitmapFactory.decodeResource(context.resources, resId, op) //获取尺寸信息 |
|
|
|
//获取比例大小 |
|
|
|
//获取比例大小 |
|
|
|
val wRatio = ceil((op.outWidth / width).toDouble()).toInt() |
|
|
|
val wRatio = ceil((op.outWidth / width).toDouble()).toInt() |
|
|
|
val hRatio = ceil((op.outHeight / height).toDouble()).toInt() |
|
|
|
val hRatio = ceil((op.outHeight / height).toDouble()).toInt() |
|
|
@ -98,9 +129,8 @@ object BitmapUtils { |
|
|
|
op.inSampleSize = hRatio |
|
|
|
op.inSampleSize = hRatio |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
inputStream = context.resources.openRawResource(resId) |
|
|
|
|
|
|
|
op.inJustDecodeBounds = false |
|
|
|
op.inJustDecodeBounds = false |
|
|
|
return BitmapFactory.decodeStream(inputStream, null, op) |
|
|
|
return BitmapFactory.decodeResource(context.resources, resId, op) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -119,31 +149,26 @@ object BitmapUtils { |
|
|
|
height: Int |
|
|
|
height: Int |
|
|
|
): Bitmap? { |
|
|
|
): Bitmap? { |
|
|
|
var inputStream = context.assets.open(fileNameInAssets) |
|
|
|
var inputStream = context.assets.open(fileNameInAssets) |
|
|
|
val op = BitmapFactory.Options() |
|
|
|
return inputStream.use { |
|
|
|
// inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight; |
|
|
|
val op = BitmapFactory.Options() |
|
|
|
op.inJustDecodeBounds = true |
|
|
|
// inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight; |
|
|
|
BitmapFactory.decodeStream(inputStream, null, op) //获取尺寸信息 |
|
|
|
op.inJustDecodeBounds = true |
|
|
|
//获取比例大小 |
|
|
|
BitmapFactory.decodeStream(inputStream, null, op) //获取尺寸信息 |
|
|
|
val wRatio = ceil((op.outWidth / width).toDouble()).toInt() |
|
|
|
//获取比例大小 |
|
|
|
val hRatio = ceil((op.outHeight / height).toDouble()).toInt() |
|
|
|
val wRatio = ceil((op.outWidth / width).toDouble()).toInt() |
|
|
|
//如果超出指定大小,则缩小相应的比例 |
|
|
|
val hRatio = ceil((op.outHeight / height).toDouble()).toInt() |
|
|
|
if (wRatio > 1 && hRatio > 1) { |
|
|
|
//如果超出指定大小,则缩小相应的比例 |
|
|
|
if (wRatio > hRatio) { |
|
|
|
if (wRatio > 1 && hRatio > 1) { |
|
|
|
op.inSampleSize = wRatio |
|
|
|
if (wRatio > hRatio) { |
|
|
|
} else { |
|
|
|
op.inSampleSize = wRatio |
|
|
|
op.inSampleSize = hRatio |
|
|
|
} else { |
|
|
|
|
|
|
|
op.inSampleSize = hRatio |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
inputStream = context.assets.open(fileNameInAssets) |
|
|
|
|
|
|
|
op.inJustDecodeBounds = false |
|
|
|
|
|
|
|
BitmapFactory.decodeStream(inputStream, null, op) |
|
|
|
} |
|
|
|
} |
|
|
|
inputStream = context.assets.open(fileNameInAssets) |
|
|
|
|
|
|
|
op.inJustDecodeBounds = false |
|
|
|
|
|
|
|
return BitmapFactory.decodeStream(inputStream, null, op) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//图片不被压缩 |
|
|
|
|
|
|
|
fun convertViewToBitmap(view: View, bitmapWidth: Int, bitmapHeight: Int): Bitmap { |
|
|
|
|
|
|
|
val bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888) |
|
|
|
|
|
|
|
view.draw(Canvas(bitmap)) |
|
|
|
|
|
|
|
return bitmap |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|