At the last weekly meeting, the experts introduced some of the new features of android5.0. One of them is the main color extraction, according to the product manager, we'd better add this feature to the next phase of demand. So we started to look at this new feature. This is the new interface placed in the support package, which means that this interface is not related to the Android version. It seems that the source code is not too big. :-)
Let's take a look at Google's official notes. Https://developer.android.com/reference/android/support/v7/graphics/Palette.html from this introduction page, we can generally understand: This new interface is relatively simple. A main construction method, passing a bitmap object, the rest is the Get series method. In addition, for ease of use, Google also deliberately added a new asynchronous construction method, similar to Imageloader. But here is a friendly reminder, the following methods, the return value is likely to be empty, so before use to do a parameter validity judgment.
/** * Returns The most vibrant swatch in the palette. Might is null. */Public Swatch Getvibrantswatch () {return mvibrantswatch; }/** * Returns a light and vibrant swatch from the palette. Might is null. */Public Swatch Getlightvibrantswatch () {return mlightvibrantswatch; }/** * Returns a dark and vibrant swatch from the palette. Might is null. */Public Swatch Getdarkvibrantswatch () {return mdarkvibrantswatch; }/** * Returns a muted swatch from the palette. Might is null. */Public Swatch Getmutedswatch () {return mmutedswatch; }/** * Returns a muted and light swatch from the palette. Might is null. */Public Swatch Getlightmutedswatch () {return mlightmutedcolor; }/** * Returns a muted and dark swatch from the palette. Might is null. */Public Swatch Getdarkmutedswatch () {return mdarkmutedswatch; }
supposedly, such a good interface should be able to get started quickly. However, in the demo, found that in many cases, this interface does not take what we want, that is, we think this attached to the image of the most color. For example, a picture that looks like a gray one at a glance, returns to be red, or yellow. The color taken from the picture is no longer within the normal comprehension range. It seems that we have to study the source code.
with this feature open interface, the implementation of this feature source code is not too complex. The package android.support.v7.graphics has a total of four files, one of which is the public class. Of the remaining three classes, two class is the capability layer code. As follows:
Palette.java external interface class. Provides access to some methods, as well as the definition of Swatch.
Colorutils.java tool classes that are strongly related to the business. There are some static methods inside. The
Colorhistogram.java implements the ability class for the color extraction algorithm. The
Colorcutquantizer.java a wrapper class sandwiched between Colorhistogram.java and Palette.java. But this class provides a set of color splitting algorithms.
Here, let's take a look at how colorhistogram this class implements the main color extraction. The construction method contains an array of pixel-point color values. Once passed in, all color values are sorted once. The type of color is given in the Countdistinctcolors method, and the ratio/number of each color is given in the Countfrequencies method. This algorithm is very common, the first encounter with the classmate of Google's comments look very fast.
private static int countdistinctcolors (final int[] pixels) {if (Pixels.length < 2) {//if we ha ve less than 2 pixels we can stop here return pixels.length; }//If We have at least 2 pixels and we have a minimum of 1 color ... int colorcount = 1; int currentcolor = pixels[0]; Now iterate from the second pixel to the end, counting distinct colors for (int i = 1; i < pixels.length; i++ ) {//If we encounter a new color, increase the population If (pixels[i]! = CurrentColor) { CurrentColor = Pixels[i]; colorcount++; }} return colorcount; } private void Countfrequencies (final int[] pixels) {if (pixels.length = = 0) {return; } int currentcolorindex = 0; int currentcolor = pixels[0]; Mcolors[currentcolorindex] = CurrentColor; Mcolorcounts[currentcolorindex] = 1; if (piXels.length = = 1) {//If we are only having one pixel, we can stop here return; }//Now iterate from the second pixel to the end, population distinct colors for (int i = 1; i < pixels. Length i++) {if (pixels[i] = = CurrentColor) {//We ' ve hit the same color as before, increase Populati On mcolorcounts[currentcolorindex]++; } else {//We ' ve hit a new color, increase index currentcolor = pixels[i]; currentcolorindex++; Mcolors[currentcolorindex] = CurrentColor; Mcolorcounts[currentcolorindex] = 1; } } }Everything is normal here. The extracted colors should all be extracted. That can only be looked up. The result of the COLORHISTOGRAM structure is directly dropped to the Colorcutquantizer's construction method as a parameter. In this constructor we find the reason:
Now go through all of the colors and keep those which we don't want to ignore mcolors = new Int[rawcolorcount];
int validcolorcount = 0; for (int color:rawcolors) { if (!shouldignorecolor (color)) { mcolors[validcolorcount++] = color; } }
Google has made a color filter by a standard. In other words, the results we get directly from the interface are filtered out by a particular business. If we don't know the business, the color we get is certainly not the color we need. After removing this filtering logic, we get the main color, which is the most color we need, in the image.
In addition, from Google's official note, this interface is very fast. In the following method, Google will be the size of the image compressed into a smaller picture, this should be a fast processing time, an important reason:
public static Palette Generate (Bitmap Bitmap, int numcolors) { checkbitmapparam (Bitmap); Checknumbercolorsparam (numcolors); First we ll scale down the bitmap so it's shortest dimension is 100px final bitmap scaledbitmap = Scalebitmapdown (b ITMAP);
Personally feel that the interface layer is better than the ability to provide. If this interface layer provides two sets of interfaces: 1. The primary color extraction ability alone, 2. A set of color extraction algorithm. It might be easier to use the app in the business layer.
android5.0l version new feature: Main color extraction