修改图片绘制

pull/1785/head
kunfei 3 years ago
parent 7a1a394581
commit cca0752305
  1. 9
      app/src/main/java/io/legado/app/api/controller/BookController.kt
  2. 3
      app/src/main/java/io/legado/app/ui/book/read/PhotoDialog.kt
  3. 5
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt
  4. 33
      app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt

@ -21,8 +21,8 @@ import io.legado.app.model.localBook.EpubFile
import io.legado.app.model.localBook.LocalBook import io.legado.app.model.localBook.LocalBook
import io.legado.app.model.localBook.UmdFile import io.legado.app.model.localBook.UmdFile
import io.legado.app.model.webBook.WebBook import io.legado.app.model.webBook.WebBook
import io.legado.app.utils.*
import io.legado.app.ui.book.read.page.provider.ImageProvider import io.legado.app.ui.book.read.page.provider.ImageProvider
import io.legado.app.utils.*
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import splitties.init.appCtx import splitties.init.appCtx
@ -87,9 +87,10 @@ object BookController {
this.bookSource = appDb.bookSourceDao.getBookSource(book.origin) this.bookSource = appDb.bookSourceDao.getBookSource(book.origin)
} }
this.bookUrl = bookUrl this.bookUrl = bookUrl
return ImageProvider.getImage(book, src, bookSource, width, width)?.let { val bitmap = runBlocking {
returnData.setData(it) ImageProvider.getImage(book, src, bookSource, width, width)
} ?: returnData.setErrorMsg("图片加载失败或不存在") }
return returnData.setData(bitmap)
} }
/** /**

@ -40,19 +40,16 @@ class PhotoDialog() : BaseDialogFragment(R.layout.dialog_photo_view) {
arguments?.let { arguments?.let {
val path = it.getString("path") val path = it.getString("path")
if (path.isNullOrEmpty()) { if (path.isNullOrEmpty()) {
val chapterIndex = it.getInt("chapterIndex")
val src = it.getString("src") val src = it.getString("src")
ReadBook.book?.let { book -> ReadBook.book?.let { book ->
src?.let { src?.let {
execute { execute {
ImageProvider.getImage(book, src, ReadBook.bookSource) ImageProvider.getImage(book, src, ReadBook.bookSource)
}.onSuccess { bitmap -> }.onSuccess { bitmap ->
if (bitmap != null) {
binding.photoView.setImageBitmap(bitmap) binding.photoView.setImageBitmap(bitmap)
} }
} }
} }
}
} else { } else {
BookCover.load(requireContext(), path = path) BookCover.load(requireContext(), path = path)
.into(binding.photoView) .into(binding.photoView)

@ -10,6 +10,7 @@ import io.legado.app.R
import io.legado.app.constant.PreferKey import io.legado.app.constant.PreferKey
import io.legado.app.data.entities.Bookmark import io.legado.app.data.entities.Bookmark
import io.legado.app.help.config.ReadBookConfig import io.legado.app.help.config.ReadBookConfig
import io.legado.app.help.coroutine.Coroutine
import io.legado.app.lib.theme.accentColor import io.legado.app.lib.theme.accentColor
import io.legado.app.model.ReadBook import io.legado.app.model.ReadBook
import io.legado.app.ui.book.read.PhotoDialog import io.legado.app.ui.book.read.PhotoDialog
@ -167,13 +168,15 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
isImageLine: Boolean isImageLine: Boolean
) { ) {
val book = ReadBook.book ?: return val book = ReadBook.book ?: return
Coroutine.async {
ImageProvider.getImage( ImageProvider.getImage(
book, book,
textChar.charData, textChar.charData,
ReadBook.bookSource, ReadBook.bookSource,
(textChar.end - textChar.start).toInt(), (textChar.end - textChar.start).toInt(),
(lineBottom - lineTop).toInt() (lineBottom - lineTop).toInt()
)?.let { bitmap -> )
}.onSuccess { bitmap ->
val rectF = if (isImageLine) { val rectF = if (isImageLine) {
RectF(textChar.start, lineTop, textChar.end, lineBottom) RectF(textChar.start, lineTop, textChar.end, lineBottom)
} else { } else {

@ -13,8 +13,9 @@ 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.FileUtils import io.legado.app.utils.FileUtils
import io.legado.app.utils.isXml import io.legado.app.utils.isXml
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.suspendCancellableCoroutine import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withContext
import splitties.init.appCtx import splitties.init.appCtx
import java.io.File import java.io.File
import java.io.FileOutputStream import java.io.FileOutputStream
@ -66,23 +67,23 @@ object ImageProvider {
} }
} }
fun getImage( suspend fun getImage(
book: Book, book: Book,
src: String, src: String,
bookSource: BookSource?, bookSource: BookSource?,
width: Int, width: Int,
height: Int height: Int
): Bitmap? { ): Bitmap {
val vFile = runBlocking { return withContext(IO) {
cacheImage(book, src, bookSource) val vFile = cacheImage(book, src, bookSource)
} try {
return try { @Suppress("BlockingMethodInNonBlockingContext")
ImageLoader.loadBitmap(appCtx, vFile.absolutePath) ImageLoader.loadBitmap(appCtx, vFile.absolutePath)
.submit(width, height) .submit(width, height)
.get() .get()
} catch (e: Exception) { } catch (e: Exception) {
Coroutine.async { Coroutine.async {
putDebug("${vFile.absolutePath} 解码失败\n${e.toString()}", e) putDebug("${vFile.absolutePath} 解码失败\n$e", e)
if (FileUtils.readText(vFile.absolutePath).isXml()) { if (FileUtils.readText(vFile.absolutePath).isXml()) {
putDebug("${vFile.absolutePath}为xml,自动删除") putDebug("${vFile.absolutePath}为xml,自动删除")
vFile.delete() vFile.delete()
@ -91,22 +92,23 @@ object ImageProvider {
errorBitmap errorBitmap
} }
} }
}
fun getImage( suspend fun getImage(
book: Book, book: Book,
src: String, src: String,
bookSource: BookSource? bookSource: BookSource?
): Bitmap? { ): Bitmap {
val vFile = runBlocking { return withContext(IO) {
cacheImage(book, src, bookSource) val vFile = cacheImage(book, src, bookSource)
} try {
return try { @Suppress("BlockingMethodInNonBlockingContext")
ImageLoader.loadBitmap(appCtx, vFile.absolutePath) ImageLoader.loadBitmap(appCtx, vFile.absolutePath)
.submit(ChapterProvider.visibleWidth, ChapterProvider.visibleHeight) .submit(ChapterProvider.visibleWidth, ChapterProvider.visibleHeight)
.get() .get()
} catch (e: Exception) { } catch (e: Exception) {
Coroutine.async { Coroutine.async {
putDebug("${vFile.absolutePath} 解码失败\n${e.toString()}", e) putDebug("${vFile.absolutePath} 解码失败\n$e", e)
if (FileUtils.readText(vFile.absolutePath).isXml()) { if (FileUtils.readText(vFile.absolutePath).isXml()) {
putDebug("${vFile.absolutePath}为xml,自动删除") putDebug("${vFile.absolutePath}为xml,自动删除")
vFile.delete() vFile.delete()
@ -115,5 +117,6 @@ object ImageProvider {
errorBitmap errorBitmap
} }
} }
}
} }

Loading…
Cancel
Save