Rubin Basha
Rubin Basha

Reputation: 292

How to divide a bitmap into parts that are bitmaps too

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:

  1. cut the portion of bitmap
  2. How do I cut out the middle area of the bitmap?

These two posts are some good options I found, but I cant calculate the CPU and RAM cost of each method, or maybe I should not bother with this calculation at all. Nonetheless if I am about to do something, why not do it the best way from the start.

I would be grateful to get some tips and links on bitmap compression so maybe I get better performance combining the two methods.

Upvotes: 12

Views: 9717

Answers (2)

Sandeep R
Sandeep R

Reputation: 2292

You want to divide a bitmap into parts. I assume you want to cut equal parts from bitmap. Say for example you need four equal parts from a bitmap.

This is a method which divides a bitmap onto four equal parts and has it in an array of bitmaps.

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

Upvotes: 11

K.Rodgers
K.Rodgers

Reputation: 111

This function allows you to split a bitmap into and number of rows and columns.

Example Bitmap[][] bitmaps = splitBitmap(bmp, 2, 1); Would create a vertically split bitmap stored in a two dimensional array. 2 columns 1 row

Example Bitmap[][] bitmaps = splitBitmap(bmp, 2, 2); Would split a bitmap into four bitmaps stored in a two dimensional array. 2 columns 2 rows

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

Upvotes: 10

Related Questions