pojo:
public class ColorPo {
@Override
public String toString() {
return "ColorPo [red=" + red + ", green=" + green + ", blue=" + blue + ", x=" + x + ", y=" + y + "]";
}
private int red;
private int green;
private int blue;
private int x;
private int y;
ColorPo(){}
ColorPo(int color,int x,int y){
this.red = (color >> 16) & 0xFF;
this.green = (color >> 8) & 0xFF;
this.blue = color & 0xFF;
this.x = x;
this.y = y;
}
public boolean isBlack() {
return sum() <= 300 ? true : false;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getColor() {
int color = ((red*256)+green)*256+blue;
return color > 8388608 ? color - 16777216 : color;
}
public int getRed() {
return red;
}
public void setRed(int red) {
this.red = red;
}
public int getGreen() {
return green;
}
public void setGreen(int green) {
this.green = green;
}
public int getBlue() {
return blue;
}
public void setBlue(int blue) {
this.blue = blue;
}
public int sum() {
return red + blue + green;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ColorPo other = (ColorPo) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
}
util:
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.imageio.ImageIO;
public class ImageUtils {
//用于存储所有相邻的像素点
private static List<Set<ColorPo>> colorList = new ArrayList<Set<ColorPo>>();
public static void main(String[] args) throws Exception {
File imageFile = new File("");
BufferedImage read = ImageIO.read(imageFile);
readImage(read);
}
/**
* 加载图片 遍历像素点
* @param image
*/
public static void readImage(BufferedImage image){
Set<ColorPo> colorPoSet = new HashSet<ColorPo>();
int z = 0;
for (int i = 0; i < image.getHeight(); i++) {
for (int j = 0; j < image.getWidth(); j++) {
z++;
ColorPo colorPo = new ColorPo(image.getRGB(i, j), i, j);
if (colorPo.isBlack() && !colorPoSet.contains(colorPo)) {
Set<ColorPo> colors = new HashSet<ColorPo>();
System.out.println(1);
colors.add(colorPo);
getBlackArrays(image,i,j,colors);
colorPoSet.addAll(colors);
colorList.add(colors);
colors = new HashSet<ColorPo>();
}
}
}
System.out.println(colorPoSet.size());
System.out.println(colorList.size());
for (Set<ColorPo> set : colorList) {
System.out.println(set.size());
}
}
/**
* 获取相邻像素集合
* @param image
* @param x
* @param y
* @param colorPoSet
* @param colorPoSet
*/
public static void getBlackArrays(BufferedImage image,int x,int y, Set<ColorPo> colorPoSet) {
int startX = 0,startY = 0,sum = 0;
for (int i = 0; i < 8; i++) {
switch (i) {
//左上
case 0:
startX = x - 1;
startY = y - 1;
break;
//上
case 1:
startX = x;
startY = y - 1;
break;
//右上
case 2:
startX = x + 1;
startY = y - 1;
break;
//左
case 3:
startX = x - 1;
startY = y;
break;
//右
case 4:
startX = x + 1;
startY = y;
break;
//左下
case 5:
startX = x - 1;
startY = y + 1;
break;
//下
case 6:
startX = x;
startY = y + 1;
break;
//右下
case 7:
startX = x + 1;
startY = y + 1;
break;
}
if (startX >=0 && startX < image.getWidth() && startY>= 0 && startY < image.getHeight()) {
ColorPo colorPo = new ColorPo(image.getRGB(startX, startY), startX, startY);
if (colorPo.isBlack() && !colorPoSet.contains(colorPo)) {
colorPoSet.add(colorPo);
sum++;
getBlackArrays(image, startX, startY,colorPoSet);
}
}
}
}