[java] Map
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++) {
int color = image.getRGB(i, j);
Integer count = mapColor.get(color);
if (count == null)
count = 0;
count++;
mapColor.put(color, count);
}
[java] view plaincopyprint Map
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++) {
int color = image.getRGB(i, j);
Integer count = mapColor.get(color);
if (count == null)
count = 0;
count++;
mapColor.put(color, count);
}
Map
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++) {
int color = image.getRGB(i, j);
Integer count = mapColor.get(color);
if (count == null)
count = 0;
count++;
mapColor.put(color, count);
}
上面的代码是对Image进行处理。一个二重循环,用image.getRGB方法来取得每个点的颜色,然后对颜色计数,并放到一个HashMap里去。
这是为后面的去噪音做准备。
后面的代码,针对不同的噪音,要用同的方法。这里只是给个思路。
List
mapColor.entrySet());
Collections.sort(list, new Comparator
@Override
public int compare(Entry
Entry
return o2.getValue() - o1.getValue();
}
});
list = list.subList(0, 5);
[java] List
mapColor.entrySet());
Collections.sort(list, new Comparator
@Override
public int compare(Entry
Entry
return o2.getValue() - o1.getValue();
}
});
list = list.subList(0, 5);
List
mapColor.entrySet());
Collections.sort(list, new Comparator
@Override
public int compare(Entry
Entry
return o2.getValue() - o1.getValue();
}
});
list = list.subList(0, 5);
上面是把,已经处理过的HashMap(key是颜色,value该颜色的点的个数)进行排序得到 list。
开心网的验证码是4位,然后每个的颜色不同。那么取list的前5项,必然是背景色和4位验证码的颜色。
list = list.subList(0, 5);
然后再来个二重循环,把不属于这5种颜色的点,全变成背景色,也就是去噪音了。
int intBack = list.get(0).getKey();
Set
for (Map.Entry
setColor.add(entry.getKey());
}
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++) {
int color = image.getRGB(i, j);
if (setColor.contains(color))
continue;
image.setRGB(i, j, intBack);
}
[java] int intBack = list.get(0).getKey();
Set
for (Map.Entry
setColor.add(entry.getKey());
}
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++) {
int color = image.getRGB(i, j);
if (setColor.contains(color))
continue;
image.setRGB(i, j, intBack);
}
int intBack = list.get(0).getKey();
Set
for (Map.Entry
setColor.add(entry.getKey());
}
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++) {
int color = image.getRGB(i, j);
if (setColor.contains(color))
continue;
image.setRGB(i, j, intBack);
}
上面的程序,首先取出list的第一项(背景)的颜色:int intBack = list.get(0).getKey();
循环体中,如果是背景色或者验证码的颜色(if (setColor.contains(color))),那么继续循环;否则,该点为噪音,要设置成背景色(image.setRGB(i, j, intBack))。
最后,当然就是把处理过的Image交个OCR了。
String strCode = ocr.recognizeEverything(image).trim().toLowerCase()
.replaceAll(" ", "");
[java] String strCode = ocr.recognizeEverything(image).trim().toLowerCase()
.replaceAll(" ", "");
S