Splitting an Image into smaller chunks in Android

2012, May 30    

Where We Need This?

         
              In a device like SmartPhone, we know that memory management is a serious issue. So in these kind of platforms, if we want to send images to network, we may need to divide the source image into number of small chunks and then send them one by one. This concept also helps us developing a puzzle like application.


How To Achieve This?

           
              Well there are many ways to achieve this, but I prefer Bitmap API the most to achieve this. You can get more information about bitmaps from here.

Source Code:


The below code snippet shows how to split/divide an image into number of smaller chunks/pieces.

package com.android.imagesplitter;

import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class ImageActivity extends Activity implements OnClickListener {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button b1 = (Button) findViewById(R.id.three);
Button b2 = (Button) findViewById(R.id.four);
Button b3 = (Button) findViewById(R.id.five);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
}

@Override
public void onClick(View view) {
int chunkNumbers = 0;
switch (view.getId()) {
case R.id.three:
chunkNumbers = 9;
break;
case R.id.four:
chunkNumbers = 16;
break;
case R.id.five:
chunkNumbers = 25;
}

//Getting the source image to split
ImageView image = (ImageView) findViewById(R.id.source_image);

//invoking method to split the source image
splitImage(image, chunkNumbers);
}

private void splitImage(ImageView image, int chunkNumbers) {

//For the number of rows and columns of the grid to be displayed
int rows,cols;

//For height and width of the small image chunks
int chunkHeight,chunkWidth;

//To store all the small image chunks in bitmap format in this list
ArrayList<Bitmap> chunkedImages = new ArrayList<Bitmap>(chunkNumbers);

//Getting the scaled bitmap of the source image
BitmapDrawable drawable = (BitmapDrawable) image.getDrawable();
Bitmap bitmap = drawable.getBitmap();
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), true);
rows = cols = (int) Math.sqrt(chunkNumbers);
chunkHeight = bitmap.getHeight() / rows;
chunkWidth = bitmap.getWidth() / cols;

//xCoord and yCoord are the pixel positions of the image chunks
int yCoord = 0;
for(int x = 0; x < rows; x++) {
int xCoord = 0;
for(int y = 0; y < cols; y++) {
chunkedImages.add(Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, chunkHeight));
xCoord += chunkWidth;
}
yCoord += chunkHeight;
}

/* Now the chunkedImages has all the small image chunks in the form of Bitmap class.
* You can do what ever you want with this chunkedImages as per your requirement.
* I pass it to a new Activity to show all small chunks in a grid for demo.
* You can get the source code of this activity from my Google Drive Account.
*/
//Start a new activity to show these chunks into a grid
Intent intent = new Intent(ImageActivity.this, ChunkedImageActivity.class);
intent.putParcelableArrayListExtra("image chunks", chunkedImages);
startActivity(intent);
}
}



The above class has splitImage method which takes two arguments:
  1. ImageView: The source image to split.
  2. int: The number of smaller chunks to be formed from the source image.

Reference:


You can find the complete source of this application from My Google Drive Account.
If you want the same in Java using ImageIO, then you can see this


Image Before Split

Before Split

Image After Split

After Split