android - 如何在android中识别不同的形状并在其中填充颜色?

标签 android react-native

我想在 native android 中构建一个用于为图像着色的应用程序,它有图像,我想知道如何检测图像边界,以及如何检测不同的形状并在其中填充颜色? 示例应用程序链接:https://play.google.com/store/apps/details?id=kidgames.coloring.pages&hl=en

enter image description here

最佳答案

您可以使用洪水填充算法。

查看链接:
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 - 如何在android中识别不同的形状并在其中填充颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44630713/

相关文章:

android - 关于共享首选项的 boolean 值问题

android - 如何更改 int 值 onclick 并将其传递给另一个 Activity ?在安卓上?

javascript - 有没有办法通过 React-Native (JS) 预打包 Realm 数据库 (default.Realm)

react-native - 如何在 React Native 第一次运行时启动远程调试

javascript - 如何在带有 react-alert 的组件中显示消息?

ios - React Native 中的 Fetch(或 Axios)不适用于 iOS 应用程序的博览会

android - Android 上的 DllNotFoundException

android - 为 BitmapFactory.decodeFile 和 BitmapFactory.Options.inBitmap 重用位图

react-native - PushNotificationIOS 在模拟器上工作吗?

java - 如何更改或设置新的 Activity 启动器应用程序? ( java 语)