首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将位图转换为灰度

将位图转换为灰度
EN

Stack Overflow用户
提问于 2020-03-30 23:18:37
回答 1查看 1.5K关注 0票数 3

在我的应用程序中,我想上传一个图像到服务器,但是我想在执行that.< br />之前将其转换为灰度,所以我找到了一个函数,它接受位图并返回位图作为灰度。

代码语言:javascript
运行
复制
fun getGrayScale(src: Bitmap): Bitmap {

        //Custom color matrix to convert to GrayScale
        val matrix = floatArrayOf(
            0.3f,
            0.59f,
            0.11f,
            0f,
            0f,
            0.3f,
            0.59f,
            0.11f,
            0f,
            0f,
            0.3f,
            0.59f,
            0.11f,
            0f,
            0f,
            0f,
            0f,
            0f,
            1f,
            0f
        )

        val dest = Bitmap.createBitmap(src.width, src.height, Bitmap.Config.RGB_565)

        val canvas = Canvas(dest)
        val paint = Paint()
        val filter = ColorMatrixColorFilter(matrix)
        paint.colorFilter = filter
        canvas.drawBitmap(src, 0.toFloat(), 0.toFloat(), paint)

        return dest
    }

它成功运行,但在Android 10中没有运行,在Android 10中,我得到以下异常

代码语言:javascript
运行
复制
java.util.concurrent.ExecutionException: java.lang.IllegalArgumentException: Software rendering doesn't support hardware bitmaps
        at androidx.work.impl.utils.futures.AbstractFuture.getDoneValue(AbstractFuture.java:516)
        at androidx.work.impl.utils.futures.AbstractFuture.get(AbstractFuture.java:475)
        at androidx.work.impl.WorkerWrapper$2.run(WorkerWrapper.java:298)
        at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:91)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:919)
     Caused by: java.lang.IllegalArgumentException: Software rendering doesn't support hardware bitmaps
        at android.graphics.BaseCanvas.onHwBitmapInSwMode(BaseCanvas.java:632)
        at android.graphics.BaseCanvas.throwIfHwBitmapInSwMode(BaseCanvas.java:639)
        at android.graphics.BaseCanvas.throwIfCannotDraw(BaseCanvas.java:73)
        at android.graphics.BaseCanvas.drawBitmap(BaseCanvas.java:113)
        at android.graphics.Canvas.drawBitmap(Canvas.java:1540)
        at com.example.utilities.ImageUtils.getGrayScale(ImageUtils.kt:64)
        at com.example.utilities.FileUtils.imageRefactor(FileUtils.kt:98)
        at com.example.workmanager.FileSplitterWorker.doWork(FileSplitterWorker.kt:54)
        at androidx.work.CoroutineWorker$startWork$1.invokeSuspend(CoroutineWorker.kt:68)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:241)
        at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
        at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:740)

我试图在我的清单中将hardwareAccelerated添加为true,但同样的情况也发生了。我能用幻灯片吗,或者用别的方法?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-08 12:34:53

在Android中,他们引入了新的Bitmap.ConfigBitmap.Config.HARDWARE,这是一种实际的优化,当涉及到将Bitmap绘制到屏幕上时。因此,由于您使用的是软件呈现,这就是为什么您会得到错误。

试试这个:

代码语言:javascript
运行
复制
fun getGrayScaleBitmap(original: Bitmap): Bitmap {
    // You have to make the Bitmap mutable when changing the config because there will be a crash
    // That only mutable Bitmap's should be allowed to change config.
    val bmp = original.copy(Bitmap.Config.ARGB_8888, true)
    val bmpGrayscale = Bitmap.createBitmap(bmp.width, bmp.height, Bitmap.Config.ARGB_8888)
    val canvas = Canvas(bmpGrayscale)
    val paint = Paint()
    val colorMatrix = ColorMatrix()
    colorMatrix.setSaturation(0f)
    val colorMatrixFilter = ColorMatrixColorFilter(colorMatrix)
    paint.colorFilter = colorMatrixFilter
    canvas.drawBitmap(bmp, 0F, 0F, paint)
    return bmpGrayscale
}

这将取代Bitmap.ConfigBitmap.Config.HARDWAREBitmap.Config.ARGB_8888

希望能帮上忙!

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60941076

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档