支持技术分享,转载或复制,请指出文章来源此博客作者为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;
}
}
}
来源:oschina
链接:https://my.oschina.net/u/2670896/blog/3073524