Monday, 10 October 2011

Bitmap processing: createScaledBitmap, getPixels and setPixels.

Example of using createScaledBitmap(), getPixels() and setPixels() on Bitmap.
Bitmap processing: createScaledBitmap, getPixels and setPixels.
package com.exercise.AndroidBitmap;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;

public class AndroidBitmapActivity extends Activity {

ImageView image1, image2, image3, image4, image5;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image1 = (ImageView)findViewById(R.id.image1);
image2 = (ImageView)findViewById(R.id.image2);
image3 = (ImageView)findViewById(R.id.image3);
image4 = (ImageView)findViewById(R.id.image4);
image5 = (ImageView)findViewById(R.id.image5);

Bitmap bmOriginal = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
image1.setImageBitmap(bmOriginal);

int width = bmOriginal.getWidth();
int height = bmOriginal.getHeight();
int halfWidth = width/2;
int halfHeight = height/2;

//Half Scaled
Bitmap bmHalf = Bitmap.createScaledBitmap(bmOriginal,
halfWidth, halfHeight, false);
image2.setImageBitmap(bmHalf);

//Upper left 1/4
Bitmap bmPartial3 = Bitmap.createBitmap(halfWidth, halfHeight, Bitmap.Config.ARGB_8888);
int[] pixels3 = new int[halfWidth * halfHeight];
bmOriginal.getPixels(pixels3, 0, halfWidth, 0, 0, halfWidth, halfHeight);
bmPartial3.setPixels(pixels3, 0, halfWidth, 0, 0, halfWidth, halfHeight);
image3.setImageBitmap(bmPartial3);

//Center 1/4
Bitmap bmPartial4 = Bitmap.createBitmap(halfWidth, halfHeight, Bitmap.Config.ARGB_8888);
int[] pixels4 = new int[halfWidth * halfHeight];
bmOriginal.getPixels(pixels4, 0, halfWidth, halfWidth/2, halfHeight/2, halfWidth, halfHeight);
bmPartial4.setPixels(pixels4, 0, halfWidth, 0, 0, halfWidth, halfHeight);
image4.setImageBitmap(bmPartial4);

//Bottom Right 1/4
Bitmap bmPartial5 = Bitmap.createBitmap(halfWidth, halfHeight, Bitmap.Config.ARGB_8888);
int[] pixels5 = new int[halfWidth * halfHeight];
bmOriginal.getPixels(pixels5, 0, halfWidth, halfWidth, halfHeight, halfWidth, halfHeight);
bmPartial5.setPixels(pixels5, 0, halfWidth, 0, 0, halfWidth, halfHeight);
image5.setImageBitmap(bmPartial5);

}
}



Related article:
- Image processing on Bitmap



No comments:

Post a Comment