Merge pull request #1770 from Xwite/master

正文图片解码
pull/1771/head
kunfei 2 years ago committed by GitHub
commit bcc5f5351a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      app/src/main/java/io/legado/app/api/controller/BookController.kt
  2. 6
      app/src/main/java/io/legado/app/help/BookHelp.kt
  3. 2
      app/src/main/java/io/legado/app/model/ReadBook.kt
  4. 52
      app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt

@ -79,7 +79,6 @@ object BookController {
?: return returnData.setErrorMsg("bookUrl为空") ?: return returnData.setErrorMsg("bookUrl为空")
val src = parameters["path"]?.firstOrNull() val src = parameters["path"]?.firstOrNull()
?: return returnData.setErrorMsg("图片链接为空") ?: return returnData.setErrorMsg("图片链接为空")
val width = parameters["width"]?.firstOrNull()?.toInt() ?: 640
if (this.bookUrl != bookUrl) { if (this.bookUrl != bookUrl) {
this.book = appDb.bookDao.getBook(bookUrl) this.book = appDb.bookDao.getBook(bookUrl)
?: return returnData.setErrorMsg("bookUrl不对") ?: return returnData.setErrorMsg("bookUrl不对")
@ -95,7 +94,7 @@ object BookController {
} }
this.bookUrl = bookUrl this.bookUrl = bookUrl
return returnData.setData( return returnData.setData(
BitmapUtils.decodeBitmap(vFile.absolutePath, width, width) ImageLoader.loadBitmap(appCtx, vFile.absolutePath).submit().get()
) )
} }

@ -122,7 +122,7 @@ object BookHelp {
cacheFolderName, cacheFolderName,
book.getFolderName(), book.getFolderName(),
cacheImageFolderName, cacheImageFolderName,
"${MD5Utils.md5Encode16(src)}${getImageSuffix(src)}" "${MD5Utils.md5Encode16(src)}.${getImageSuffix(src)}"
).writeBytes(it) ).writeBytes(it)
} }
} catch (e: Exception) { } catch (e: Exception) {
@ -137,14 +137,14 @@ object BookHelp {
cacheFolderName, cacheFolderName,
book.getFolderName(), book.getFolderName(),
cacheImageFolderName, cacheImageFolderName,
"${MD5Utils.md5Encode16(src)}${getImageSuffix(src)}" "${MD5Utils.md5Encode16(src)}.${getImageSuffix(src)}"
) )
} }
fun getImageSuffix(src: String): String { fun getImageSuffix(src: String): String {
var suffix = src.substringAfterLast(".").substringBefore(",") var suffix = src.substringAfterLast(".").substringBefore(",")
if (suffix.length > 5) { if (suffix.length > 5) {
suffix = ".jpg" suffix = "jpg"
} }
return suffix return suffix
} }

@ -55,7 +55,6 @@ object ReadBook : CoroutineScope by MainScope() {
callBack?.upMenuView() callBack?.upMenuView()
callBack?.upPageAnim() callBack?.upPageAnim()
upWebBook(book) upWebBook(book)
ImageProvider.clearAllCache()
synchronized(this) { synchronized(this) {
loadingChapters.clear() loadingChapters.clear()
} }
@ -207,7 +206,6 @@ object ReadBook : CoroutineScope by MainScope() {
} }
upReadTime() upReadTime()
preDownload() preDownload()
ImageProvider.clearOut(durChapterIndex)
} }
/** /**

@ -4,32 +4,17 @@ import android.graphics.Bitmap
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.help.BookHelp import io.legado.app.help.BookHelp
import io.legado.app.help.glide.ImageLoader
import io.legado.app.model.localBook.EpubFile import io.legado.app.model.localBook.EpubFile
import io.legado.app.utils.BitmapUtils import io.legado.app.utils.BitmapUtils
import io.legado.app.utils.FileUtils import io.legado.app.utils.FileUtils
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import splitties.init.appCtx
import java.io.FileOutputStream import java.io.FileOutputStream
import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentHashMap
object ImageProvider { object ImageProvider {
private val cache = ConcurrentHashMap<Int, ConcurrentHashMap<String, Bitmap>>()
@Synchronized
fun getCache(chapterIndex: Int, src: String): Bitmap? {
return cache[chapterIndex]?.get(src)
}
@Synchronized
fun setCache(chapterIndex: Int, src: String, bitmap: Bitmap) {
var indexCache = cache[chapterIndex]
if (indexCache == null) {
indexCache = ConcurrentHashMap()
cache[chapterIndex] = indexCache
}
indexCache[src] = bitmap
}
fun getImage( fun getImage(
book: Book, book: Book,
chapterIndex: Int, chapterIndex: Int,
@ -37,9 +22,6 @@ object ImageProvider {
bookSource: BookSource?, bookSource: BookSource?,
onUi: Boolean = false, onUi: Boolean = false,
): Bitmap? { ): Bitmap? {
getCache(chapterIndex, src)?.let {
return it
}
val vFile = BookHelp.getImage(book, src) val vFile = BookHelp.getImage(book, src)
if (!vFile.exists()) { if (!vFile.exists()) {
if (book.isEpub()) { if (book.isEpub()) {
@ -56,38 +38,10 @@ object ImageProvider {
} }
} }
return try { return try {
val bitmap = BitmapUtils.decodeBitmap( ImageLoader.loadBitmap(appCtx, vFile.absolutePath).submit().get()
vFile.absolutePath,
ChapterProvider.visibleWidth,
ChapterProvider.visibleHeight
)
setCache(chapterIndex, src, bitmap)
bitmap
} catch (e: Exception) { } catch (e: Exception) {
null null
} }
} }
@Synchronized
fun clearAllCache() {
cache.forEach { indexCache ->
indexCache.value.forEach {
it.value.recycle()
}
}
cache.clear()
}
@Synchronized
fun clearOut(chapterIndex: Int) {
cache.forEach { indexCache ->
if (indexCache.key !in chapterIndex - 1..chapterIndex + 1) {
indexCache.value.forEach {
it.value.recycle()
}
cache.remove(indexCache.key)
}
}
}
} }

Loading…
Cancel
Save