I want to fill the color in white area for Paint based application so please give me suggestion for how to do this work..
after lot of searching, i came up with the following. It is fast and efficient
private void myFloodFillMethod(Point node,int targetColor, int fillColor) {
if (targetColor != fillColor) {
Queue queue = new LinkedList<>();
do {
int x = node.x;
int y = node.y;
while (x > 0 && getPixelColorFromArr(x - 1, y) == targetColor) {
x--; }
boolean spanUp = false;
boolean spanDown = false;
while (x < width && checkEquality(getPixelColorFromArr(x, y),targetColor)) {
setPixelColorFromArr(x, y, fillColor);
if (!spanUp && y > 0
&& checkEquality(getPixelColorFromArr(x, y - 1),targetColor)) {
queue.add(new Point(x, y - 1));
spanUp = true;
} else if (spanUp && y > 0
&& !checkEquality(getPixelColorFromArr(x, y - 1),targetColor)) {
spanUp = false;
}
if (!spanDown && y < height - 1
&& checkEquality(getPixelColorFromArr(x, y + 1), targetColor)) {
queue.add(new Point(x, y + 1));
spanDown = true;
} else if (spanDown && y < height - 1
&& !checkEquality(getPixelColorFromArr(x, y + 1),targetColor)) {
spanDown = false;
}
x++;
}
} while ((node = queue.poll()) != null);
}
}
private boolean checkEquality(int pixelColorFromArr, int targetColor) {
return pixelColorFromArr == targetColor;
}
void fillColorInImage(Bitmap image, Point node, int targetColor, int replacementColor){
if(targetColor == replacementColor) return;
width = image.getWidth();
height = image.getHeight();
pixelArr = new int[width*height];
image.getPixels(pixelArr,0,width,0,0,width,height);
myFloodFillMethod(node,targetColor,replacementColor);
image.setPixels(pixelArr,0,width,0,0,width,height);
}
how to call:
fillColorInImage(bmp, point, targetColor, replacementColor);
where
bmp
is the bitmap of image(extract form the image view)
point
is Point(x,y) where user has tapped:get it from event.getX()
and event.getY()
targetColor
is the color of the point: get it from bitmap.getPixel()
replacementColor
is the color you want to replace it to