From 1312bb038cca3a28dc81d459ea2d7e93e12b9293 Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 14 Apr 2022 14:15:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/api/controller/BookController.kt | 2 +- .../book/read/page/provider/ImageProvider.kt | 21 +++++++++++++++++++ .../java/io/legado/app/utils/BitmapUtils.kt | 16 ++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/io/legado/app/api/controller/BookController.kt b/app/src/main/java/io/legado/app/api/controller/BookController.kt index 8771c8225..9254585f1 100644 --- a/app/src/main/java/io/legado/app/api/controller/BookController.kt +++ b/app/src/main/java/io/legado/app/api/controller/BookController.kt @@ -89,7 +89,7 @@ object BookController { this.bookUrl = bookUrl val bitmap = runBlocking { ImageProvider.cacheImage(book, src, bookSource) - ImageProvider.getImage(book, src, width, width) + ImageProvider.getImage(book, src, width) } return returnData.setData(bitmap) } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt index 2de937b44..147d33e41 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt @@ -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 + } + } + } diff --git a/app/src/main/java/io/legado/app/utils/BitmapUtils.kt b/app/src/main/java/io/legado/app/utils/BitmapUtils.kt index 9fbba9c02..fce4366f6 100644 --- a/app/src/main/java/io/legado/app/utils/BitmapUtils.kt +++ b/app/src/main/java/io/legado/app/utils/BitmapUtils.kt @@ -44,6 +44,22 @@ object BitmapUtils { return BitmapFactory.decodeFile(path, op) } + @Throws(IOException::class) + fun decodeBitmap(path: String, width: Int): Bitmap { + val op = BitmapFactory.Options() + // inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight; + op.inJustDecodeBounds = true + BitmapFactory.decodeFile(path, op) + //获取比例大小 + val wRatio = ceil((op.outWidth / width).toDouble()).toInt() + //如果超出指定大小,则缩小相应的比例 + if (wRatio > 1) { + op.inSampleSize = wRatio + } + op.inJustDecodeBounds = false + return BitmapFactory.decodeFile(path, op) + } + /** 从path中获取Bitmap图片 * @param path 图片路径 * @return