Android 删除图像的背景,将其拆分为相同大小并更改颜色(覆盖)

Android 删除图像的背景,将其拆分为相同大小并更改颜色(覆盖),android,canvas,paint,Android,Canvas,Paint,我一直在互联网上搜索更改颜色或覆盖位图的特定部分。我有一个正方形位图,我想改变一个矩阵模式中的颜色,这个模式在9个相同的块中,可以从下图中理解。(此处青色线仅供演示) 我在大学里读过关于边界填充算法的书,但在这里,对于java,我知道它太庞大了,无法对9个特定部分执行这样的操作。我不知道如何使用Paint和Canvas来处理这种方形场景 那么,有没有一种方法或东西可以帮助我通过提供大小或位置来绘制特定的正方形,而不必在UI上执行大量任务呢 以下是我需要实现的目标: 如果有什么可以帮我的话,我

我一直在互联网上搜索更改颜色或覆盖位图的特定部分。我有一个正方形位图,我想改变一个矩阵模式中的颜色,这个模式在9个相同的块中,可以从下图中理解。(此处青色线仅供演示)

我在大学里读过关于边界填充算法的书,但在这里,对于java,我知道它太庞大了,无法对9个特定部分执行这样的操作。我不知道如何使用
Paint
Canvas
来处理这种方形场景

那么,有没有一种方法或东西可以帮助我通过提供大小或位置来绘制特定的正方形,而不必在UI上执行大量任务呢

以下是我需要实现的目标:

如果有什么可以帮我的话,我可以自己改变颜色、位置、尺寸。 此外,由于有白色背景,有没有办法不绘制背景或我必须使用PNG

更新:

我能够使用以下代码成功地将图像分成9等份,但PorterDuffColorFilter没有按预期工作

代码:

public void splitBitmap(位图){
int宽度、高度;
//将原始位图宽度除以所需的垂直列计数
宽度=位图.getWidth()/3;
//将原始位图高度除以所需的水平行数
高度=位图.getHeight()/3;
//循环数组并为每个坐标创建位图
对于(int x=0;x<3;++x){
对于(int y=0;y<3;++y){
//创建切片位图
添加(位图.createBitmap(位图,x*宽度,y*高度,宽度,高度));
}
}
意向意向=新意向(此,MainActivity.class);
星触觉(意向);
}

上面的代码提供了9个位图,然后将其设置为
GridLayout
。但是没有一种模式是有用的。要么图像保持原始状态,要么完全绘制。我尝试了所有可用的模式,但都没有成功。

我做了类似的事情,因此在稍微更改代码后,我认为这就是您想要的:

假设你没有PNG

首先,从图像中删除白色背景:
将白色设置为透明,可以使用所需的任何颜色

private static final int[] FROM_COLOR = new int[]{255, 255, 255};
private static final int THRESHOLD = 3;

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test_colors);

    ImageView iv = (ImageView) findViewById(R.id.img);
    Drawable d = getResources().getDrawable(RES);
    iv.setImageDrawable(adjust(d));
}

private Drawable adjust(Drawable d)
{
    int to = Color.TRANSPARENT;

    //Need to copy to ensure that the bitmap is mutable.
    Bitmap src = ((BitmapDrawable) d).getBitmap();
    Bitmap bitmap = src.copy(Bitmap.Config.ARGB_8888, true);
    for(int x = 0;x < bitmap.getWidth();x++)
        for(int y = 0;y < bitmap.getHeight();y++)
            if(match(bitmap.getPixel(x, y))) 
                bitmap.setPixel(x, y, to);

    return new BitmapDrawable(bitmap);
}

private boolean match(int pixel)
{
    //There may be a better way to match, but I wanted to do a comparison ignoring
    //transparency, so I couldn't just do a direct integer compare.
    return Math.abs(Color.red(pixel) - FROM_COLOR[0]) < THRESHOLD &&
        Math.abs(Color.green(pixel) - FROM_COLOR[1]) < THRESHOLD &&
        Math.abs(Color.blue(pixel) - FROM_COLOR[2]) < THRESHOLD;
}
可能会有一些问题,因为它对我有效,而对你可能无效,但这是你实现所需结果的方式。
如果任何问题仍然存在,我可以帮助。

@pskink我已经尝试过了,但它不起作用。尽管如此,我不得不将
Drawable
更改为
Bitmap
,因为您的
splitBitmap
需要
Bitmap
adjust
返回一个可绘制的位图,但您的代码帮助了我。谢谢你。
private static final int[] FROM_COLOR = new int[]{255, 255, 255};
private static final int THRESHOLD = 3;

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test_colors);

    ImageView iv = (ImageView) findViewById(R.id.img);
    Drawable d = getResources().getDrawable(RES);
    iv.setImageDrawable(adjust(d));
}

private Drawable adjust(Drawable d)
{
    int to = Color.TRANSPARENT;

    //Need to copy to ensure that the bitmap is mutable.
    Bitmap src = ((BitmapDrawable) d).getBitmap();
    Bitmap bitmap = src.copy(Bitmap.Config.ARGB_8888, true);
    for(int x = 0;x < bitmap.getWidth();x++)
        for(int y = 0;y < bitmap.getHeight();y++)
            if(match(bitmap.getPixel(x, y))) 
                bitmap.setPixel(x, y, to);

    return new BitmapDrawable(bitmap);
}

private boolean match(int pixel)
{
    //There may be a better way to match, but I wanted to do a comparison ignoring
    //transparency, so I couldn't just do a direct integer compare.
    return Math.abs(Color.red(pixel) - FROM_COLOR[0]) < THRESHOLD &&
        Math.abs(Color.green(pixel) - FROM_COLOR[1]) < THRESHOLD &&
        Math.abs(Color.blue(pixel) - FROM_COLOR[2]) < THRESHOLD;
}
public void splitBitmap(Bitmap bitmap) {
        ArrayList<Bitmap> smallimages = new ArrayList<>(9);
        int width, height;
        // Divide the original bitmap width by the desired vertical column count
        width = bitmap.getWidth() / 3;
        // Divide the original bitmap height by the desired horizontal row count
        height = bitmap.getHeight() / 3;
        // Loop the array and create bitmaps for each coordinate
        for (int x = 0; x < 3; ++x) {
            for (int y = 0; y < 3; ++y) {
                // Create the sliced bitmap
                smallimages.add(Bitmap.createBitmap(bitmap, x * width, y * height, width, height));
            }
        }
    }
    imageView.setImageDrawable(arrayList.get(0));
    imageView.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_ATOP);