如何识别不同的形状并在android中填充它们的颜色?
我想建立一个应用程序在native android中着色图像,它具有图像,我想知道如何检测图像边框,以及如何检测不同的形状并在其中填充颜色? 示例应用程序的链接:https://play.google.com/store/apps/details?id=kidgames.coloring.pages&hl=en如何识别不同的形状并在android中填充它们的颜色?
你可以用颜色填充算法。
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先生,但是你能否澄清一下,它是否会填满一个点击的形状,或者它将不得不通过触摸屏来填充它? –
它将填满边界下的全部区域。点击任何形状后。 –
谢谢..将尽力实施 –
退房这个答案https://stackoverflow.com/a/9748429/2556660。 – RameshJaga