PixelMap下填色功能的实现
头像 Tiechui.Wang 2021-04-14 10:00:00    发布
3300 浏览 10 点赞 19 收藏

本文利用FloodFill算法实现PixelMap的填色(油漆桶)效果。

关于FloodFill算法的介绍,请查看这篇文章:https://blog.csdn.net/lion19930924/article/details/54293661

下面介绍鸿蒙系统下填色功能的实现:

定义一个算法类,用于实现FloodFill算法:


public class FloodFillAlgorithm {

    private PixelMap fillBitmap;
    private int width;
    private int height;
    private boolean isFillColor = true;
    private int maxStackSize = 500;
    private int[] xstack = new int[maxStackSize];
    private int[] ystack = new int[maxStackSize];
    private int stackSize;

    public FloodFillAlgorithm(PixelMap fillBitmap) {
        this.fillBitmap = fillBitmap;
        width = fillBitmap.getImageInfo().size.width;
        height = fillBitmap.getImageInfo().size.height;
    }

    public int getColor(int x, int y) {
        return fillBitmap.readPixel(new Position(x, y));
    }

    public void setColor(int x, int y, int newColor) {
        fillBitmap.writePixel(new Position(x, y), newColor);
    }

    public boolean isFill() {
        return isFillColor;
    }

    public void floodFillScanLineWithStack(int x, int y, int newColor, int oldColor) {
        if (oldColor == newColor) {
            System.out.println("do nothing !!!, filled area!!");
            return;
        }
        emptyStack();

        int y1;
        boolean spanLeft, spanRight;
        push(x, y);

        while (true) {
            x = popx();
            if (x == -1) return;
            y = popy();
            y1 = y;
            while (y1 >= 0 && getColor(x, y1) == oldColor) y1--; // go to line top/bottom
            y1++; // start from line starting point pixel
            spanLeft = spanRight = false;
            while (y1 < height && getColor(x, y1) == oldColor) {
                setColor(x, y1, newColor);
                if (!spanLeft && x > 0 && getColor(x - 1, y1) == oldColor)// just keep left line once in the stack
                {
                    push(x - 1, y1);
                    spanLeft = true;
                } else if (spanLeft && x > 0 && getColor(x - 1, y1) != oldColor) {
                    spanLeft = false;
                }
                if (!spanRight && x < width - 1 && getColor(x + 1, y1) == oldColor) // just keep right line once in the stack
                {
                    push(x + 1, y1);
                    spanRight = true;
                } else if (spanRight && x < width - 1 && getColor(x + 1, y1) != oldColor) {
                    spanRight = false;
                }
                y1++;
            }
        }

    }

    private void emptyStack() {
        while (popx() != -1) {
            popy();
        }
        stackSize = 0;
    }

    final void push(int x, int y) {
        stackSize++;
        if (stackSize == maxStackSize) {
            int[] newXStack = new int[maxStackSize * 2];
            int[] newYStack = new int[maxStackSize * 2];
            System.arraycopy(xstack, 0, newXStack, 0, maxStackSize);
            System.arraycopy(ystack, 0, newYStack, 0, maxStackSize);
            xstack = newXStack;
            ystack = newYStack;
            maxStackSize *= 2;
        }
        xstack[stackSize - 1] = x;
        ystack[stackSize - 1] = y;
    }

    final int popx() {
        if (stackSize == 0)
            return -1;
        else
            return xstack[stackSize - 1];
    }

    final int popy() {
        int value = ystack[stackSize - 1];
        stackSize--;
        return value;
        
        

调用方式如下:


FloodFillAlgorithm floodFillAlgorithm = new FloodFillAlgorithm(pixelBitmap);
floodFillAlgorithm.floodFillScanLineWithStack(1, 1, Color.RED.getValue(), Color.GREEN.getValue());

效果如下:

PixelMap下填色功能的实现-鸿蒙开发者社区

至此,填色(油漆桶)效果已实现


©本站发布的所有内容,包括但不限于文字、图片、音频、视频、图表、标志、标识、广告、商标、商号、域名、软件、程序等,除特别标明外,均来源于网络或用户投稿,版权归原作者或原出处所有。我们致力于保护原作者版权,若涉及版权问题,请及时联系我们进行处理。
分类
其它
地址:北京市朝阳区北三环东路三元桥曙光西里甲1号第三置业A座1508室 商务内容合作QQ:2291221 电话:13391790444或(010)62178877
版权所有:电脑商情信息服务集团 北京赢邦策略咨询有限责任公司
声明:本媒体部分图片、文章来源于网络,版权归原作者所有,我司致力于保护作者版权,如有侵权,请与我司联系删除
京ICP备:2022009079号-2
京公网安备:11010502051901号
ICP证:京B2-20230255