Merge branch 'gedoor:master' into master

pull/1786/head
Xwite 3 years ago committed by GitHub
commit d1172d58fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      app/src/main/java/io/legado/app/api/controller/BookController.kt
  2. 4
      app/src/main/java/io/legado/app/help/http/cronet/CronetInterceptor.kt
  3. 3
      app/src/main/java/io/legado/app/help/storage/BackupConfig.kt
  4. 21
      app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt
  5. 69
      app/src/main/java/io/legado/app/utils/BitmapUtils.kt

@ -89,7 +89,7 @@ object BookController {
this.bookUrl = bookUrl this.bookUrl = bookUrl
val bitmap = runBlocking { val bitmap = runBlocking {
ImageProvider.cacheImage(book, src, bookSource) ImageProvider.cacheImage(book, src, bookSource)
ImageProvider.getImage(book, src, width, width) ImageProvider.getImage(book, src, width)
} }
return returnData.setData(bitmap) return returnData.setData(bitmap)
} }

@ -48,8 +48,8 @@ class CronetInterceptor(private val cookieJar: CookieJar?) : Interceptor {
} else { } else {
OldCallback(request, call) OldCallback(request, call)
} }
buildRequest(request, callBack)?.let { buildRequest(request, callBack)?.runCatching {
return callBack.waitForDone(it) return callBack.waitForDone(this)
} }
return null return null
} }

@ -26,7 +26,8 @@ object BackupConfig {
PreferKey.webDavUrl, PreferKey.webDavUrl,
PreferKey.webDavDir, PreferKey.webDavDir,
PreferKey.webDavAccount, PreferKey.webDavAccount,
PreferKey.webDavPassword PreferKey.webDavPassword,
PreferKey.launcherIcon
) )
//配置忽略标题 //配置忽略标题

@ -80,4 +80,25 @@ object ImageProvider {
} }
} }
fun getImage(
book: Book,
src: String,
width: Int
): Bitmap {
val vFile = BookHelp.getImage(book, src)
@Suppress("BlockingMethodInNonBlockingContext")
return try {
BitmapUtils.decodeBitmap(vFile.absolutePath, width)
} catch (e: Exception) {
Coroutine.async {
putDebug("${vFile.absolutePath} 解码失败\n$e", e)
if (FileUtils.readText(vFile.absolutePath).isXml()) {
putDebug("${vFile.absolutePath}为xml,自动删除")
vFile.delete()
}
}
errorBitmap
}
}
} }

@ -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,10 +28,13 @@ 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 fis = FileInputStream(path)
return fis.use {
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.decodeFile(path, op) BitmapFactory.decodeFileDescriptor(fis.fd, null, 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()
@ -41,7 +47,32 @@ object BitmapUtils {
} }
} }
op.inJustDecodeBounds = false 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.decodeFileDescriptor(fis.fd, null, op)
//获取比例大小
val wRatio = ceil((op.outWidth / width).toDouble()).toInt()
//如果超出指定大小,则缩小相应的比例
if (wRatio > 1) {
op.inSampleSize = wRatio
}
op.inJustDecodeBounds = false
BitmapFactory.decodeFileDescriptor(fis.fd, null, 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 fis = FileInputStream(path)
return fis.use {
val opts = BitmapFactory.Options() val opts = BitmapFactory.Options()
opts.inJustDecodeBounds = true opts.inJustDecodeBounds = true
BitmapFactory.decodeFile(path, opts)
BitmapFactory.decodeFileDescriptor(fis.fd, null, opts)
opts.inSampleSize = computeSampleSize(opts, -1, 128 * 128) opts.inSampleSize = computeSampleSize(opts, -1, 128 * 128)
opts.inJustDecodeBounds = false opts.inJustDecodeBounds = false
return BitmapFactory.decodeFile(path, opts) 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,6 +149,7 @@ object BitmapUtils {
height: Int height: Int
): Bitmap? { ): Bitmap? {
var inputStream = context.assets.open(fileNameInAssets) var inputStream = context.assets.open(fileNameInAssets)
return inputStream.use {
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
@ -136,14 +167,8 @@ object BitmapUtils {
} }
inputStream = context.assets.open(fileNameInAssets) inputStream = context.assets.open(fileNameInAssets)
op.inJustDecodeBounds = false op.inJustDecodeBounds = false
return BitmapFactory.decodeStream(inputStream, null, op) 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
} }
/** /**

Loading…
Cancel
Save