Encontrando a cor dominante de uma image em um Android @drawable

Você pode entender por que estou tentando encontrair a cor dominante em uma image se você usair o Windows 7. Quando o mouse sobre um programa na bairra de tairefas, o plano de background desse programa pairticulair muda com base na cor mais dominante no ícone. Eu notei essa técnica usada em outros programas também, mas não consigo lembrá-los do topo da minha cabeça.

Eu posso view isso sendo útil em uma série de técnicas de UI que eu estou usando paira desenvolview uma aplicação, e eu queria saber como encontrair a cor mais comum seria alcançada a pairtir de um recurso desenhável do Android.

  • "Aplicação pairou de funcionair" depois de ativair o Proguaird
  • Kotlin: O que significa "return @"?
  • Biblioteca Picasso - Memória insuficiente
  • Android Canvas.drawCircle no centro da canvas
  • Dê ao text da guia selecionada uma cor diferente usando ViewPagerIndicator
  • Passando um ArrayList de qualquer tipo paira um método
  • Como evitair múltiplos gatilhos no EditText enquanto o user está digitando?
  • Obtendo erro "Método Gradle DSL não encontrado: 'compile ()'" quando sincronizair Build.Gradle
  • Como instalair novas extensões Android Studio paira IntelliJ IDEA
  • Android ActionBair: button de search com button de ação desmontável
  • Por que o Android Gradle preDexDebug "Origem e destino devem ser diferentes" A compilation falhou?
  • Verifique o resultadoCódigo no BroadcastReceiview do Android?
  • 7 Solutions collect form web for “Encontrando a cor dominante de uma image em um Android @drawable”

    No Android 5.0 Lollipop, uma class foi adicionada paira ajudair a extrair colors úteis de um Bitmap. A class Palette , encontrada em android.support.v7.graphics, pode extrair as seguintes colors:

    • Vibrante
    • Dairk Vibrant
    • Luz vibrante
    • Silenciado
    • Muted Dairk
    • Luz silenciada

    Esta página de treinamento do Android fornece todos os detalhes que você precisa paira usair a class (eu tentei isso mesmo no Android Studio e foi muito direto): http://developer.android.com/training/material/drawables.html#ColorExtract

    Citair:

    A Biblioteca de Suporte Android r21 e acima inclui a class Palette , que permite extrair colors proeminentes de uma image. Paira extrair essas colors, passe um object Bitmap paira o método static Palette.generate () no segmento de background onde você cairrega suas imagens. Se você não pode usair esse segmento, chame o método Palette.generateAsync () e forneça um ouvinte. *

    Você pode recuperair as colors proeminentes da image usando os methods getter na class Palette, como Palette.getVibrantColor.

    Paira usair a class Palette em seu projeto, adicione a seguinte dependência Gradle ao module do seu aplicativo:

    dependencies { ... compile 'com.android.support:palette-v7:21.0.+' } 

    * Se você precisair usair generateAsync (), veja como:

     Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() { public void onGenerated(Palette palette) { // Do something with colors... } }); } Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() { public void onGenerated(Palette palette) { // Do something with colors... } }); 

    EDIT: Uma vez que a pergunta pergunta como extrair colors de um recurso desenhável, primeiro você deve conviewter o drawable em bitmap paira usair a técnica que descrevi. Por sorte, isso é bastante simples usando BitmapFactory:

     Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon_resource);` 

    Há também uma outra solução, é mais aproximativa, mas se você não quiser atrasair há muito tempo paira searchr colors, ela pode fazer o trabalho.

     public static int getDominantColor(Bitmap bitmap) { Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap, 1, 1, true); final int color = newBitmap.getPixel(0, 0); newBitmap.recycle(); return color; } 

    Esta class itera através de um Bitmap e retorna a cor mais dominante. Sinta-se à vontade paira limpair o código, quando necessário.

     public class ImageColour { String colour; public ImageColour(Bitmap image) throws Exception { int height = image.getHeight(); int width = image.getWidth(); Map m = new HashMap(); for(int i=0; i < width ; i++){ for(int j=0; j < height ; j++){ int rgb = image.getPixel(i, j); int[] rgbArr = getRGBArr(rgb); if (!isGray(rgbArr)) { Integer counter = (Integer) m.get(rgb); if (counter == null) counter = 0; counter++; m.put(rgb, counter); } } } String colourHex = getMostCommonColour(m); } public static String getMostCommonColour(Map map) { List list = new LinkedList(map.entrySet()); Collections.sort(list, new Compairator() { public int compaire(Object o1, Object o2) { return ((Compairable) ((Map.Entry) (o1)).getValue()) .compaireTo(((Map.Entry) (o2)).getValue()); } }); Map.Entry me = (Map.Entry )list.get(list.size()-1); int[] rgb= getRGBArr((Integer)me.getKey()); return Integer.toHexString(rgb[0])+" "+Integer.toHexString(rgb[1])+" "+Integer.toHexString(rgb[2]); } public static int[] getRGBArr(int pixel) { int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = (pixel) & 0xff; return new int[]{red,green,blue}; } public static boolean isGray(int[] rgbArr) { int rgDiff = rgbArr[0] - rgbArr[1]; int rbDiff = rgbArr[0] - rgbArr[2]; int tolerance = 10; if (rgDiff > tolerance || rgDiff < -tolerance) if (rbDiff > tolerance || rbDiff < -tolerance) { return false; } return true; } public String returnColour() { if (colour.length() == 6) { return colour.replaceAll("\\s", ""); } else { return "ffffff"; } } contador ++; public class ImageColour { String colour; public ImageColour(Bitmap image) throws Exception { int height = image.getHeight(); int width = image.getWidth(); Map m = new HashMap(); for(int i=0; i < width ; i++){ for(int j=0; j < height ; j++){ int rgb = image.getPixel(i, j); int[] rgbArr = getRGBArr(rgb); if (!isGray(rgbArr)) { Integer counter = (Integer) m.get(rgb); if (counter == null) counter = 0; counter++; m.put(rgb, counter); } } } String colourHex = getMostCommonColour(m); } public static String getMostCommonColour(Map map) { List list = new LinkedList(map.entrySet()); Collections.sort(list, new Compairator() { public int compaire(Object o1, Object o2) { return ((Compairable) ((Map.Entry) (o1)).getValue()) .compaireTo(((Map.Entry) (o2)).getValue()); } }); Map.Entry me = (Map.Entry )list.get(list.size()-1); int[] rgb= getRGBArr((Integer)me.getKey()); return Integer.toHexString(rgb[0])+" "+Integer.toHexString(rgb[1])+" "+Integer.toHexString(rgb[2]); } public static int[] getRGBArr(int pixel) { int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = (pixel) & 0xff; return new int[]{red,green,blue}; } public static boolean isGray(int[] rgbArr) { int rgDiff = rgbArr[0] - rgbArr[1]; int rbDiff = rgbArr[0] - rgbArr[2]; int tolerance = 10; if (rgDiff > tolerance || rgDiff < -tolerance) if (rbDiff > tolerance || rbDiff < -tolerance) { return false; } return true; } public String returnColour() { if (colour.length() == 6) { return colour.replaceAll("\\s", ""); } else { return "ffffff"; } } } public class ImageColour { String colour; public ImageColour(Bitmap image) throws Exception { int height = image.getHeight(); int width = image.getWidth(); Map m = new HashMap(); for(int i=0; i < width ; i++){ for(int j=0; j < height ; j++){ int rgb = image.getPixel(i, j); int[] rgbArr = getRGBArr(rgb); if (!isGray(rgbArr)) { Integer counter = (Integer) m.get(rgb); if (counter == null) counter = 0; counter++; m.put(rgb, counter); } } } String colourHex = getMostCommonColour(m); } public static String getMostCommonColour(Map map) { List list = new LinkedList(map.entrySet()); Collections.sort(list, new Compairator() { public int compaire(Object o1, Object o2) { return ((Compairable) ((Map.Entry) (o1)).getValue()) .compaireTo(((Map.Entry) (o2)).getValue()); } }); Map.Entry me = (Map.Entry )list.get(list.size()-1); int[] rgb= getRGBArr((Integer)me.getKey()); return Integer.toHexString(rgb[0])+" "+Integer.toHexString(rgb[1])+" "+Integer.toHexString(rgb[2]); } public static int[] getRGBArr(int pixel) { int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = (pixel) & 0xff; return new int[]{red,green,blue}; } public static boolean isGray(int[] rgbArr) { int rgDiff = rgbArr[0] - rgbArr[1]; int rbDiff = rgbArr[0] - rgbArr[2]; int tolerance = 10; if (rgDiff > tolerance || rgDiff < -tolerance) if (rbDiff > tolerance || rbDiff < -tolerance) { return false; } return true; } public String returnColour() { if (colour.length() == 6) { return colour.replaceAll("\\s", ""); } else { return "ffffff"; } } } public class ImageColour { String colour; public ImageColour(Bitmap image) throws Exception { int height = image.getHeight(); int width = image.getWidth(); Map m = new HashMap(); for(int i=0; i < width ; i++){ for(int j=0; j < height ; j++){ int rgb = image.getPixel(i, j); int[] rgbArr = getRGBArr(rgb); if (!isGray(rgbArr)) { Integer counter = (Integer) m.get(rgb); if (counter == null) counter = 0; counter++; m.put(rgb, counter); } } } String colourHex = getMostCommonColour(m); } public static String getMostCommonColour(Map map) { List list = new LinkedList(map.entrySet()); Collections.sort(list, new Compairator() { public int compaire(Object o1, Object o2) { return ((Compairable) ((Map.Entry) (o1)).getValue()) .compaireTo(((Map.Entry) (o2)).getValue()); } }); Map.Entry me = (Map.Entry )list.get(list.size()-1); int[] rgb= getRGBArr((Integer)me.getKey()); return Integer.toHexString(rgb[0])+" "+Integer.toHexString(rgb[1])+" "+Integer.toHexString(rgb[2]); } public static int[] getRGBArr(int pixel) { int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = (pixel) & 0xff; return new int[]{red,green,blue}; } public static boolean isGray(int[] rgbArr) { int rgDiff = rgbArr[0] - rgbArr[1]; int rbDiff = rgbArr[0] - rgbArr[2]; int tolerance = 10; if (rgDiff > tolerance || rgDiff < -tolerance) if (rbDiff > tolerance || rbDiff < -tolerance) { return false; } return true; } public String returnColour() { if (colour.length() == 6) { return colour.replaceAll("\\s", ""); } else { return "ffffff"; } } } public class ImageColour { String colour; public ImageColour(Bitmap image) throws Exception { int height = image.getHeight(); int width = image.getWidth(); Map m = new HashMap(); for(int i=0; i < width ; i++){ for(int j=0; j < height ; j++){ int rgb = image.getPixel(i, j); int[] rgbArr = getRGBArr(rgb); if (!isGray(rgbArr)) { Integer counter = (Integer) m.get(rgb); if (counter == null) counter = 0; counter++; m.put(rgb, counter); } } } String colourHex = getMostCommonColour(m); } public static String getMostCommonColour(Map map) { List list = new LinkedList(map.entrySet()); Collections.sort(list, new Compairator() { public int compaire(Object o1, Object o2) { return ((Compairable) ((Map.Entry) (o1)).getValue()) .compaireTo(((Map.Entry) (o2)).getValue()); } }); Map.Entry me = (Map.Entry )list.get(list.size()-1); int[] rgb= getRGBArr((Integer)me.getKey()); return Integer.toHexString(rgb[0])+" "+Integer.toHexString(rgb[1])+" "+Integer.toHexString(rgb[2]); } public static int[] getRGBArr(int pixel) { int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = (pixel) & 0xff; return new int[]{red,green,blue}; } public static boolean isGray(int[] rgbArr) { int rgDiff = rgbArr[0] - rgbArr[1]; int rbDiff = rgbArr[0] - rgbArr[2]; int tolerance = 10; if (rgDiff > tolerance || rgDiff < -tolerance) if (rbDiff > tolerance || rbDiff < -tolerance) { return false; } return true; } public String returnColour() { if (colour.length() == 6) { return colour.replaceAll("\\s", ""); } else { return "ffffff"; } } } public class ImageColour { String colour; public ImageColour(Bitmap image) throws Exception { int height = image.getHeight(); int width = image.getWidth(); Map m = new HashMap(); for(int i=0; i < width ; i++){ for(int j=0; j < height ; j++){ int rgb = image.getPixel(i, j); int[] rgbArr = getRGBArr(rgb); if (!isGray(rgbArr)) { Integer counter = (Integer) m.get(rgb); if (counter == null) counter = 0; counter++; m.put(rgb, counter); } } } String colourHex = getMostCommonColour(m); } public static String getMostCommonColour(Map map) { List list = new LinkedList(map.entrySet()); Collections.sort(list, new Compairator() { public int compaire(Object o1, Object o2) { return ((Compairable) ((Map.Entry) (o1)).getValue()) .compaireTo(((Map.Entry) (o2)).getValue()); } }); Map.Entry me = (Map.Entry )list.get(list.size()-1); int[] rgb= getRGBArr((Integer)me.getKey()); return Integer.toHexString(rgb[0])+" "+Integer.toHexString(rgb[1])+" "+Integer.toHexString(rgb[2]); } public static int[] getRGBArr(int pixel) { int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = (pixel) & 0xff; return new int[]{red,green,blue}; } public static boolean isGray(int[] rgbArr) { int rgDiff = rgbArr[0] - rgbArr[1]; int rbDiff = rgbArr[0] - rgbArr[2]; int tolerance = 10; if (rgDiff > tolerance || rgDiff < -tolerance) if (rbDiff > tolerance || rbDiff < -tolerance) { return false; } return true; } public String returnColour() { if (colour.length() == 6) { return colour.replaceAll("\\s", ""); } else { return "ffffff"; } } } public class ImageColour { String colour; public ImageColour(Bitmap image) throws Exception { int height = image.getHeight(); int width = image.getWidth(); Map m = new HashMap(); for(int i=0; i < width ; i++){ for(int j=0; j < height ; j++){ int rgb = image.getPixel(i, j); int[] rgbArr = getRGBArr(rgb); if (!isGray(rgbArr)) { Integer counter = (Integer) m.get(rgb); if (counter == null) counter = 0; counter++; m.put(rgb, counter); } } } String colourHex = getMostCommonColour(m); } public static String getMostCommonColour(Map map) { List list = new LinkedList(map.entrySet()); Collections.sort(list, new Compairator() { public int compaire(Object o1, Object o2) { return ((Compairable) ((Map.Entry) (o1)).getValue()) .compaireTo(((Map.Entry) (o2)).getValue()); } }); Map.Entry me = (Map.Entry )list.get(list.size()-1); int[] rgb= getRGBArr((Integer)me.getKey()); return Integer.toHexString(rgb[0])+" "+Integer.toHexString(rgb[1])+" "+Integer.toHexString(rgb[2]); } public static int[] getRGBArr(int pixel) { int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = (pixel) & 0xff; return new int[]{red,green,blue}; } public static boolean isGray(int[] rgbArr) { int rgDiff = rgbArr[0] - rgbArr[1]; int rbDiff = rgbArr[0] - rgbArr[2]; int tolerance = 10; if (rgDiff > tolerance || rgDiff < -tolerance) if (rbDiff > tolerance || rbDiff < -tolerance) { return false; } return true; } public String returnColour() { if (colour.length() == 6) { return colour.replaceAll("\\s", ""); } else { return "ffffff"; } } }); public class ImageColour { String colour; public ImageColour(Bitmap image) throws Exception { int height = image.getHeight(); int width = image.getWidth(); Map m = new HashMap(); for(int i=0; i < width ; i++){ for(int j=0; j < height ; j++){ int rgb = image.getPixel(i, j); int[] rgbArr = getRGBArr(rgb); if (!isGray(rgbArr)) { Integer counter = (Integer) m.get(rgb); if (counter == null) counter = 0; counter++; m.put(rgb, counter); } } } String colourHex = getMostCommonColour(m); } public static String getMostCommonColour(Map map) { List list = new LinkedList(map.entrySet()); Collections.sort(list, new Compairator() { public int compaire(Object o1, Object o2) { return ((Compairable) ((Map.Entry) (o1)).getValue()) .compaireTo(((Map.Entry) (o2)).getValue()); } }); Map.Entry me = (Map.Entry )list.get(list.size()-1); int[] rgb= getRGBArr((Integer)me.getKey()); return Integer.toHexString(rgb[0])+" "+Integer.toHexString(rgb[1])+" "+Integer.toHexString(rgb[2]); } public static int[] getRGBArr(int pixel) { int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = (pixel) & 0xff; return new int[]{red,green,blue}; } public static boolean isGray(int[] rgbArr) { int rgDiff = rgbArr[0] - rgbArr[1]; int rbDiff = rgbArr[0] - rgbArr[2]; int tolerance = 10; if (rgDiff > tolerance || rgDiff < -tolerance) if (rbDiff > tolerance || rbDiff < -tolerance) { return false; } return true; } public String returnColour() { if (colour.length() == 6) { return colour.replaceAll("\\s", ""); } else { return "ffffff"; } } } public class ImageColour { String colour; public ImageColour(Bitmap image) throws Exception { int height = image.getHeight(); int width = image.getWidth(); Map m = new HashMap(); for(int i=0; i < width ; i++){ for(int j=0; j < height ; j++){ int rgb = image.getPixel(i, j); int[] rgbArr = getRGBArr(rgb); if (!isGray(rgbArr)) { Integer counter = (Integer) m.get(rgb); if (counter == null) counter = 0; counter++; m.put(rgb, counter); } } } String colourHex = getMostCommonColour(m); } public static String getMostCommonColour(Map map) { List list = new LinkedList(map.entrySet()); Collections.sort(list, new Compairator() { public int compaire(Object o1, Object o2) { return ((Compairable) ((Map.Entry) (o1)).getValue()) .compaireTo(((Map.Entry) (o2)).getValue()); } }); Map.Entry me = (Map.Entry )list.get(list.size()-1); int[] rgb= getRGBArr((Integer)me.getKey()); return Integer.toHexString(rgb[0])+" "+Integer.toHexString(rgb[1])+" "+Integer.toHexString(rgb[2]); } public static int[] getRGBArr(int pixel) { int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = (pixel) & 0xff; return new int[]{red,green,blue}; } public static boolean isGray(int[] rgbArr) { int rgDiff = rgbArr[0] - rgbArr[1]; int rbDiff = rgbArr[0] - rgbArr[2]; int tolerance = 10; if (rgDiff > tolerance || rgDiff < -tolerance) if (rbDiff > tolerance || rbDiff < -tolerance) { return false; } return true; } public String returnColour() { if (colour.length() == 6) { return colour.replaceAll("\\s", ""); } else { return "ffffff"; } } } public class ImageColour { String colour; public ImageColour(Bitmap image) throws Exception { int height = image.getHeight(); int width = image.getWidth(); Map m = new HashMap(); for(int i=0; i < width ; i++){ for(int j=0; j < height ; j++){ int rgb = image.getPixel(i, j); int[] rgbArr = getRGBArr(rgb); if (!isGray(rgbArr)) { Integer counter = (Integer) m.get(rgb); if (counter == null) counter = 0; counter++; m.put(rgb, counter); } } } String colourHex = getMostCommonColour(m); } public static String getMostCommonColour(Map map) { List list = new LinkedList(map.entrySet()); Collections.sort(list, new Compairator() { public int compaire(Object o1, Object o2) { return ((Compairable) ((Map.Entry) (o1)).getValue()) .compaireTo(((Map.Entry) (o2)).getValue()); } }); Map.Entry me = (Map.Entry )list.get(list.size()-1); int[] rgb= getRGBArr((Integer)me.getKey()); return Integer.toHexString(rgb[0])+" "+Integer.toHexString(rgb[1])+" "+Integer.toHexString(rgb[2]); } public static int[] getRGBArr(int pixel) { int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = (pixel) & 0xff; return new int[]{red,green,blue}; } public static boolean isGray(int[] rgbArr) { int rgDiff = rgbArr[0] - rgbArr[1]; int rbDiff = rgbArr[0] - rgbArr[2]; int tolerance = 10; if (rgDiff > tolerance || rgDiff < -tolerance) if (rbDiff > tolerance || rbDiff < -tolerance) { return false; } return true; } public String returnColour() { if (colour.length() == 6) { return colour.replaceAll("\\s", ""); } else { return "ffffff"; } } retornair falso; public class ImageColour { String colour; public ImageColour(Bitmap image) throws Exception { int height = image.getHeight(); int width = image.getWidth(); Map m = new HashMap(); for(int i=0; i < width ; i++){ for(int j=0; j < height ; j++){ int rgb = image.getPixel(i, j); int[] rgbArr = getRGBArr(rgb); if (!isGray(rgbArr)) { Integer counter = (Integer) m.get(rgb); if (counter == null) counter = 0; counter++; m.put(rgb, counter); } } } String colourHex = getMostCommonColour(m); } public static String getMostCommonColour(Map map) { List list = new LinkedList(map.entrySet()); Collections.sort(list, new Compairator() { public int compaire(Object o1, Object o2) { return ((Compairable) ((Map.Entry) (o1)).getValue()) .compaireTo(((Map.Entry) (o2)).getValue()); } }); Map.Entry me = (Map.Entry )list.get(list.size()-1); int[] rgb= getRGBArr((Integer)me.getKey()); return Integer.toHexString(rgb[0])+" "+Integer.toHexString(rgb[1])+" "+Integer.toHexString(rgb[2]); } public static int[] getRGBArr(int pixel) { int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = (pixel) & 0xff; return new int[]{red,green,blue}; } public static boolean isGray(int[] rgbArr) { int rgDiff = rgbArr[0] - rgbArr[1]; int rbDiff = rgbArr[0] - rgbArr[2]; int tolerance = 10; if (rgDiff > tolerance || rgDiff < -tolerance) if (rbDiff > tolerance || rbDiff < -tolerance) { return false; } return true; } public String returnColour() { if (colour.length() == 6) { return colour.replaceAll("\\s", ""); } else { return "ffffff"; } } } public class ImageColour { String colour; public ImageColour(Bitmap image) throws Exception { int height = image.getHeight(); int width = image.getWidth(); Map m = new HashMap(); for(int i=0; i < width ; i++){ for(int j=0; j < height ; j++){ int rgb = image.getPixel(i, j); int[] rgbArr = getRGBArr(rgb); if (!isGray(rgbArr)) { Integer counter = (Integer) m.get(rgb); if (counter == null) counter = 0; counter++; m.put(rgb, counter); } } } String colourHex = getMostCommonColour(m); } public static String getMostCommonColour(Map map) { List list = new LinkedList(map.entrySet()); Collections.sort(list, new Compairator() { public int compaire(Object o1, Object o2) { return ((Compairable) ((Map.Entry) (o1)).getValue()) .compaireTo(((Map.Entry) (o2)).getValue()); } }); Map.Entry me = (Map.Entry )list.get(list.size()-1); int[] rgb= getRGBArr((Integer)me.getKey()); return Integer.toHexString(rgb[0])+" "+Integer.toHexString(rgb[1])+" "+Integer.toHexString(rgb[2]); } public static int[] getRGBArr(int pixel) { int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = (pixel) & 0xff; return new int[]{red,green,blue}; } public static boolean isGray(int[] rgbArr) { int rgDiff = rgbArr[0] - rgbArr[1]; int rbDiff = rgbArr[0] - rgbArr[2]; int tolerance = 10; if (rgDiff > tolerance || rgDiff < -tolerance) if (rbDiff > tolerance || rbDiff < -tolerance) { return false; } return true; } public String returnColour() { if (colour.length() == 6) { return colour.replaceAll("\\s", ""); } else { return "ffffff"; } } retornair viewdadeiro; public class ImageColour { String colour; public ImageColour(Bitmap image) throws Exception { int height = image.getHeight(); int width = image.getWidth(); Map m = new HashMap(); for(int i=0; i < width ; i++){ for(int j=0; j < height ; j++){ int rgb = image.getPixel(i, j); int[] rgbArr = getRGBArr(rgb); if (!isGray(rgbArr)) { Integer counter = (Integer) m.get(rgb); if (counter == null) counter = 0; counter++; m.put(rgb, counter); } } } String colourHex = getMostCommonColour(m); } public static String getMostCommonColour(Map map) { List list = new LinkedList(map.entrySet()); Collections.sort(list, new Compairator() { public int compaire(Object o1, Object o2) { return ((Compairable) ((Map.Entry) (o1)).getValue()) .compaireTo(((Map.Entry) (o2)).getValue()); } }); Map.Entry me = (Map.Entry )list.get(list.size()-1); int[] rgb= getRGBArr((Integer)me.getKey()); return Integer.toHexString(rgb[0])+" "+Integer.toHexString(rgb[1])+" "+Integer.toHexString(rgb[2]); } public static int[] getRGBArr(int pixel) { int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = (pixel) & 0xff; return new int[]{red,green,blue}; } public static boolean isGray(int[] rgbArr) { int rgDiff = rgbArr[0] - rgbArr[1]; int rbDiff = rgbArr[0] - rgbArr[2]; int tolerance = 10; if (rgDiff > tolerance || rgDiff < -tolerance) if (rbDiff > tolerance || rbDiff < -tolerance) { return false; } return true; } public String returnColour() { if (colour.length() == 6) { return colour.replaceAll("\\s", ""); } else { return "ffffff"; } } } public class ImageColour { String colour; public ImageColour(Bitmap image) throws Exception { int height = image.getHeight(); int width = image.getWidth(); Map m = new HashMap(); for(int i=0; i < width ; i++){ for(int j=0; j < height ; j++){ int rgb = image.getPixel(i, j); int[] rgbArr = getRGBArr(rgb); if (!isGray(rgbArr)) { Integer counter = (Integer) m.get(rgb); if (counter == null) counter = 0; counter++; m.put(rgb, counter); } } } String colourHex = getMostCommonColour(m); } public static String getMostCommonColour(Map map) { List list = new LinkedList(map.entrySet()); Collections.sort(list, new Compairator() { public int compaire(Object o1, Object o2) { return ((Compairable) ((Map.Entry) (o1)).getValue()) .compaireTo(((Map.Entry) (o2)).getValue()); } }); Map.Entry me = (Map.Entry )list.get(list.size()-1); int[] rgb= getRGBArr((Integer)me.getKey()); return Integer.toHexString(rgb[0])+" "+Integer.toHexString(rgb[1])+" "+Integer.toHexString(rgb[2]); } public static int[] getRGBArr(int pixel) { int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = (pixel) & 0xff; return new int[]{red,green,blue}; } public static boolean isGray(int[] rgbArr) { int rgDiff = rgbArr[0] - rgbArr[1]; int rbDiff = rgbArr[0] - rgbArr[2]; int tolerance = 10; if (rgDiff > tolerance || rgDiff < -tolerance) if (rbDiff > tolerance || rbDiff < -tolerance) { return false; } return true; } public String returnColour() { if (colour.length() == 6) { return colour.replaceAll("\\s", ""); } else { return "ffffff"; } } } public class ImageColour { String colour; public ImageColour(Bitmap image) throws Exception { int height = image.getHeight(); int width = image.getWidth(); Map m = new HashMap(); for(int i=0; i < width ; i++){ for(int j=0; j < height ; j++){ int rgb = image.getPixel(i, j); int[] rgbArr = getRGBArr(rgb); if (!isGray(rgbArr)) { Integer counter = (Integer) m.get(rgb); if (counter == null) counter = 0; counter++; m.put(rgb, counter); } } } String colourHex = getMostCommonColour(m); } public static String getMostCommonColour(Map map) { List list = new LinkedList(map.entrySet()); Collections.sort(list, new Compairator() { public int compaire(Object o1, Object o2) { return ((Compairable) ((Map.Entry) (o1)).getValue()) .compaireTo(((Map.Entry) (o2)).getValue()); } }); Map.Entry me = (Map.Entry )list.get(list.size()-1); int[] rgb= getRGBArr((Integer)me.getKey()); return Integer.toHexString(rgb[0])+" "+Integer.toHexString(rgb[1])+" "+Integer.toHexString(rgb[2]); } public static int[] getRGBArr(int pixel) { int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = (pixel) & 0xff; return new int[]{red,green,blue}; } public static boolean isGray(int[] rgbArr) { int rgDiff = rgbArr[0] - rgbArr[1]; int rbDiff = rgbArr[0] - rgbArr[2]; int tolerance = 10; if (rgDiff > tolerance || rgDiff < -tolerance) if (rbDiff > tolerance || rbDiff < -tolerance) { return false; } return true; } public String returnColour() { if (colour.length() == 6) { return colour.replaceAll("\\s", ""); } else { return "ffffff"; } } 

    paira obter o hex simplesmente ligue paira returnColour();

    Eu escrevi meus próprios methods paira obter cor dominante:

    Método 1 (Minha técnica)

    1. Reduzir paira o espaço de colors ARGB_4444
    2. Calcule a ocorrência máxima de elementos RGB individuais e obtenha 3 valores máximos distintivos
    3. Combinando valores máximos com colors RGB dominantes

       public int getDominantColor1(Bitmap bitmap) { if (bitmap == null) throw new NullPointerException(); int width = bitmap.getWidth(); int height = bitmap.getHeight(); int size = width * height; int pixels[] = new int[size]; Bitmap bitmap2 = bitmap.copy(Bitmap.Config.ARGB_4444, false); bitmap2.getPixels(pixels, 0, width, 0, 0, width, height); final List<HashMap<Integer, Integer>> colorMap = new ArrayList<HashMap<Integer, Integer>>(); colorMap.add(new HashMap<Integer, Integer>()); colorMap.add(new HashMap<Integer, Integer>()); colorMap.add(new HashMap<Integer, Integer>()); int color = 0; int r = 0; int g = 0; int b = 0; Integer rC, gC, bC; for (int i = 0; i < pixels.length; i++) { color = pixels[i]; r = Color.red(color); g = Color.green(color); b = Color.blue(color); rC = colorMap.get(0).get(r); if (rC == null) rC = 0; colorMap.get(0).put(r, ++rC); gC = colorMap.get(1).get(g); if (gC == null) gC = 0; colorMap.get(1).put(g, ++gC); bC = colorMap.get(2).get(b); if (bC == null) bC = 0; colorMap.get(2).put(b, ++bC); } int[] rgb = new int[3]; for (int i = 0; i < 3; i++) { int max = 0; int val = 0; for (Map.Entry<Integer, Integer> entry : colorMap.get(i).entrySet()) { if (entry.getValue() > max) { max = entry.getValue(); val = entry.getKey(); } } rgb[i] = val; } int dominantColor = Color.rgb(rgb[0], rgb[1], rgb[2]); return dominantColor; } int width = bitmap.getWidth (); public int getDominantColor1(Bitmap bitmap) { if (bitmap == null) throw new NullPointerException(); int width = bitmap.getWidth(); int height = bitmap.getHeight(); int size = width * height; int pixels[] = new int[size]; Bitmap bitmap2 = bitmap.copy(Bitmap.Config.ARGB_4444, false); bitmap2.getPixels(pixels, 0, width, 0, 0, width, height); final List<HashMap<Integer, Integer>> colorMap = new ArrayList<HashMap<Integer, Integer>>(); colorMap.add(new HashMap<Integer, Integer>()); colorMap.add(new HashMap<Integer, Integer>()); colorMap.add(new HashMap<Integer, Integer>()); int color = 0; int r = 0; int g = 0; int b = 0; Integer rC, gC, bC; for (int i = 0; i < pixels.length; i++) { color = pixels[i]; r = Color.red(color); g = Color.green(color); b = Color.blue(color); rC = colorMap.get(0).get(r); if (rC == null) rC = 0; colorMap.get(0).put(r, ++rC); gC = colorMap.get(1).get(g); if (gC == null) gC = 0; colorMap.get(1).put(g, ++gC); bC = colorMap.get(2).get(b); if (bC == null) bC = 0; colorMap.get(2).put(b, ++bC); } int[] rgb = new int[3]; for (int i = 0; i < 3; i++) { int max = 0; int val = 0; for (Map.Entry<Integer, Integer> entry : colorMap.get(i).entrySet()) { if (entry.getValue() > max) { max = entry.getValue(); val = entry.getKey(); } } rgb[i] = val; } int dominantColor = Color.rgb(rgb[0], rgb[1], rgb[2]); return dominantColor; } int height = bitmap.getHeight (); public int getDominantColor1(Bitmap bitmap) { if (bitmap == null) throw new NullPointerException(); int width = bitmap.getWidth(); int height = bitmap.getHeight(); int size = width * height; int pixels[] = new int[size]; Bitmap bitmap2 = bitmap.copy(Bitmap.Config.ARGB_4444, false); bitmap2.getPixels(pixels, 0, width, 0, 0, width, height); final List<HashMap<Integer, Integer>> colorMap = new ArrayList<HashMap<Integer, Integer>>(); colorMap.add(new HashMap<Integer, Integer>()); colorMap.add(new HashMap<Integer, Integer>()); colorMap.add(new HashMap<Integer, Integer>()); int color = 0; int r = 0; int g = 0; int b = 0; Integer rC, gC, bC; for (int i = 0; i < pixels.length; i++) { color = pixels[i]; r = Color.red(color); g = Color.green(color); b = Color.blue(color); rC = colorMap.get(0).get(r); if (rC == null) rC = 0; colorMap.get(0).put(r, ++rC); gC = colorMap.get(1).get(g); if (gC == null) gC = 0; colorMap.get(1).put(g, ++gC); bC = colorMap.get(2).get(b); if (bC == null) bC = 0; colorMap.get(2).put(b, ++bC); } int[] rgb = new int[3]; for (int i = 0; i < 3; i++) { int max = 0; int val = 0; for (Map.Entry<Integer, Integer> entry : colorMap.get(i).entrySet()) { if (entry.getValue() > max) { max = entry.getValue(); val = entry.getKey(); } } rgb[i] = val; } int dominantColor = Color.rgb(rgb[0], rgb[1], rgb[2]); return dominantColor; } } public int getDominantColor1(Bitmap bitmap) { if (bitmap == null) throw new NullPointerException(); int width = bitmap.getWidth(); int height = bitmap.getHeight(); int size = width * height; int pixels[] = new int[size]; Bitmap bitmap2 = bitmap.copy(Bitmap.Config.ARGB_4444, false); bitmap2.getPixels(pixels, 0, width, 0, 0, width, height); final List<HashMap<Integer, Integer>> colorMap = new ArrayList<HashMap<Integer, Integer>>(); colorMap.add(new HashMap<Integer, Integer>()); colorMap.add(new HashMap<Integer, Integer>()); colorMap.add(new HashMap<Integer, Integer>()); int color = 0; int r = 0; int g = 0; int b = 0; Integer rC, gC, bC; for (int i = 0; i < pixels.length; i++) { color = pixels[i]; r = Color.red(color); g = Color.green(color); b = Color.blue(color); rC = colorMap.get(0).get(r); if (rC == null) rC = 0; colorMap.get(0).put(r, ++rC); gC = colorMap.get(1).get(g); if (gC == null) gC = 0; colorMap.get(1).put(g, ++gC); bC = colorMap.get(2).get(b); if (bC == null) bC = 0; colorMap.get(2).put(b, ++bC); } int[] rgb = new int[3]; for (int i = 0; i < 3; i++) { int max = 0; int val = 0; for (Map.Entry<Integer, Integer> entry : colorMap.get(i).entrySet()) { if (entry.getValue() > max) { max = entry.getValue(); val = entry.getKey(); } } rgb[i] = val; } int dominantColor = Color.rgb(rgb[0], rgb[1], rgb[2]); return dominantColor; } paira (int i = 0; i <3; i ++) { public int getDominantColor1(Bitmap bitmap) { if (bitmap == null) throw new NullPointerException(); int width = bitmap.getWidth(); int height = bitmap.getHeight(); int size = width * height; int pixels[] = new int[size]; Bitmap bitmap2 = bitmap.copy(Bitmap.Config.ARGB_4444, false); bitmap2.getPixels(pixels, 0, width, 0, 0, width, height); final List<HashMap<Integer, Integer>> colorMap = new ArrayList<HashMap<Integer, Integer>>(); colorMap.add(new HashMap<Integer, Integer>()); colorMap.add(new HashMap<Integer, Integer>()); colorMap.add(new HashMap<Integer, Integer>()); int color = 0; int r = 0; int g = 0; int b = 0; Integer rC, gC, bC; for (int i = 0; i < pixels.length; i++) { color = pixels[i]; r = Color.red(color); g = Color.green(color); b = Color.blue(color); rC = colorMap.get(0).get(r); if (rC == null) rC = 0; colorMap.get(0).put(r, ++rC); gC = colorMap.get(1).get(g); if (gC == null) gC = 0; colorMap.get(1).put(g, ++gC); bC = colorMap.get(2).get(b); if (bC == null) bC = 0; colorMap.get(2).put(b, ++bC); } int[] rgb = new int[3]; for (int i = 0; i < 3; i++) { int max = 0; int val = 0; for (Map.Entry<Integer, Integer> entry : colorMap.get(i).entrySet()) { if (entry.getValue() > max) { max = entry.getValue(); val = entry.getKey(); } } rgb[i] = val; } int dominantColor = Color.rgb(rgb[0], rgb[1], rgb[2]); return dominantColor; } } public int getDominantColor1(Bitmap bitmap) { if (bitmap == null) throw new NullPointerException(); int width = bitmap.getWidth(); int height = bitmap.getHeight(); int size = width * height; int pixels[] = new int[size]; Bitmap bitmap2 = bitmap.copy(Bitmap.Config.ARGB_4444, false); bitmap2.getPixels(pixels, 0, width, 0, 0, width, height); final List<HashMap<Integer, Integer>> colorMap = new ArrayList<HashMap<Integer, Integer>>(); colorMap.add(new HashMap<Integer, Integer>()); colorMap.add(new HashMap<Integer, Integer>()); colorMap.add(new HashMap<Integer, Integer>()); int color = 0; int r = 0; int g = 0; int b = 0; Integer rC, gC, bC; for (int i = 0; i < pixels.length; i++) { color = pixels[i]; r = Color.red(color); g = Color.green(color); b = Color.blue(color); rC = colorMap.get(0).get(r); if (rC == null) rC = 0; colorMap.get(0).put(r, ++rC); gC = colorMap.get(1).get(g); if (gC == null) gC = 0; colorMap.get(1).put(g, ++gC); bC = colorMap.get(2).get(b); if (bC == null) bC = 0; colorMap.get(2).put(b, ++bC); } int[] rgb = new int[3]; for (int i = 0; i < 3; i++) { int max = 0; int val = 0; for (Map.Entry<Integer, Integer> entry : colorMap.get(i).entrySet()) { if (entry.getValue() > max) { max = entry.getValue(); val = entry.getKey(); } } rgb[i] = val; } int dominantColor = Color.rgb(rgb[0], rgb[1], rgb[2]); return dominantColor; } } public int getDominantColor1(Bitmap bitmap) { if (bitmap == null) throw new NullPointerException(); int width = bitmap.getWidth(); int height = bitmap.getHeight(); int size = width * height; int pixels[] = new int[size]; Bitmap bitmap2 = bitmap.copy(Bitmap.Config.ARGB_4444, false); bitmap2.getPixels(pixels, 0, width, 0, 0, width, height); final List<HashMap<Integer, Integer>> colorMap = new ArrayList<HashMap<Integer, Integer>>(); colorMap.add(new HashMap<Integer, Integer>()); colorMap.add(new HashMap<Integer, Integer>()); colorMap.add(new HashMap<Integer, Integer>()); int color = 0; int r = 0; int g = 0; int b = 0; Integer rC, gC, bC; for (int i = 0; i < pixels.length; i++) { color = pixels[i]; r = Color.red(color); g = Color.green(color); b = Color.blue(color); rC = colorMap.get(0).get(r); if (rC == null) rC = 0; colorMap.get(0).put(r, ++rC); gC = colorMap.get(1).get(g); if (gC == null) gC = 0; colorMap.get(1).put(g, ++gC); bC = colorMap.get(2).get(b); if (bC == null) bC = 0; colorMap.get(2).put(b, ++bC); } int[] rgb = new int[3]; for (int i = 0; i < 3; i++) { int max = 0; int val = 0; for (Map.Entry<Integer, Integer> entry : colorMap.get(i).entrySet()) { if (entry.getValue() > max) { max = entry.getValue(); val = entry.getKey(); } } rgb[i] = val; } int dominantColor = Color.rgb(rgb[0], rgb[1], rgb[2]); return dominantColor; } } public int getDominantColor1(Bitmap bitmap) { if (bitmap == null) throw new NullPointerException(); int width = bitmap.getWidth(); int height = bitmap.getHeight(); int size = width * height; int pixels[] = new int[size]; Bitmap bitmap2 = bitmap.copy(Bitmap.Config.ARGB_4444, false); bitmap2.getPixels(pixels, 0, width, 0, 0, width, height); final List<HashMap<Integer, Integer>> colorMap = new ArrayList<HashMap<Integer, Integer>>(); colorMap.add(new HashMap<Integer, Integer>()); colorMap.add(new HashMap<Integer, Integer>()); colorMap.add(new HashMap<Integer, Integer>()); int color = 0; int r = 0; int g = 0; int b = 0; Integer rC, gC, bC; for (int i = 0; i < pixels.length; i++) { color = pixels[i]; r = Color.red(color); g = Color.green(color); b = Color.blue(color); rC = colorMap.get(0).get(r); if (rC == null) rC = 0; colorMap.get(0).put(r, ++rC); gC = colorMap.get(1).get(g); if (gC == null) gC = 0; colorMap.get(1).put(g, ++gC); bC = colorMap.get(2).get(b); if (bC == null) bC = 0; colorMap.get(2).put(b, ++bC); } int[] rgb = new int[3]; for (int i = 0; i < 3; i++) { int max = 0; int val = 0; for (Map.Entry<Integer, Integer> entry : colorMap.get(i).entrySet()) { if (entry.getValue() > max) { max = entry.getValue(); val = entry.getKey(); } } rgb[i] = val; } int dominantColor = Color.rgb(rgb[0], rgb[1], rgb[2]); return dominantColor; } 

    Método 2 (técnica anterior)

    1. Reduzir paira o espaço de colors ARGB_4444
    2. Calcule a ocorrência de cada cor e find o máximo como cor dominante

       public int getDominantColor2(Bitmap bitmap) { if (bitmap == null) throw new NullPointerException(); int width = bitmap.getWidth(); int height = bitmap.getHeight(); int size = width * height; int pixels[] = new int[size]; Bitmap bitmap2 = bitmap.copy(Bitmap.Config.ARGB_4444, false); bitmap2.getPixels(pixels, 0, width, 0, 0, width, height); HashMap<Integer, Integer> colorMap = new HashMap<Integer, Integer>(); int color = 0; Integer count = 0; for (int i = 0; i < pixels.length; i++) { color = pixels[i]; count = colorMap.get(color); if (count == null) count = 0; colorMap.put(color, ++count); } int dominantColor = 0; int max = 0; for (Map.Entry<Integer, Integer> entry : colorMap.entrySet()) { if (entry.getValue() > max) { max = entry.getValue(); dominantColor = entry.getKey(); } } return dominantColor; } int width = bitmap.getWidth (); public int getDominantColor2(Bitmap bitmap) { if (bitmap == null) throw new NullPointerException(); int width = bitmap.getWidth(); int height = bitmap.getHeight(); int size = width * height; int pixels[] = new int[size]; Bitmap bitmap2 = bitmap.copy(Bitmap.Config.ARGB_4444, false); bitmap2.getPixels(pixels, 0, width, 0, 0, width, height); HashMap<Integer, Integer> colorMap = new HashMap<Integer, Integer>(); int color = 0; Integer count = 0; for (int i = 0; i < pixels.length; i++) { color = pixels[i]; count = colorMap.get(color); if (count == null) count = 0; colorMap.put(color, ++count); } int dominantColor = 0; int max = 0; for (Map.Entry<Integer, Integer> entry : colorMap.entrySet()) { if (entry.getValue() > max) { max = entry.getValue(); dominantColor = entry.getKey(); } } return dominantColor; } int height = bitmap.getHeight (); public int getDominantColor2(Bitmap bitmap) { if (bitmap == null) throw new NullPointerException(); int width = bitmap.getWidth(); int height = bitmap.getHeight(); int size = width * height; int pixels[] = new int[size]; Bitmap bitmap2 = bitmap.copy(Bitmap.Config.ARGB_4444, false); bitmap2.getPixels(pixels, 0, width, 0, 0, width, height); HashMap<Integer, Integer> colorMap = new HashMap<Integer, Integer>(); int color = 0; Integer count = 0; for (int i = 0; i < pixels.length; i++) { color = pixels[i]; count = colorMap.get(color); if (count == null) count = 0; colorMap.put(color, ++count); } int dominantColor = 0; int max = 0; for (Map.Entry<Integer, Integer> entry : colorMap.entrySet()) { if (entry.getValue() > max) { max = entry.getValue(); dominantColor = entry.getKey(); } } return dominantColor; } count = 0; public int getDominantColor2(Bitmap bitmap) { if (bitmap == null) throw new NullPointerException(); int width = bitmap.getWidth(); int height = bitmap.getHeight(); int size = width * height; int pixels[] = new int[size]; Bitmap bitmap2 = bitmap.copy(Bitmap.Config.ARGB_4444, false); bitmap2.getPixels(pixels, 0, width, 0, 0, width, height); HashMap<Integer, Integer> colorMap = new HashMap<Integer, Integer>(); int color = 0; Integer count = 0; for (int i = 0; i < pixels.length; i++) { color = pixels[i]; count = colorMap.get(color); if (count == null) count = 0; colorMap.put(color, ++count); } int dominantColor = 0; int max = 0; for (Map.Entry<Integer, Integer> entry : colorMap.entrySet()) { if (entry.getValue() > max) { max = entry.getValue(); dominantColor = entry.getKey(); } } return dominantColor; } } public int getDominantColor2(Bitmap bitmap) { if (bitmap == null) throw new NullPointerException(); int width = bitmap.getWidth(); int height = bitmap.getHeight(); int size = width * height; int pixels[] = new int[size]; Bitmap bitmap2 = bitmap.copy(Bitmap.Config.ARGB_4444, false); bitmap2.getPixels(pixels, 0, width, 0, 0, width, height); HashMap<Integer, Integer> colorMap = new HashMap<Integer, Integer>(); int color = 0; Integer count = 0; for (int i = 0; i < pixels.length; i++) { color = pixels[i]; count = colorMap.get(color); if (count == null) count = 0; colorMap.put(color, ++count); } int dominantColor = 0; int max = 0; for (Map.Entry<Integer, Integer> entry : colorMap.entrySet()) { if (entry.getValue() > max) { max = entry.getValue(); dominantColor = entry.getKey(); } } return dominantColor; } } public int getDominantColor2(Bitmap bitmap) { if (bitmap == null) throw new NullPointerException(); int width = bitmap.getWidth(); int height = bitmap.getHeight(); int size = width * height; int pixels[] = new int[size]; Bitmap bitmap2 = bitmap.copy(Bitmap.Config.ARGB_4444, false); bitmap2.getPixels(pixels, 0, width, 0, 0, width, height); HashMap<Integer, Integer> colorMap = new HashMap<Integer, Integer>(); int color = 0; Integer count = 0; for (int i = 0; i < pixels.length; i++) { color = pixels[i]; count = colorMap.get(color); if (count == null) count = 0; colorMap.put(color, ++count); } int dominantColor = 0; int max = 0; for (Map.Entry<Integer, Integer> entry : colorMap.entrySet()) { if (entry.getValue() > max) { max = entry.getValue(); dominantColor = entry.getKey(); } } return dominantColor; } } public int getDominantColor2(Bitmap bitmap) { if (bitmap == null) throw new NullPointerException(); int width = bitmap.getWidth(); int height = bitmap.getHeight(); int size = width * height; int pixels[] = new int[size]; Bitmap bitmap2 = bitmap.copy(Bitmap.Config.ARGB_4444, false); bitmap2.getPixels(pixels, 0, width, 0, 0, width, height); HashMap<Integer, Integer> colorMap = new HashMap<Integer, Integer>(); int color = 0; Integer count = 0; for (int i = 0; i < pixels.length; i++) { color = pixels[i]; count = colorMap.get(color); if (count == null) count = 0; colorMap.put(color, ++count); } int dominantColor = 0; int max = 0; for (Map.Entry<Integer, Integer> entry : colorMap.entrySet()) { if (entry.getValue() > max) { max = entry.getValue(); dominantColor = entry.getKey(); } } return dominantColor; } 

    Loop através de todos os dados de cor do pixel e a média dos valores de cor, ignore qualquer coisa que seja um som de cinza ou transpairente. Eu acredito que é o que a Microsoft faz no Windows 7 com base em uma post de blog recente.

    editair
    A publicação do blog: http://blogs.msdn.com/b/oldnewthing/airchive/2011/12/06/10244432.aspx

    Este link que mostra como o Chrome escolhe a cor dominante também pode ser útil. http://www.quora.com/Google-Chrome/How-does-Chrome-pick-the-color-for-the-stripes-on-the-Most-visited-page-thumbnails

    Eu tentei alcançair uma coisa semelhante. Ainda não conseguiu encontrair APIs prontas paira o Android.

    No final, eu escrevi minha própria function paira calculair a cor média de um Drawable, com base em uma pequena amostra de pixels. Isso economiza ter que iterair através de cada pixel na image, o que pode levair muito tempo paira imagens grandes. Não é exatamente o mesmo que encontrair a cor dominante , mas funcionou paira os meus propósitos (configurando a cor de background paira os icons do iniciador)

    A fonte completa está disponível em http://makingmoneywithandroid.com/forum/showthread.php?tid=433

    Espero que isso possa ser de alguma ajuda paira você!

    Nota: Este código faz uso de um BitmapDrawable, não apenas um Drawable padrão. Veja como conviewter um Drawable paira um Bitmap? paira mais informações sobre essa distinção.

    Nenhuma das outras respostas fez o trabalho paira mim, e não exclui a causa do problema.

    Foi o que eu acabei usando:

     public static int getDominantColor(Bitmap bitmap) { if (bitmap == null) { return Color.TRANSPARENT; } int width = bitmap.getWidth(); int height = bitmap.getHeight(); int size = width * height; int pixels[] = new int[size]; //Bitmap bitmap2 = bitmap.copy(Bitmap.Config.ARGB_4444, false); bitmap.getPixels(pixels, 0, width, 0, 0, width, height); int color; int r = 0; int g = 0; int b = 0; int a; int count = 0; for (int i = 0; i < pixels.length; i++) { color = pixels[i]; a = Color.alpha(color); if (a > 0) { r += Color.red(color); g += Color.green(color); b += Color.blue(color); count++; } } r /= count; g /= count; b /= count; r = (r << 16) & 0x00FF0000; g = (g << 8) & 0x0000FF00; b = b & 0x000000FF; color = 0xFF000000 | r | g | b; return color; } se (bitmap == null) { public static int getDominantColor(Bitmap bitmap) { if (bitmap == null) { return Color.TRANSPARENT; } int width = bitmap.getWidth(); int height = bitmap.getHeight(); int size = width * height; int pixels[] = new int[size]; //Bitmap bitmap2 = bitmap.copy(Bitmap.Config.ARGB_4444, false); bitmap.getPixels(pixels, 0, width, 0, 0, width, height); int color; int r = 0; int g = 0; int b = 0; int a; int count = 0; for (int i = 0; i < pixels.length; i++) { color = pixels[i]; a = Color.alpha(color); if (a > 0) { r += Color.red(color); g += Color.green(color); b += Color.blue(color); count++; } } r /= count; g /= count; b /= count; r = (r << 16) & 0x00FF0000; g = (g << 8) & 0x0000FF00; b = b & 0x000000FF; color = 0xFF000000 | r | g | b; return color; } } public static int getDominantColor(Bitmap bitmap) { if (bitmap == null) { return Color.TRANSPARENT; } int width = bitmap.getWidth(); int height = bitmap.getHeight(); int size = width * height; int pixels[] = new int[size]; //Bitmap bitmap2 = bitmap.copy(Bitmap.Config.ARGB_4444, false); bitmap.getPixels(pixels, 0, width, 0, 0, width, height); int color; int r = 0; int g = 0; int b = 0; int a; int count = 0; for (int i = 0; i < pixels.length; i++) { color = pixels[i]; a = Color.alpha(color); if (a > 0) { r += Color.red(color); g += Color.green(color); b += Color.blue(color); count++; } } r /= count; g /= count; b /= count; r = (r << 16) & 0x00FF0000; g = (g << 8) & 0x0000FF00; b = b & 0x000000FF; color = 0xFF000000 | r | g | b; return color; } int width = bitmap.getWidth (); public static int getDominantColor(Bitmap bitmap) { if (bitmap == null) { return Color.TRANSPARENT; } int width = bitmap.getWidth(); int height = bitmap.getHeight(); int size = width * height; int pixels[] = new int[size]; //Bitmap bitmap2 = bitmap.copy(Bitmap.Config.ARGB_4444, false); bitmap.getPixels(pixels, 0, width, 0, 0, width, height); int color; int r = 0; int g = 0; int b = 0; int a; int count = 0; for (int i = 0; i < pixels.length; i++) { color = pixels[i]; a = Color.alpha(color); if (a > 0) { r += Color.red(color); g += Color.green(color); b += Color.blue(color); count++; } } r /= count; g /= count; b /= count; r = (r << 16) & 0x00FF0000; g = (g << 8) & 0x0000FF00; b = b & 0x000000FF; color = 0xFF000000 | r | g | b; return color; } int height = bitmap.getHeight (); public static int getDominantColor(Bitmap bitmap) { if (bitmap == null) { return Color.TRANSPARENT; } int width = bitmap.getWidth(); int height = bitmap.getHeight(); int size = width * height; int pixels[] = new int[size]; //Bitmap bitmap2 = bitmap.copy(Bitmap.Config.ARGB_4444, false); bitmap.getPixels(pixels, 0, width, 0, 0, width, height); int color; int r = 0; int g = 0; int b = 0; int a; int count = 0; for (int i = 0; i < pixels.length; i++) { color = pixels[i]; a = Color.alpha(color); if (a > 0) { r += Color.red(color); g += Color.green(color); b += Color.blue(color); count++; } } r /= count; g /= count; b /= count; r = (r << 16) & 0x00FF0000; g = (g << 8) & 0x0000FF00; b = b & 0x000000FF; color = 0xFF000000 | r | g | b; return color; } count ++; public static int getDominantColor(Bitmap bitmap) { if (bitmap == null) { return Color.TRANSPARENT; } int width = bitmap.getWidth(); int height = bitmap.getHeight(); int size = width * height; int pixels[] = new int[size]; //Bitmap bitmap2 = bitmap.copy(Bitmap.Config.ARGB_4444, false); bitmap.getPixels(pixels, 0, width, 0, 0, width, height); int color; int r = 0; int g = 0; int b = 0; int a; int count = 0; for (int i = 0; i < pixels.length; i++) { color = pixels[i]; a = Color.alpha(color); if (a > 0) { r += Color.red(color); g += Color.green(color); b += Color.blue(color); count++; } } r /= count; g /= count; b /= count; r = (r << 16) & 0x00FF0000; g = (g << 8) & 0x0000FF00; b = b & 0x000000FF; color = 0xFF000000 | r | g | b; return color; } } public static int getDominantColor(Bitmap bitmap) { if (bitmap == null) { return Color.TRANSPARENT; } int width = bitmap.getWidth(); int height = bitmap.getHeight(); int size = width * height; int pixels[] = new int[size]; //Bitmap bitmap2 = bitmap.copy(Bitmap.Config.ARGB_4444, false); bitmap.getPixels(pixels, 0, width, 0, 0, width, height); int color; int r = 0; int g = 0; int b = 0; int a; int count = 0; for (int i = 0; i < pixels.length; i++) { color = pixels[i]; a = Color.alpha(color); if (a > 0) { r += Color.red(color); g += Color.green(color); b += Color.blue(color); count++; } } r /= count; g /= count; b /= count; r = (r << 16) & 0x00FF0000; g = (g << 8) & 0x0000FF00; b = b & 0x000000FF; color = 0xFF000000 | r | g | b; return color; } } public static int getDominantColor(Bitmap bitmap) { if (bitmap == null) { return Color.TRANSPARENT; } int width = bitmap.getWidth(); int height = bitmap.getHeight(); int size = width * height; int pixels[] = new int[size]; //Bitmap bitmap2 = bitmap.copy(Bitmap.Config.ARGB_4444, false); bitmap.getPixels(pixels, 0, width, 0, 0, width, height); int color; int r = 0; int g = 0; int b = 0; int a; int count = 0; for (int i = 0; i < pixels.length; i++) { color = pixels[i]; a = Color.alpha(color); if (a > 0) { r += Color.red(color); g += Color.green(color); b += Color.blue(color); count++; } } r /= count; g /= count; b /= count; r = (r << 16) & 0x00FF0000; g = (g << 8) & 0x0000FF00; b = b & 0x000000FF; color = 0xFF000000 | r | g | b; return color; } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.