반응형

[안드로이드]Bitmap black&white(흑백)만들기


안녕하세요 예지우랑입니다.


안드로이드 개발중에 이미지를 다루는것은 여간 귀찮은것이 아니지요

생각없이 비트맵에 불러왔다가 메모리 오류가 뜨는경우도 있고

다루다보면 마음대로 안되기 마련이죠 


오늘은 비트맵을 흑백으로 만드는 방법에대해 알려드리려합니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
 *bitmap 흑백으로 변환
 */
    private Bitmap grayScale(final Bitmap orgBitmap){
        Log.i("gray""in");
        int width, height;
        width = orgBitmap.getWidth();
        height = orgBitmap.getHeight();
 
        Bitmap bmpGrayScale = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
 
        // color information
        int A, R, G, B;
        int pixel;
 
        // scan through all pixels
        for (int x = 0; x < width; ++x) {
            for (int y = 0; y < height; ++y) {
                // get pixel color
                pixel = orgBitmap.getPixel(x, y);
                A = Color.alpha(pixel);
                R = Color.red(pixel);
                G = Color.green(pixel);
                B = Color.blue(pixel);
                int gray = (int) (0.2989 * R + 0.5870 * G + 0.1140 * B);
 
                // use 128 as threshold, above -> white, below -> black
                if (gray > 128)
                    gray = 255;
                else
                    gray = 0;
                // set new pixel color to output bitmap
                bmpGrayScale.setPixel(x, y, Color.argb(A, gray, gray, gray));
            }
        }
        return bmpGrayScale;
 
    }
cs


비트맵에 이 소스를 적용하면 

정확하게 흑/백으로 나뉘게 됩니다.

처음에 이소스를 만든 이유는 혹시나 이렇게 흑백으로 만들면 

용량이 줄어들까 해서 만들어봤는데 

용량은 안줄어들더군요 ㅠㅠ

혹시 이미지 용량을 줄일 수 있는 방법을 알고계신 분 있으시다면 

댓글이나 메일 부탁드립니다 ㅠㅠ


감사합니다.



반응형

+ Recent posts