1

I want to build an app for coloring images in native android, which has images and I want to know how to detect the image borders, and how to detect different shapes and fill color in them? example app link : https://play.google.com/store/apps/details?id=kidgames.coloring.pages&hl=en

enter image description here

N.R
  • 191
  • 1
  • 13

1 Answers1

3

You can use Flood Fill Algorithm.

see link:
Flood Fill Algorithm

private void FloodFill(Bitmap bmp, Point pt, int targetColor, int replacementColor){
Queue<Point> q = new LinkedList<Point>();
q.add(pt);
while (q.size() > 0) {
    Point n = q.poll();
    if (bmp.getPixel(n.x, n.y) != targetColor)
        continue;

    Point w = n, e = new Point(n.x + 1, n.y);
    while ((w.x > 0) && (bmp.getPixel(w.x, w.y) == targetColor)) {
        bmp.setPixel(w.x, w.y, replacementColor);
        if ((w.y > 0) && (bmp.getPixel(w.x, w.y - 1) == targetColor))
            q.add(new Point(w.x, w.y - 1));
        if ((w.y < bmp.getHeight() - 1)
                && (bmp.getPixel(w.x, w.y + 1) == targetColor))
            q.add(new Point(w.x, w.y + 1));
        w.x--;
    }
    while ((e.x < bmp.getWidth() - 1)
            && (bmp.getPixel(e.x, e.y) == targetColor)) {
        bmp.setPixel(e.x, e.y, replacementColor);

        if ((e.y > 0) && (bmp.getPixel(e.x, e.y - 1) == targetColor))
            q.add(new Point(e.x, e.y - 1));
        if ((e.y < bmp.getHeight() - 1)
                && (bmp.getPixel(e.x, e.y + 1) == targetColor))
            q.add(new Point(e.x, e.y + 1));
        e.x++;
    }
}}
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
  • thank you Mr. Chetan, but can you clarify that whether it will fill the shape on one click or it will have to fill it by touching screen? – N.R Jun 19 '17 at 13:04
  • it will fill full area under boundary . after clicking under any shape. – Chetan Joshi Jun 19 '17 at 13:06
  • this solution is working but is too slow.. taking lot of time to fill colour. Is there any other option rather than this – N.R Jun 21 '17 at 10:37
  • for this you need to understand algorithm and then build your own Algorithm, yes its slow because its holds colored pixels and add them into Linked-list so its contains too much data , So you need to create way in which pixels should be colored in many directions simultaneously . in multi pal Th-reds. – Chetan Joshi Jun 21 '17 at 11:13
  • [link](https://play.google.com/store/apps/details?id=com.kidsplaylearning.apps.colorbook&hl=en) this app works absolutely fine. So for this would there be any bucket algorithm used or created new one? – N.R Jun 21 '17 at 11:21