2015년 9월 3일 목요일

[Android] 이미지 대표 색상 추출 하기

안드로이드에서 bitmap의 대표색상을 추출은
안드로이드 support library v7에 포함되어 있는 Palette Class를 통해 가능하다.

1
2
3
4
5
6
7
8
9
10
11
12
13
int color = 0xFFFFFF// default white
Palette.Builder pb = Palette.from(bitmap);
Palette palette = pb.generate();
if (palette != null && palette.getLightVibrantSwatch() != null) {
    color = palette.getLightVibrantSwatch().getRgb();
}else if (palette != null && palette.getDarkVibrantSwatch() != null) {
    color = palette.getDarkVibrantSwatch().getRgb();
else if (palette != null && palette.getDarkMutedSwatch() != null) {
    color = palette.getDarkMutedSwatch().getRgb();
else if (palette != null && palette.getLightMutedSwatch() != null) {
    color = palette.getLightMutedSwatch().getRgb();
}
Log.e(TAG, "dominantColorFromBitmap = " + Integer.toString(color, 16));
cs

정확한 이유는 모르겠지만 bitmap에서 추출할 수 있는 대표색의 종류가 밝은색, 어두운색 등으로 나뉘지만 모든 색상이 다 추출되지는 않기 때문에 우선순위를 두어 추출하길 권장 한다.