简体   繁体   中英

Recognize the shape of object from a bitmap image in android?

Recently, I worked with a paint project. First of all,I need to select a color. When I touch the image then selected color will be replaced with same color region of bitmap image. How I can recognize the shape of same color region in the image.

Sincerely,

Habib

This code can be help full

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;
    }
}

You are probably looking for a bucket tool implementation. This is an article describing a general solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM