【问题标题】:Android: How to fill color to the specific part of the Image only? [duplicate]Android:如何仅将颜色填充到图像的特定部分? [复制]
【发布时间】:2012-03-26 14:57:13
【问题描述】:

我有这样的图像:

现在,我想将颜色填充到该图像的特定部分。就像如果我选择蓝色并且如果我触摸 Cap 那么帽子应该用蓝色填充。鼻子,嘴巴,眼睛等其他部位也应该发生同样的事情

那么,如何使用 android 呢?

有没有朋友可以帮帮我。

更新

我已尝试在我的应用中实现 FloodFill 算法。 See Here

但在这样做之后,我得到了如下异常:

    03-09 17:45:16.260: ERROR/AndroidRuntime(2558): java.lang.IllegalStateException
03-09 17:45:16.260: ERROR/AndroidRuntime(2558):     at android.graphics.Bitmap.setPixel(Bitmap.java:847)
03-09 17:45:16.260: ERROR/AndroidRuntime(2558):     at com.project.fingerpaint.FinderPaintDemo.FloodFill(FinderPaintDemo.java:284)
03-09 17:45:16.260: ERROR/AndroidRuntime(2558):     at com.project.fingerpaint.FinderPaintDemo.access$3(FinderPaintDemo.java:272)
03-09 17:45:16.260: ERROR/AndroidRuntime(2558):     at com.project.fingerpaint.FinderPaintDemo$MyView.onTouchEvent(FinderPaintDemo.java:187)
03-09 17:45:16.260: ERROR/AndroidRuntime(2558):     at android.view.View.dispatchTouchEvent(View.java:3766)
03-09 17:45:16.260: ERROR/AndroidRuntime(2558):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
03-09 17:45:16.260: ERROR/AndroidRuntime(2558):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
03-09 17:45:16.260: ERROR/AndroidRuntime(2558):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
03-09 17:45:16.260: ERROR/AndroidRuntime(2558):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
03-09 17:45:16.260: ERROR/AndroidRuntime(2558):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
03-09 17:45:16.260: ERROR/AndroidRuntime(2558):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)
03-09 17:45:16.260: ERROR/AndroidRuntime(2558):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
03-09 17:45:16.260: ERROR/AndroidRuntime(2558):     at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
03-09 17:45:16.260: ERROR/AndroidRuntime(2558):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
03-09 17:45:16.260: ERROR/AndroidRuntime(2558):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
03-09 17:45:16.260: ERROR/AndroidRuntime(2558):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-09 17:45:16.260: ERROR/AndroidRuntime(2558):     at android.os.Looper.loop(Looper.java:123)
03-09 17:45:16.260: ERROR/AndroidRuntime(2558):     at android.app.ActivityThread.main(ActivityThread.java:4627)
03-09 17:45:16.260: ERROR/AndroidRuntime(2558):     at java.lang.reflect.Method.invokeNative(Native Method)
03-09 17:45:16.260: ERROR/AndroidRuntime(2558):     at java.lang.reflect.Method.invoke(Method.java:521)
03-09 17:45:16.260: ERROR/AndroidRuntime(2558):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-09 17:45:16.260: ERROR/AndroidRuntime(2558):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-09 17:45:16.260: ERROR/AndroidRuntime(2558):     at dalvik.system.NativeStart.main(Native Method)

现在,我的代码出了什么问题??

请在这种情况下帮助我。

谢谢。

【问题讨论】:

  • stackoverflow.com/questions/8801047/… 在这里。顺便说一句,漂亮的图片哈哈
  • @ladiesMan217:你在安卓上做过吗?作为指向该 FloodFill 函数的点,我应该给出什么?如果你有任何演示,请提供给我。
  • 请使用 FloodFill 算法,请使用此链接 Link
  • 嘿 iDroid Explorer 能否请您发布您的整个工作代码?
  • 您的代码中包含的 ColorPickerDialog 是什么?

标签: android android-canvas paint draw


【解决方案1】:

这是给图像填充颜色的完整代码:

填充图片:http://i.stack.imgur.com/7rita.jpg

ma​​in.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >

<RelativeLayout
    android:id="@+id/relative_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:layout_weight="1" >

    <ImageView
        android:id="@+id/coringImage"
        android:layout_width="300dp"
        android:layout_height="300dp" />
</RelativeLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btn_red"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="red" />

    <Button
        android:id="@+id/btn_yellow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="yellow" />

    <Button
        android:id="@+id/btn_blue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="blue" />
</LinearLayout>

</LinearLayout> 

活动类

public class FillColorActivity extends Activity implements OnTouchListener {
private RelativeLayout drawingLayout;
private MyView myView;
Button red, blue, yellow;
Paint paint;

/** Called when the activity is first created. */
/*
 * 
 * private ImageView imageView; private Canvas cv; private Bitmap mask,
 * original, colored; private int r,g,b; private int sG, sR, sB;
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    myView = new MyView(this);
    drawingLayout = (RelativeLayout) findViewById(R.id.relative_layout);
    drawingLayout.addView(myView);

    red = (Button) findViewById(R.id.btn_red);
    blue = (Button) findViewById(R.id.btn_blue);
    yellow = (Button) findViewById(R.id.btn_yellow);

    red.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            paint.setColor(Color.RED);
        }
    });

    yellow.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            paint.setColor(Color.YELLOW);
        }
    });
    blue.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            paint.setColor(Color.BLUE);
        }
    });
}

public class MyView extends View {

    private Path path;
    Bitmap mBitmap;
    ProgressDialog pd;
    final Point p1 = new Point();
    Canvas canvas;

    // Bitmap mutableBitmap ;
    public MyView(Context context) {
        super(context);

        paint = new Paint();
        paint.setAntiAlias(true);
        pd = new ProgressDialog(context);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeWidth(5f);
        mBitmap = BitmapFactory.decodeResource(getResources(),
                R.drawable.cartoon).copy(Bitmap.Config.ARGB_8888, true);

        this.path = new Path();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        this.canvas = canvas;
        paint.setColor(Color.GREEN);
        canvas.drawBitmap(mBitmap, 0, 0, paint);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:

            p1.x = (int) x;
            p1.y = (int) y;
            final int sourceColor = mBitmap.getPixel((int) x, (int) y);
            final int targetColor = paint.getColor();
            new TheTask(mBitmap, p1, sourceColor, targetColor).execute();
            invalidate();
        }
        return true;
    }

    public void clear() {
        path.reset();
        invalidate();
    }

    public int getCurrentPaintColor() {
        return paint.getColor();
    }

    class TheTask extends AsyncTask<Void, Integer, Void> {

        Bitmap bmp;
        Point pt;
        int replacementColor, targetColor;

        public TheTask(Bitmap bm, Point p, int sc, int tc) {
            this.bmp = bm;
            this.pt = p;
            this.replacementColor = tc;
            this.targetColor = sc;
            pd.setMessage("Filling....");
            pd.show();
        }

        @Override
        protected void onPreExecute() {
            pd.show();

        }

        @Override
        protected void onProgressUpdate(Integer... values) {

        }

        @Override
        protected Void doInBackground(Void... params) {
            FloodFill f = new FloodFill();
            f.floodFill(bmp, pt, targetColor, replacementColor);
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            pd.dismiss();
            invalidate();
        }
    }
}

// flood fill

public class FloodFill {
    public void floodFill(Bitmap image, Point node, int targetColor,
            int replacementColor) {
        int width = image.getWidth();
        int height = image.getHeight();
        int target = targetColor;
        int replacement = replacementColor;
        if (target != replacement) {
            Queue<Point> queue = new LinkedList<Point>();
            do {

                int x = node.x;
                int y = node.y;
                while (x > 0 && image.getPixel(x - 1, y) == target) {
                    x--;

                }
                boolean spanUp = false;
                boolean spanDown = false;
                while (x < width && image.getPixel(x, y) == target) {
                    image.setPixel(x, y, replacement);
                    if (!spanUp && y > 0
                            && image.getPixel(x, y - 1) == target) {
                        queue.add(new Point(x, y - 1));
                        spanUp = true;
                    } else if (spanUp && y > 0
                            && image.getPixel(x, y - 1) != target) {
                        spanUp = false;
                    }
                    if (!spanDown && y < height - 1
                            && image.getPixel(x, y + 1) == target) {
                        queue.add(new Point(x, y + 1));
                        spanDown = true;
                    } else if (spanDown && y < height - 1
                            && image.getPixel(x, y + 1) != target) {
                        spanDown = false;
                    }
                    x++;
                }
        } while ((node = queue.poll()) != null);
        }
    }
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    return false;
}
}

【讨论】:

  • 太棒了!做得好。谢谢,但是我们怎样才能使这种填充快速。如何添加代码以使其快速填充?它会在角落留下空白/点,如何完全填充/覆盖它。请
【解决方案2】:

使用 FloodFill。

  FloodFill f= new FloodFill();
        f.floodFill(bmp,pt,targetColor,replacementColor);

  public class FloodFill {
public void floodFill(Bitmap  image, Point node, int targetColor,
        int replacementColor) {
    int width = image.getWidth();
    int height = image.getHeight();
    int target = targetColor;
    int replacement = replacementColor;
    if (target != replacement) {
        Queue<Point> queue = new LinkedList<Point>();
        do {
            int x = node.x;
            int y = node.y;
            while (x > 0 && image.getPixel(x - 1, y) == target) {
                x--;
            }
            boolean spanUp = false;
            boolean spanDown = false;
            while (x < width && image.getPixel(x, y) == target) {
                image.setPixel(x, y, replacement);
                if (!spanUp && y > 0 && image.getPixel(x, y - 1) == target) {
                    queue.add(new Point(x, y - 1));
                    spanUp = true;
                } else if (spanUp && y > 0
                        && image.getPixel(x, y - 1) != target) {
                    spanUp = false;
                }
                if (!spanDown && y < height - 1
                        && image.getPixel(x, y + 1) == target) {
                    queue.add(new Point(x, y + 1));
                    spanDown = true;
                } else if (spanDown && y < height - 1
                        && image.getPixel(x, y + 1) != target) {
                    spanDown = false;
                }
                x++;
            }
        } while ((node = queue.poll()) != null);
    }
}
}

【讨论】:

  • @Raghuandan K 我们如何从选定区域获取特定位图和点。你能解释一下吗?
  • 最终点 p1 = new Point(); p1.x=(int) x; p1.y=(int) y; final int sourceColor= mDrawingManager.mDrawingUtilities.mBitmap.getPixel((int)x,(int) y);最终 int targetColor = mDrawingManager.mDrawingUtilities.mPaint.getColor(); new TheTask(mDrawingManager.mDrawingUtilities.mBitmap, p1, sourceColor, targetColor).execute();
  • 好的,谢谢,让我试着回复你。
  • 这里的 x 和 y 是用户在屏幕上触摸的坐标。
  • @isuru 不确定,因为我没有这样做。但是你能不能将vectordrawable 转换为Bitmap 然后应用floodfill stackoverflow.com/questions/33696488/…。检查它是否有效
【解决方案3】:

我怀疑初始位图是在只读模式下创建的。这可能是导致 setPixel() 异常的原因。您可以通过 BitmapFactory 选项进行更改:

opt = new BitmapFactory.Options();
// force RGBA pixel format even for 8-bit grey scale image files.
opt.inPreferredConfig = Bitmap.Config.ARGB_8888;
// want to modify the bitmap content.
opt.inMutable = true;
Bitmap bitmap=BitmapFactory.decodeFile("clown.png", opt);

【讨论】:

    猜你喜欢
    • 2014-03-30
    • 1970-01-01
    • 2012-02-06
    • 2016-12-27
    • 1970-01-01
    • 2019-12-11
    • 1970-01-01
    • 2023-04-10
    相关资源
    最近更新 更多