gpt4 book ai didi

java - 用于 base64 图像的自定义 Glide ModelLoader

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:00:36 34 4
gpt4 key购买 nike

我有来自某些 SOAP API 的图像,这些图像以 base64 编码。为了使用 Glide 直接加载它们,我决定编写自定义 ModelLoader。

Glide 版本:4.3.1

Glide 载重线:

GlideApp.with(activity)
.load(Data().apply { code = licensePlateData.licensePlateImgId })
.into(view.imageLicense)

GlideModule:

@GlideModule
class MyAppGlideModule : AppGlideModule() {

override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
super.registerComponents(context, glide, registry)
val app = context.applicationContext as MyApplication
registry.append(Data::class.java, ByteArray::class.java, MyImageLoaderFactory(app.api))
}
}

加载程序:

class MyImageLoaderFactory(private val api: Api) : ModelLoaderFactory<Data, ByteArray> {

override fun teardown() {

}

override fun build(multiFactory: MultiModelLoaderFactory): ModelLoader<Data, ByteArray> {
return MyImageLoader(api)
}
}

class MyImageLoader(private val api: Api) : ModelLoader<Data, ByteArray> {

override fun buildLoadData(model: Data, width: Int, height: Int, options: Options): ModelLoader.LoadData<ByteArray> {
val key = "code:${model.code};width:$width;height:$height"
return ModelLoader.LoadData<ByteArray>(ObjectKey(key), MyImageDataFetcher(api, GetImageRequest().apply { data = model }))
}

override fun handles(model: Data): Boolean {
return true
}
}

class MyImageDataFetcher(private val api: Api, private val request: GetImageRequest) : DataFetcher<ByteArray> {

private val disposables = CompositeDisposable()

override fun cleanup() {
disposables.clear()
}

override fun loadData(priority: Priority, callback: DataFetcher.DataCallback<in ByteArray>) {
api.getImage(request) //soap request
.map { it.image.decodeBase64() } //translate String to ByteArray
.subscribe({ callback.onDataReady(it) }, {
if (it is Exception) {
callback.onLoadFailed(it)
} else {
callback.onLoadFailed(MyException(it))
}
})
.addTo(disposables)
}

override fun cancel() {
disposables.clear()
}

override fun getDataClass(): Class<ByteArray> {
return ByteArray::class.java
}

override fun getDataSource(): DataSource {
return DataSource.REMOTE
}

}

堆栈跟踪/LogCat:

com.bumptech.glide.Registry$NoSourceEncoderAvailableException: Failed to find source encoder for data class: class [B
at com.bumptech.glide.Registry.getSourceEncoder(Registry.java:534)
at com.bumptech.glide.load.engine.DecodeHelper.getSourceEncoder(DecodeHelper.java:232)
at com.bumptech.glide.load.engine.SourceGenerator.cacheData(SourceGenerator.java:74)
at com.bumptech.glide.load.engine.SourceGenerator.startNext(SourceGenerator.java:45)
at com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:298)
at com.bumptech.glide.load.engine.DecodeJob.runWrapped(DecodeJob.java:268)
at com.bumptech.glide.load.engine.DecodeJob.run(DecodeJob.java:229)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
at com.bumptech.glide.load.engine.executor.GlideExecutor$DefaultThreadFactory$1.run(GlideExecutor.java:413)

我认为我缺少让它工作的东西。

最佳答案

感谢 https://github.com/bumptech/glide/issues/2677 的评论我找到了解决方案。

@GlideModule
class MyAppGlideModule : AppGlideModule() {

override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
super.registerComponents(context, glide, registry)
val app = context.applicationContext as MyApplication
registry.append(Data::class.java, ByteBuffer::class.java, MyImageLoaderFactory(app.api))
}
}

class MyImageLoaderFactory(private val api: Api) : ModelLoaderFactory<Data, ByteBuffer> {

override fun teardown() {

}

override fun build(multiFactory: MultiModelLoaderFactory): ModelLoader<Data, ByteBuffer> {
return MyImageLoader(api)
}
}

class MyImageLoader(private val api: Api) : ModelLoader<Data, ByteBuffer> {

override fun buildLoadData(model: Data, width: Int, height: Int, options: Options): ModelLoader.LoadData<ByteBuffer> {
val key = "code:${model.code};width:$width;height:$height"
return ModelLoader.LoadData<ByteBuffer>(ObjectKey(key), MyImageDataFetcher(api, GetImageRequest().apply { data = model }))
}

override fun handles(model: Data): Boolean {
return true
}
}

class MyImageDataFetcher(private val api: Api, private val request: GetImageRequest) : DataFetcher<ByteBuffer> {

private val disposables = CompositeDisposable()

override fun cleanup() {
disposables.clear()
}

override fun loadData(priority: Priority, callback: DataFetcher.DataCallback<in ByteBuffer>) {
api.getImage(request)
.map { it.image.decodeBase64() }
.subscribe({ callback.onDataReady(ByteBuffer.wrap(it)) }, {
if (it is Exception) {
callback.onLoadFailed(it)
} else {
callback.onLoadFailed(MyException(it))
}
})
.addTo(disposables)
}

override fun cancel() {
disposables.clear()
}

override fun getDataClass(): Class<ByteBuffer> {
return ByteBuffer::class.java
}

override fun getDataSource(): DataSource {
return DataSource.REMOTE
}

}

关于java - 用于 base64 图像的自定义 Glide ModelLoader,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47631363/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com