如何在android中识别不同的形状并填充颜色? [英] How to recognise different shapes and fill color in them in android?
本文介绍了如何在android中识别不同的形状并填充颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想构建一个用于在原生 android 中为图像着色的应用程序,其中包含图像,我想知道如何检测图像边框,以及如何检测不同的形状并在其中填充颜色?示例应用链接:
解决方案
您可以使用 Flood Fill 算法.
见链接:
洪水填充算法
private void FloodFill(Bitmap bmp, Point pt, int targetColor, int replacementColor){队列<点>q = new LinkedList();q.add(pt);而 (q.size() > 0) {点 n = q.poll();如果(bmp.getPixel(n.x,n.y)!= targetColor)继续;点 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));如果 ((w.y < bmp.getHeight() - 1)&&(bmp.getPixel(w.x, w.y + 1) == targetColor))q.add(new Point(w.x, w.y + 1));w.x--;}而 ((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));如果 ((e.y < bmp.getHeight() - 1)&&(bmp.getPixel(e.x, e.y + 1) == targetColor))q.add(new Point(e.x, e.y + 1));e.x++;}}}
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
解决方案
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++;
}
}}
这篇关于如何在android中识别不同的形状并填充颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文