How to divide a bitmap into parts that are bitmaps too

Rub*_*sha 6 android bitmap

Hello StackOverflow comunity. I need some info on the possible methods for dividing a bitmap in smaller pieces. More importantly i would need some options to judge. I have checked many posts and I am still not entirely convinced about what to do.

cut the portion of bitmap

How do I cut out the middle area of ​​the bitmap?

这两个链接是我发现的一些很好的选项,但我不能计算每种方法的CPU和RAM成本,或者我可能根本不应该打扰这个计算.尽管如此,如果我要做某事,为什么不从头开始做最好的方法.

我将非常感谢获得关于位图压缩的一些提示和链接,因此我可能会在两种方法中获得更好的性能.

我提前谢谢你.

San*_*p R 9

您想将位图分成几部分。我假设您想从位图中剪切相等的部分。比如说你需要一个位图中的四个相等的部分。

这是一种将位图划分为四个相等部分并将其放在位图数组中的方法。

public Bitmap[] splitBitmap(Bitmap src) {
    Bitmap[] divided = new Bitmap[4];
    imgs[0] = Bitmap.createBitmap(
        src,
        0, 0,
        src.getWidth() / 2, src.getHeight() / 2
    );
    imgs[1] = Bitmap.createBitmap(
        src,
        src.getWidth() / 2, 0,
        src.getWidth() / 2, src.getHeight() / 2
    );
    imgs[2] = Bitmap.createBitmap(
        src,
        0, src.getHeight() / 2,
        src.getWidth() / 2, src.getHeight() / 2
    );
    imgs[3] = Bitmap.createBitmap(
        src,
        src.getWidth() / 2, src.getHeight() / 2,
        src.getWidth() / 2, src.getHeight() / 2
    );
    return divided;
}
Run Code Online (Sandbox Code Playgroud)


K.R*_*ers 7

此功能允许您将位图拆分为行数和列数.

示例Bitmap [] [] bitmaps = splitBitmap(bmp,2,1); 将创建存储在二维数组中的垂直分割位图.2列1行

示例Bitmap [] [] bitmaps = splitBitmap(bmp,2,2); 将位图拆分为存储在二维数组中的四个位图.2列2行

public Bitmap[][] splitBitmap(Bitmap bitmap, int xCount, int yCount) {
    // Allocate a two dimensional array to hold the individual images.
    Bitmap[][] bitmaps = new Bitmap[xCount][yCount];
    int width, height;
    // Divide the original bitmap width by the desired vertical column count
    width = bitmap.getWidth() / xCount;
    // Divide the original bitmap height by the desired horizontal row count
    height = bitmap.getHeight() / yCount;
    // Loop the array and create bitmaps for each coordinate
    for(int x = 0; x < xCount; ++x) {
        for(int y = 0; y < yCount; ++y) {
            // Create the sliced bitmap
            bitmaps[x][y] = Bitmap.createBitmap(bitmap, x * width, y * height, width, height);
        }
    }
    // Return the array
    return bitmaps;     
}
Run Code Online (Sandbox Code Playgroud)