支持技術分享,轉載或複製,請指出文章來源此博客作者爲Jack__0023
1、背景
最近比較悠閒一點,然後就在考慮,我應該如何識別我們公司的終端是不是黑屏了,如果我能識別了,這樣的話,運維的工作就可以變的簡單一點,所以有了這個東西的產生。
bak:只能說滿足我的需求,你們可以參考一下
2、簡介和代碼區
2-1、簡介區域
2-1-1、沒有什麼很複雜的類,基本我都封好了,支持資源文件夾,sd卡這些的圖片進行顏色提取,可能比較有爭議的是我自己編寫的顏色區分類(MyColor 類),因爲是我自己慢慢去測試訓練的,所以這個一定不是非常準確的,會存在我劃分出來的顏色值,只能說是一個大概的範圍判斷。
2-2、代碼區域
2-2-1、先上檢測圖片和提取結果,還有一張顏色表
2-2-1-1、識別的圖片
2-2-1-2、提取結果
2-2-1-3、顏色表,看完我已經不會區分顏色了
2-2-2、MyColor 類
2-2-2-1、因爲最主要的代碼是 MyColor 所以我放到最前面了
/**
* @Description 自定義顏色類,不是很準確的,但是提取純色,例如黑白完全沒問題
* @author 姚旭民
* @date 2019/7/12 18:11
*/
public enum MyColor {//自己劃定的幾個顏色的顏色分類,會存在識別不出來的顏色
COLOR_BLACK(0, 66, 0, 66, 0, 66, "黑色", 0),
COLOR_GRAY(67, 190, 67, 190, 67, 190, "灰色", 0),
COLOR_WHITE(230, 255, 230, 255, 230, 255, "白色", 0),
COLOR_RED(70, 255, 0, 200, 0, 200, "紅色", 0),
COLOR_YELLOW(220, 255, 140, 230, 0, 160, "黃色", 0),
COLOR_ORANGE(200, 255, 85, 110, 0, 61, "橙色", 0),
COLOR_GREEN(0, 127, 60, 255, 0, 205, "綠色", 0),
COLOR_CHING(0, 160, 120, 255, 84, 255, "青色", 0),
COLOR_BLUE(0, 25, 25, 60, 150, 255, "藍色", 0),
COLOR_PURPLE(80, 218, 32, 112, 64, 250, "紫色", 0);
private MyColor(int rMin, int rMax, int gMin, int gMax, int bMin, int bMax, String name, double parcent) {
this.rMin = rMin;
this.rMax = rMax;
this.gMin = gMin;
this.gMax = gMax;
this.bMin = bMin;
this.bMax = bMax;
this.name = name;
this.parcent = parcent;
}
private int rMin, rMax, gMin, gMax, bMin, bMax;
private double parcent;
private String name;
public int getrMin() {
return rMin;
}
public int getrMax() {
return rMax;
}
public int getgMin() {
return gMin;
}
public int getgMax() {
return gMax;
}
public int getbMin() {
return bMin;
}
public int getbMax() {
return bMax;
}
public String getName() {
return name;
}
public double getParcent() {
return parcent;
}
public void setParcent(double parcent) {
this.parcent = parcent;
}
/**
* @param r 紅色色值
* @param g 綠色色值
* @param b 藍色色值
* @Description 通過三原色色值查找顏色
* @author 姚旭民
* @date 2019/7/9 17:05
*/
static int findCount = 0;
public static MyColor find(double r, double g, double b) {//如果可以的話,不要改這裏的順序,因爲改了結果會不一樣的
if (r <= COLOR_BLACK.rMax && r >= COLOR_BLACK.rMin && g <= COLOR_BLACK.gMax && g >= COLOR_BLACK.gMin && b <= COLOR_BLACK.bMax && b >= COLOR_BLACK.bMin) {
return COLOR_BLACK;//黑色
} else if (r <= COLOR_WHITE.rMax && r >= COLOR_WHITE.rMin && g <= COLOR_WHITE.gMax && g >= COLOR_WHITE.gMin && b <= COLOR_WHITE.bMax && b >= COLOR_WHITE.bMin) {
return COLOR_WHITE;//白色
} else if (r <= COLOR_GRAY.rMax && r >= COLOR_GRAY.rMin && g <= COLOR_GRAY.gMax && g >= COLOR_GRAY.gMin && b <= COLOR_GRAY.bMax && b >= COLOR_GRAY.bMin) {
return COLOR_GRAY;//灰色
} else if (r <= COLOR_RED.rMax && r >= COLOR_RED.rMin && g <= COLOR_RED.gMax && g >= COLOR_RED.gMin && b <= COLOR_RED.bMax && b >= COLOR_RED.bMin) {
return COLOR_RED;//紅色
} else if (r <= COLOR_BLUE.rMax && r >= COLOR_BLUE.rMin && g <= COLOR_BLUE.gMax && g >= COLOR_BLUE.gMin && b <= COLOR_BLUE.bMax && b >= COLOR_BLUE.bMin) {
return COLOR_BLUE;//藍色
} else if (r <= COLOR_CHING.rMax && r >= COLOR_CHING.rMin && g <= COLOR_CHING.gMax && g >= COLOR_CHING.gMin && b <= COLOR_CHING.bMax && b >= COLOR_CHING.bMin) {
return COLOR_CHING;//青色
} else if (r <= COLOR_GREEN.rMax && r >= COLOR_GREEN.rMin && g <= COLOR_GREEN.gMax && g >= COLOR_GREEN.gMin && b <= COLOR_GREEN.bMax && b >= COLOR_GREEN.bMin) {
return COLOR_GREEN;//綠色
} else if (r <= COLOR_ORANGE.rMax && r >= COLOR_ORANGE.rMin && g <= COLOR_ORANGE.gMax && g >= COLOR_ORANGE.gMin && b <= COLOR_ORANGE.bMax && b >= COLOR_ORANGE.bMin) {
return COLOR_ORANGE;//橙色
} else if (r <= COLOR_YELLOW.rMax && r >= COLOR_YELLOW.rMin && g <= COLOR_YELLOW.gMax && g >= COLOR_YELLOW.gMin && b <= COLOR_YELLOW.bMax && b >= COLOR_YELLOW.bMin) {
return COLOR_YELLOW;//黃色
} else if (r <= COLOR_PURPLE.rMax && r >= COLOR_PURPLE.rMin && g <= COLOR_PURPLE.gMax && g >= COLOR_PURPLE.gMin && b <= COLOR_PURPLE.bMax && b >= COLOR_PURPLE.bMin) {
return COLOR_PURPLE;//紫色
}
if (findCount < 20) {
Log.i(TAG, "find| r : " + r + ",g : " + g + ",b : " + b);
findCount++;
}
return null;
}
}
2-2-2-2、測試封裝 ColorUtils 類 (MyColor類也在裏面)
public class ColorUtils {
private static final String TAG = ColorUtils.class.getSimpleName();
//沒有什麼用的,做測試的,隨時都可以刪除
private static Map<MyColor, Integer> mMap = new HashMap<MyColor, Integer>();
/**
* @param resId 資源文件夾中的索引id
* @Description 檢查圖片資源的顏色佔比
* @author 姚旭民
* @date 2019/7/12 16:57
*/
public static Map<String, String> analyseColor(Context context, int resId) {
return analyseColor(getBitmapFormDrawable(getDrawableFromResources(context, resId)));
}
/**
* @param url sd卡中資源的地址
* @Description 檢查圖片資源的顏色佔比
* @author 姚旭民
* @date 2019/7/12 16:57
*/
public static Map<String, String> analyseColor(String url) {
Log.i(TAG, "analyseColor| ");
FileOutputStream out = null;
FileInputStream in = null;
try {
in = new FileInputStream(url);
Bitmap bitmap = BitmapFactory.decodeStream(in);
//開始分析顏色
return analyseColor(bitmap);
} catch (Exception e) {
Log.e(TAG, e.toString());
} finally {
if (out != null) {
try {
out.flush();
out.close();
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
if (in != null) {
try {
in.close();
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
}
return null;
}
/**
* @param bitmap 資源對象
* @Description 檢查圖片資源的顏色佔比
* @author 姚旭民
* @date 2019/7/12 16:58
*/
public static Map<String, String> analyseColor(Bitmap bitmap) throws NullPointerException {
try {
if (bitmap == null)
throw new NullPointerException("bitmap is null.");
int rows = bitmap.getWidth();
int cols = bitmap.getHeight();
MyColor key;
int r, g, b;
for (int x = 0; x < rows; x++) {
for (int y = 0; y < cols; y++) {
r = Color.red(bitmap.getPixel(x, y));
g = Color.green(bitmap.getPixel(x, y));
b = Color.blue(bitmap.getPixel(x, y));
key = MyColor.find(r, g, b);
if (!mMap.containsKey(key)) {
mMap.put(key, 1);
} else {
mMap.put(key, mMap.get(key) + 1);
}
}
}
Log.i(TAG, "analyseColor| MyColor JackTestMethod mMap的值爲 : " + JSON.toJSONString(mMap));
return parcentage(rows * cols);
} catch (Exception e) {
Log.e(TAG, e.toString());
}
return null;
}
/**
* @return 返回一個保存顏色和佔比的 Map<String, Double> 對象
* @Description 計算百分比
* @author 姚旭民
* @date 2019/7/10 19:08
*/
public static Map<String, String> parcentage(int total) {
MyColor temp;
Map<String, String> result = new HashMap<String, String>();
for (Map.Entry<MyColor, Integer> entry : mMap.entrySet()) {
temp = entry.getKey();
if (temp != null) {
temp.setParcent((entry.getValue() * 1.0) / total);
Log.i(TAG, "parcentage| name : " + temp.getName() + ",parcent : " + temp.getParcent() * 100 + "%");
result.put(temp.getName(), temp.getParcent() * 100 + "%");
}
}
return result;
}
public static Bitmap getBitmapFormDrawable(Drawable drawable) {
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE
? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
//設置繪畫的邊界,此處表示完整繪製
drawable.draw(canvas);
return bitmap;
}
public static Drawable getDrawableFromResources(Context context, int resId) {
return context.getResources().getDrawable(resId);
}
public enum MyColor {//自己劃定的幾個顏色的顏色分類
COLOR_BLACK(0, 66, 0, 66, 0, 66, "黑色", 0),
COLOR_GRAY(67, 190, 67, 190, 67, 190, "灰色", 0),
COLOR_WHITE(230, 255, 230, 255, 230, 255, "白色", 0),
COLOR_RED(70, 255, 0, 200, 0, 200, "紅色", 0),
COLOR_YELLOW(220, 255, 140, 230, 0, 160, "黃色", 0),
COLOR_ORANGE(200, 255, 85, 110, 0, 61, "橙色", 0),
COLOR_GREEN(0, 127, 60, 255, 0, 205, "綠色", 0),
COLOR_CHING(0, 160, 120, 255, 84, 255, "青色", 0),
COLOR_BLUE(0, 25, 25, 60, 150, 255, "藍色", 0),
COLOR_PURPLE(80, 218, 32, 112, 64, 250, "紫色", 0);
private MyColor(int rMin, int rMax, int gMin, int gMax, int bMin, int bMax, String name, double parcent) {
this.rMin = rMin;
this.rMax = rMax;
this.gMin = gMin;
this.gMax = gMax;
this.bMin = bMin;
this.bMax = bMax;
this.name = name;
this.parcent = parcent;
}
private int rMin, rMax, gMin, gMax, bMin, bMax;
private double parcent;
private String name;
public int getrMin() {
return rMin;
}
public int getrMax() {
return rMax;
}
public int getgMin() {
return gMin;
}
public int getgMax() {
return gMax;
}
public int getbMin() {
return bMin;
}
public int getbMax() {
return bMax;
}
public String getName() {
return name;
}
public double getParcent() {
return parcent;
}
public void setParcent(double parcent) {
this.parcent = parcent;
}
/**
* @param r 紅色色值
* @param g 綠色色值
* @param b 藍色色值
* @Description 通過三原色色值查找顏色
* @author 姚旭民
* @date 2019/7/9 17:05
*/
static int findCount = 0;
public static MyColor find(double r, double g, double b) {
if (r <= COLOR_BLACK.rMax && r >= COLOR_BLACK.rMin && g <= COLOR_BLACK.gMax && g >= COLOR_BLACK.gMin && b <= COLOR_BLACK.bMax && b >= COLOR_BLACK.bMin) {
return COLOR_BLACK;//黑色
} else if (r <= COLOR_WHITE.rMax && r >= COLOR_WHITE.rMin && g <= COLOR_WHITE.gMax && g >= COLOR_WHITE.gMin && b <= COLOR_WHITE.bMax && b >= COLOR_WHITE.bMin) {
return COLOR_WHITE;//白色
} else if (r <= COLOR_GRAY.rMax && r >= COLOR_GRAY.rMin && g <= COLOR_GRAY.gMax && g >= COLOR_GRAY.gMin && b <= COLOR_GRAY.bMax && b >= COLOR_GRAY.bMin) {
return COLOR_GRAY;//灰色
} else if (r <= COLOR_RED.rMax && r >= COLOR_RED.rMin && g <= COLOR_RED.gMax && g >= COLOR_RED.gMin && b <= COLOR_RED.bMax && b >= COLOR_RED.bMin) {
return COLOR_RED;//紅色
} else if (r <= COLOR_BLUE.rMax && r >= COLOR_BLUE.rMin && g <= COLOR_BLUE.gMax && g >= COLOR_BLUE.gMin && b <= COLOR_BLUE.bMax && b >= COLOR_BLUE.bMin) {
return COLOR_BLUE;//藍色
} else if (r <= COLOR_CHING.rMax && r >= COLOR_CHING.rMin && g <= COLOR_CHING.gMax && g >= COLOR_CHING.gMin && b <= COLOR_CHING.bMax && b >= COLOR_CHING.bMin) {
return COLOR_CHING;//青色
} else if (r <= COLOR_GREEN.rMax && r >= COLOR_GREEN.rMin && g <= COLOR_GREEN.gMax && g >= COLOR_GREEN.gMin && b <= COLOR_GREEN.bMax && b >= COLOR_GREEN.bMin) {
return COLOR_GREEN;//綠色
} else if (r <= COLOR_ORANGE.rMax && r >= COLOR_ORANGE.rMin && g <= COLOR_ORANGE.gMax && g >= COLOR_ORANGE.gMin && b <= COLOR_ORANGE.bMax && b >= COLOR_ORANGE.bMin) {
return COLOR_ORANGE;//橙色
} else if (r <= COLOR_YELLOW.rMax && r >= COLOR_YELLOW.rMin && g <= COLOR_YELLOW.gMax && g >= COLOR_YELLOW.gMin && b <= COLOR_YELLOW.bMax && b >= COLOR_YELLOW.bMin) {
return COLOR_YELLOW;//黃色
} else if (r <= COLOR_PURPLE.rMax && r >= COLOR_PURPLE.rMin && g <= COLOR_PURPLE.gMax && g >= COLOR_PURPLE.gMin && b <= COLOR_PURPLE.bMax && b >= COLOR_PURPLE.bMin) {
return COLOR_PURPLE;//紫色
}
if (findCount < 20) {
Log.i(TAG, "find| r : " + r + ",g : " + g + ",b : " + b);
findCount++;
}
return null;
}
}
}