2011-02-24 6 views
0

그레이 스케일 JPG 그림이 있는데 형식이 비트 맵 Bitmap.Config.ALPHA_8으로로드하고 싶습니다. 그게 가능하니, 어떻게 할 수 있니?그레이 스케일 비트 맵을 알파 마스크로 변환하는 방법은 무엇입니까?

PNG (비어있는 R, G, B 채널을 가질 수 있음)에서 알파 채널을로드하는 것은 간단하지만 압축 할 때 JPG를 사용하고 싶습니다.

이 구출 How to combine two opaque bitmaps into one with alpha channel?

답변

10

ColorMatrix에 대한 후속 질문입니다!

인용 안드로이드 문서, ColorMatrix : 비트 맵의 ​​ 컬러 + 알파 성분을 변환하기위한

× 4 행렬. 행렬은 배열로 저장되고 다음과 같이 처리됩니다. [ a, b, c, d, e, f, g, h, i, j, k, l, m, n, p, q, r, s, t] 에 [r, g, b, a] 색상을 적용하면 색상은 (클램핑 후) R '= a R + b G + c B + dA + e; G '= f R + gG + h B + i A + j; B '= k R + lg + m b + n A + o; A '= p R + q G + r B + s A + t;

Paint.setColorFilter()에서 사용 (그레이 스케일 ... 문제가되지 않거나, 녹색, 파란색) 빨강 채널에서 알파 값을 취 컬러 매트릭스를 설정합니다. 다음은 다소 예입니다 :

final BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
options.inScaled = false; 

// Load source grayscale bitmap 
Bitmap grayscale = BitmapFactory.decodeResource(getResources(), R.drawable.my_grayscale_mask, options); 
// Place for alpha mask. It's specifically ARGB_8888 not ALPHA_8, 
// ALPHA_8 for some reason didn't work out for me. 
Bitmap alpha = Bitmap.createBitmap(grayscale.getWidth(), grayscale.getHeight(), 
     Bitmap.Config.ARGB_8888); 
float[] matrix = new float[] { 
     0, 0, 0, 0, 0, 
     0, 0, 0, 0, 0, 
     0, 0, 0, 0, 0, 
     1, 0, 0, 0, 0}; 
Paint grayToAlpha = new Paint(); 
grayToAlpha.setColorFilter(new ColorMatrixColorFilter(new ColorMatrix(matrix))); 
Canvas alphaCanvas = new Canvas(alpha); 
// Make sure nothing gets scaled during drawing 
alphaCanvas.setDensity(Bitmap.DENSITY_NONE); 
// Draw grayscale bitmap on to alpha canvas, using color filter that 
// takes alpha from red channel 
alphaCanvas.drawBitmap(grayscale, 0, 0, grayToAlpha); 
// Bitmap alpha now has usable alpha channel! 
+1

이 매트릭스는 마스크 밖으로 빨간 채널 만 가져 오지 않습니까? 소스가 실제로 그레이 스케일 인 경우에는 물론 작동합니다. mactix가 일반 RGB와 어떻게 비슷합니까? 하단 라인에서 '0.333, 0.333, 0.334, 0, 0'라고 생각합니다. 그게 맞습니까? –

관련 문제