Back to project page yammp.
The source code is released under:
GNU Lesser General Public License
If you think the Android project yammp listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package org.yammp.util; //www . j a v a 2 s. com import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import android.graphics.Bitmap; import android.graphics.Color; /** * * @author mariotaku * * Get the main color from a {@link Bitmap}<br> * <br> * * <b>Important</b>: I recommand using this method in different thread. * Or it will make your application laggy or unresponceable. */ public class ColorAnalyser { /** * * Get the main color from a {@link Bitmap}.<br> * * @param bitmap * The {@link Bitmap} to analyse * @return The rgb {@link Color} in integer (no alpha) */ public static int analyse(Bitmap bitmap) { return analyse(bitmap, 18, 28); } /** * * Get the main color from a {@link Bitmap}.<br> * * @param bitmap * The {@link Bitmap} to analyse * @param width * The desired width of scaled bitmap * @param height * The desired height of scaled bitmap * @return The rgb {@link Color} in integer (no alpha) */ public static int analyse(Bitmap bitmap, int width, int height) { if (bitmap == null) return Color.WHITE; int color = 0; HashMap<Float, Integer> colorsMap = new HashMap<Float, Integer>(); ArrayList<Float> colorsScore = new ArrayList<Float>(); Bitmap resized = Bitmap.createScaledBitmap(bitmap, width, height, false); for (int y = 0; y < resized.getHeight(); y++) { for (int x = 0; x < resized.getWidth(); x++) { int temp_color = resized.getPixel(x, y); color = Color.argb(0xFF, Color.red(temp_color), Color.green(temp_color), Color.blue(temp_color)); float[] hsv = new float[3]; Color.colorToHSV(color, hsv); float score = (hsv[1] * hsv[1] + 0.001f) * (hsv[2] * hsv[2]); colorsMap.put(score, color); colorsScore.add(score); } } Collections.sort(colorsScore); bitmap.recycle(); resized.recycle(); return colorsMap.get(colorsScore.get(colorsScore.size() - 1)); } /** * * Get the main color from a {@link Bitmap}.<br> * * @param bitmap * The {@link Bitmap} to analyse * @param width * The desired width of scaled bitmap * @param height * The desired height of scaled bitmap * @param def * The default color returned, if bitmap is null * @return The rgb {@link Color} in integer (no alpha) */ public static int analyse(Bitmap bitmap, int width, int height, int def) { if (bitmap == null) return def; int color = 0; HashMap<Float, Integer> colorsMap = new HashMap<Float, Integer>(); ArrayList<Float> colorsScore = new ArrayList<Float>(); Bitmap resized = Bitmap.createScaledBitmap(bitmap, width, height, false); for (int y = 0; y < resized.getHeight(); y++) { for (int x = 0; x < resized.getWidth(); x++) { int temp_color = resized.getPixel(x, y); color = Color.argb(0xFF, Color.red(temp_color), Color.green(temp_color), Color.blue(temp_color)); float[] hsv = new float[3]; Color.colorToHSV(color, hsv); float score = (hsv[1] * hsv[1] + 0.001f) * (hsv[2] * hsv[2]); colorsMap.put(score, color); colorsScore.add(score); } } Collections.sort(colorsScore); bitmap.recycle(); resized.recycle(); return colorsMap.get(colorsScore.get(colorsScore.size() - 1)); } }