从android中的位图图像中识别对象的形状?

时间:2011-06-30 13:13:11

标签: android colors bitmap paint shape

最近,我参与了一个油漆项目。首先,我需要 选择一种颜色。当我触摸图像时,选择的颜色将是 替换为位图图像的相同颜色区域。我怎么认识 图像中相同颜色区域的形状。

此致

的Habib

2 个答案:

答案 0 :(得分:1)

此代码可以完整帮助

imageViewEdit.setOnTouchListener(new View.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        setColor = newbitmap.getPixel((int)event.getX(),(int)event.getY());
        replaceX=(int)event.getX();
        replaceY=(int)event.getY();

        setColor_R = (setColor & 0x00ff0000) >> 16;
        setColor_G = (setColor & 0x0000ff00) >> 8;
        setColor_B = setColor & 0x000000ff;

        boolean[][] visited = new boolean[newbitmap.getWidth()][newbitmap.getHeight()];


        Queue<Pixel> pixel=new LinkedList<Pixel>();
        pixel.add(new Pixel(replaceX, replaceY));

        while(!pixel.isEmpty()){

            Pixel loc = pixel.remove();
            visited[loc._x][loc._y] = true;

            putIfValid(loc._x+1,loc._y-1,pixel,visited);
            putIfValid(loc._x+1,loc._y,pixel,visited);
            putIfValid(loc._x+1,loc._y+1,pixel,visited);
            putIfValid(loc._x-1,loc._y-1,pixel,visited);
            putIfValid(loc._x-1,loc._y,pixel,visited);
            putIfValid(loc._x-1,loc._y+1,pixel,visited);
            putIfValid(loc._x,loc._y+1,pixel,visited);
            putIfValid(loc._x,loc._y-1,pixel,visited);
        }
        imageViewEdit.setImageBitmap(newbitmap);

        return false;
    }


    public void putIfValid(int x, int y, Queue<Pixel> pixel, boolean[][] visited) {

            int colors=newbitmap.getPixel(x, y);

            int  current_R = (colors & 0x00ff0000) >> 16;
            int  current_G = (colors & 0x0000ff00) >> 8;
            int  current_B = colors & 0x000000ff;

            if(((setColor_R-RGBThreshold<=current_R) && (setColor_R+RGBThreshold>=current_R))
              &&((setColor_G-RGBThreshold<=current_G)&&(setColor_G+RGBThreshold>=current_G))
              &&((setColor_B-RGBThreshold<=current_B)&& (setColor_B+RGBThreshold>=current_B))
              && valid(x,y,visited)){

                pixel.add(new Pixel(x,y));
                newbitmap.setPixel(x, y, getColor);
                visited[x][y]=true;
            }
    }

    private boolean valid(int x, int y, boolean[][] visited) {

        return x >= 2 && x < (newbitmap.getWidth()-2)&&
               y >= 2 && y < (newbitmap.getHeight()-2) &&
               !visited[x][y];
    }
});


public static class Pixel {
    final public int _x;
    final public int _y;
    public Pixel(int x,int y){
        _x=x;
        _y=y;
    }
}

答案 1 :(得分:0)

您可能正在寻找一个存储桶工具实现。 This是一篇描述一般解决方案的文章。