[英]Convert bitmap to Grayscale
在我的應用程序中,我想將圖像上傳到服務器,但我想在這樣做之前將其轉換為灰度。
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)
我試圖在我的清單中將 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.HARDWARE
替換為Bitmap.Config.ARGB_8888
。
希望能幫助到你!!!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.