如果您使用Windows 7,则可以理解为什么我要查找图像中的主色。将鼠标悬停在任务栏上的程序后,该特定程序会根据图标中最主要的颜色进行更改。我注意到了其他程序中使用的这种技术,但无法记住它们。查找Android中可支配图像的主色颜色
我可以看到这在我用来开发应用程序的许多UI技术中很有用,而且我想知道如何从Android可绘制资源中找到最常见的颜色。
如果您使用Windows 7,则可以理解为什么我要查找图像中的主色。将鼠标悬停在任务栏上的程序后,该特定程序会根据图标中最主要的颜色进行更改。我注意到了其他程序中使用的这种技术,但无法记住它们。查找Android中可支配图像的主色颜色
我可以看到这在我用来开发应用程序的许多UI技术中很有用,而且我想知道如何从Android可绘制资源中找到最常见的颜色。
在Android中5.0棒棒糖,加入类,以帮助提取有用颜色的位图。 0123.类,在android.support.v7中找到。图形,可以提取颜色如下:
这个Android培训页面提供您需要使用该类的所有详细信息(我在Android Studio中自己尝试过这是非常简单):http://developer.android.com/training/material/drawables.html#ColorExtract
引述:
Android的支持库R21以上包括Palette 类,它可以让你从图像中提取突出的颜色。若要 提取这些颜色,请在加载图像的后台线程中将Bitmap对象传递给Palette.generate() 静态方法。如果 你不能使用线程,调用Palette.generateAsync()方法和 提供一个监听器来代替。*
可以使用的getter方法 在Palette类检索图像最突出的颜色,这样的作为Palette.getVibrantColor。
要使用调色板类在你的项目中,添加以下摇篮 依赖于你的应用程序的模块:
dependencies { ... compile 'com.android.support:palette-v7:21.0.+' }
*如果你需要使用generateAsync(),方法如下:
Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() {
public void onGenerated(Palette palette) {
// Do something with colors...
}
});
编辑: 由于问题问如何从可绘制资源中提取颜色,您首先必须将drawable转换为位图才能使用我描述的技巧。幸运的是,这是一个使用BitmapFactory很简单:
Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
R.drawable.icon_resource);`
如果您想试用Palette类提取的颜色,请在Play商店中查看我的应用:https://play.google.com/store/apps/details?id = com.tonyw.sampleapps.palettecolorextraction。你可以在GitHub上找到它的源代码:https://github.com/tony-w/PaletteColorExtraction –
优秀的答案。这使得大多数可用的库和方法变得冗余。 – Baz
我认为这将是最好的解释一切,这样会更清晰...... –
循环遍历所有像素的颜色数据并平均颜色值,忽略任何灰色或透明的阴影。我相信这是微软在Windows 7基于最近的博客文章做的。
编辑
的博客文章:http://blogs.msdn.com/b/oldnewthing/archive/2011/12/06/10244432.aspx
此链接显示Chrome浏览器如何挑选主色也可能会有所帮助。 http://www.quora.com/Google-Chrome/How-does-Chrome-pick-the-color-for-the-stripes-on-the-Most-visited-page-thumbnails
我希望有一个API函数深埋在某个地方。这是很好的信息 – styler1972
我发现了一个简单的黑客:复制为1x1位图并获取颜色:http://aerilys.fr/blog/?p = 1341 – radley
该类迭代通过位图并返回最主要的颜色。 如有必要,请随时清理代码。
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 Comparator() {
public int compare(Object o1, Object o2) {
return ((Comparable) ((Map.Entry) (o1)).getValue())
.compareTo(((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";
}
}
得到六角只需拨打 returnColour();
我建议谁使用这种方法来玩容忍变量。根据您设置的值,算法运行得更快或更慢。 –
我写我自己的方法来获取主色:
方法1(我的技术)
ARGB_4444
色彩空间最大值结合到显性RGB颜色
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;
}
方法2(旧法)
ARGB_4444
色彩空间计算每种颜色的出现,并找到最大的一个作为主色
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;
}
问题:什么是bitmap2?从原始位图复制后,您似乎没有使用它。 –
还有一个另一种解决方案,它更近似的,但如果你不希望有用于搜索的颜色长时间的延迟,它可以做的工作。
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;
}
这是一个非常有用的答案。我正在使用循环方法,但这是使用户界面生涩。现在非常流畅。 –
尽管代码中存在语法错误。 –
从支持:palette-v7:25.0.0开始,这不再需要自定义方法 - 只需使用palette.getDominantSwatch()即可。 –
没有其他的答案为我做了这份工作,我也没有排除问题的原因。
这是我最终使用:用棒棒糖,有助于从位图中提取突出的颜色加入
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;
}
一个新的API。有关详细信息,请参见[我的答案](http://stackoverflow.com/a/28145358/1956632)。由于提到的Palette类位于support7库中,因此它应该可以在旧版本的Android中使用。 –
安卓v7调色板支持库为我们做。所有寻找演示http://code2concept.blogspot.in/2015/10/android-support-v7-palette-demo.html – nitesh