修改图片绘制

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. 5
      app/src/main/java/io/legado/app/ui/book/read/PhotoDialog.kt
  3. 17
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt
  4. 69
      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.UmdFile
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.utils.*
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import splitties.init.appCtx
@ -87,9 +87,10 @@ object BookController {
this.bookSource = appDb.bookSourceDao.getBookSource(book.origin)
}
this.bookUrl = bookUrl
return ImageProvider.getImage(book, src, bookSource, width, width)?.let {
returnData.setData(it)
} ?: returnData.setErrorMsg("图片加载失败或不存在")
val bitmap = runBlocking {
ImageProvider.getImage(book, src, bookSource, width, width)
}
return returnData.setData(bitmap)
}
/**

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

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

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

Loading…
Cancel
Save