Tuesday, 11 October 2011

Image processing on Bitmap

In this exercise, the bitmaps will be processed in form of int[], to swap color.

Image processing on Bitmap

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;

/** 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);

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;

Bitmap bmDulicated2 = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Bitmap bmDulicated3 = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Bitmap bmDulicated4 = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
int[] srcPixels = new int[width * height];
bmOriginal.getPixels(srcPixels, 0, width, 0, 0, width, height);
int[] destPixels = new int[width * height];

swapGB(srcPixels, destPixels);
bmDulicated2.setPixels(destPixels, 0, width, 0, 0, width, height);
image2.setImageBitmap(bmDulicated2);

swapRB(srcPixels, destPixels);
bmDulicated3.setPixels(destPixels, 0, width, 0, 0, width, height);
image3.setImageBitmap(bmDulicated3);

swapRG(srcPixels, destPixels);
bmDulicated4.setPixels(destPixels, 0, width, 0, 0, width, height);
image4.setImageBitmap(bmDulicated4);


}

void swapGB(int[] src, int[] dest){
for(int i = 0; i < src.length; i++){
dest[i] = (src[i] & 0xffff0000)
| ((src[i] & 0x000000ff)<<8)
| ((src[i] & 0x0000ff00)>>8);
}
}

void swapRB(int[] src, int[] dest){
for(int i = 0; i < src.length; i++){
dest[i] = (src[i] & 0xff00ff00)
| ((src[i] & 0x000000ff)<<16)
| ((src[i] & 0x00ff0000)>>16);
}
}

void swapRG(int[] src, int[] dest){
for(int i = 0; i < src.length; i++){
dest[i] = (src[i] & 0xff0000ff)
| ((src[i] & 0x0000ff00)<<8)
| ((src[i] & 0x00ff0000)>>8);
}
}
}



Related article:
- Bitmap processing: createScaledBitmap, getPixels and setPixels.



No comments:

Post a Comment