android - 将位图转换为灰度

标签 android android-glide android-bitmap grayscale

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

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 中没有运行,其中出现以下异常

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)

我尝试在我的 list 中将 hardwareAccelerated 添加为 true,但同样发生了。 我可以用 Glide 或其他方式做到这一点吗?

最佳答案

在 Android O 中,他们引入了新的 Bitmap.Config Bitmap.Config.HARDWARE在绘制 Bitmap 时这是一个实际的优化到屏幕。因此,由于您使用的是软件渲染,这就是您收到错误的原因。

试试这个:

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.Config来自 Bitmap.Config.HARDWAREBitmap.Config.ARGB_8888 .

希望对您有所帮助!!!

关于android - 将位图转换为灰度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60941076/

相关文章:

java - IME 的自定义 Android View 的一部分被切断?

android - 使用Parcelable接口(interface)时如何序列化空值

java - Android:java.lang.IllegalStateException:无法在 Android 8.0 上池回收位图(Glide)

c# - 如何将选定的微调器项目获取到字符串?

java - 更改应用程序中的方向

java - Glide中如何获取不超过一定尺寸的缩略图?

android - 在android中使用Glide从图像中获取位图

android - 对于同一个位图,位图有时保存为黑色图像

android - 获取 Canvas 的一部分作为位图

android - 如何在 Android 上的 Glide 中设置位图图像