Android에서 비트 맵을 그레이 스케일로 변환
저는이 사이트를 처음 사용하는데 Android에 대한 질문이 있습니다.
비트 맵을 회색조로 변환하는 방법이 있습니까? 회색조 비트 맵을 그리는 방법을 알고 있지만 (캔버스 작업 사용 : http://www.mail-archive.com/android-developers@googlegroups.com/msg38890.html ) 실제로 회색 색상의 실제 비트 맵이 필요합니다 (또는 나중에 비트 맵으로 변환 할 수있는 것). 직접 구현해야합니까 (픽셀 단위 작업)?
많이 검색했지만 여전히 찾을 수 없습니다. 누구나 쉽고 효율적인 방법을 알고 있습니까?
감사합니다!
연결하려는 코드가 정확히하는 일이 아닙니까? 색상 비트 맵 ( "bmp")을 사용하여 중복 비트 맵 ( "bm")을 만든 다음 필터를 사용하여 색상 비트 맵을 "bm"으로 그려서 회색 음영으로 변환합니다. 그 시점부터 "bm"을 실제 회색조 비트 맵으로 사용하고 원하는 작업을 수행 할 수 있습니다.
샘플을 약간 조정해야합니다 (하드 코딩 된 크기를 사용하고 있으며 원본 비트 맵의 크기 만 복제 할 수 있음). 그 외에는 사용할 준비가 된 것 같습니다. , 원하는 항목에 따라.
오, 그래요. 나는 그것을 잘못 사용하고 있었다. 그것을 지적 해 주셔서 감사합니다. (쓸모없는 질문에 대해 죄송합니다) 다음은 누군가에게 도움이 될 수 있으므로 마지막 코드입니다 (링크 된 코드를 기반으로 함).
public Bitmap toGrayscale(Bitmap bmpOriginal)
{
int width, height;
height = bmpOriginal.getHeight();
width = bmpOriginal.getWidth();
Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(bmpOriginal, 0, 0, paint);
return bmpGrayscale;
}
그것에 대한 모든 의견이나 의견은 매우 환영합니다.
감사
해당 비트 맵을 ImageView
. 그런 다음 비트 맵을 그레이 스케일로 변환하는 대신 아래 코드를 시도해 볼 수 있습니다.
ColorMatrix matrix = new ColorMatrix();
matrix.setSaturation(0);
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
imageview.setColorFilter(filter);
이 접근 방식에서는 한 가지 중요한 측면을 고려해야한다는 점을 언급하고 싶습니다. Android의 BitMap은 NativeHeap에 저장됩니다. "비트 맵을 생성"하면 결국 메모리가 OutOfMemoryException
막히고 (OOM)이 발생합니다.
따라서 비트 맵은 항상 .recycled()
.
다음은 모든 Android 버전을 지원하기 위해 만든보다 효율적인 방법입니다.
// https://xjaphx.wordpress.com/2011/06/21/image-processing-grayscale-image-on-the-fly/
@JvmStatic
fun getGrayscaledBitmapFallback(src: Bitmap, redVal: Float = 0.299f, greenVal: Float = 0.587f, blueVal: Float = 0.114f): Bitmap {
// create output bitmap
val bmOut = Bitmap.createBitmap(src.width, src.height, src.config)
// pixel information
var A: Int
var R: Int
var G: Int
var B: Int
var pixel: Int
// get image size
val width = src.width
val height = src.height
// scan through every single pixel
for (x in 0 until width) {
for (y in 0 until height) {
// get one pixel color
pixel = src.getPixel(x, y)
// retrieve color of all channels
A = Color.alpha(pixel)
R = Color.red(pixel)
G = Color.green(pixel)
B = Color.blue(pixel)
// take conversion up to one single value
B = (redVal * R + greenVal * G + blueVal * B).toInt()
G = B
R = G
// set new pixel color to output bitmap
bmOut.setPixel(x, y, Color.argb(A, R, G, B))
}
}
// return final image
return bmOut
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@JvmStatic
fun getGrayscaledBitmap(context: Context, src: Bitmap): Bitmap {
// https://gist.github.com/imminent/cf4ab750104aa286fa08
// https://en.wikipedia.org/wiki/Grayscale
val redVal = 0.299f
val greenVal = 0.587f
val blueVal = 0.114f
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1)
return getGrayscaledBitmapFallback(src, redVal, greenVal, blueVal)
val render = RenderScript.create(context)
val matrix = Matrix4f(floatArrayOf(-redVal, -redVal, -redVal, 1.0f, -greenVal, -greenVal, -greenVal, 1.0f, -blueVal, -blueVal, -blueVal, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f))
val result = src.copy(src.config, true)
val input = Allocation.createFromBitmap(render, src, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT)
val output = Allocation.createTyped(render, input.type)
// Inverts and do grayscale to the image
@Suppress("DEPRECATION")
val inverter =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
ScriptIntrinsicColorMatrix.create(render)
else
ScriptIntrinsicColorMatrix.create(render, Element.U8_4(render))
inverter.setColorMatrix(matrix)
inverter.forEach(input, output)
output.copyTo(result)
src.recycle()
render.destroy()
return result
}
참조 URL : https://stackoverflow.com/questions/3373860/convert-a-bitmap-to-grayscale-in-android
'developer tip' 카테고리의 다른 글
document.getElementById (id) .focus ()가 firefox 또는 chrome에서 작동하지 않습니다. (0) | 2021.01.09 |
---|---|
빠르고 간단한 해시 코드 조합 (0) | 2021.01.09 |
프로그래밍 방식으로 UINavigationController에서 "뒤로"버튼을 누르는 방법 (0) | 2021.01.09 |
JavaScript에서 Date ()를 가장 가까운 5 분으로 반올림 (0) | 2021.01.09 |
UIButton에서 이미지를 늘리지 않는 방법 (0) | 2021.01.09 |