Copy your test picture to /res/drawable folder, name it testpicture.jpg.
package com.AndroidBitmapProcessing;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.ImageView;
public class AndroidBitmapProcessingActivity extends Activity {
ImageView imageView_Source, imageView_Gray, imageView_BW;
Bitmap bitmap_Source, bitmap_Dest;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView_Source = (ImageView)findViewById(R.id.source);
imageView_Gray = (ImageView)findViewById(R.id.imageGray);
imageView_BW = (ImageView)findViewById(R.id.imageBW);
bitmap_Source = BitmapFactory.decodeResource(getResources(), R.drawable.testpicture);
imageView_Gray.setImageBitmap(processingBitmap_Gray(bitmap_Source));
imageView_BW.setImageBitmap(processingBitmap_BW(bitmap_Source));
}
private Bitmap processingBitmap_Gray(Bitmap src){
Bitmap dest = Bitmap.createBitmap(
src.getWidth(), src.getHeight(), src.getConfig());
for(int x = 0; x < src.getWidth(); x++){
for(int y = 0; y < src.getHeight(); y++){
int pixelColor = src.getPixel(x, y);
int pixelAlpha = Color.alpha(pixelColor);
float pixelRed = (float)Color.red(pixelColor) * 0.3f;
float pixelGreen = (float)Color.green(pixelColor) * 0.59f;
float pixelBlue = (float)Color.blue(pixelColor) * 0.11f;
int pixelBW = (int)(pixelRed + pixelGreen + pixelBlue);
int newPixel = Color.argb(
pixelAlpha, pixelBW, pixelBW, pixelBW);
dest.setPixel(x, y, newPixel);
}
}
return dest;
}
private Bitmap processingBitmap_BW(Bitmap src){
Bitmap dest = Bitmap.createBitmap(
src.getWidth(), src.getHeight(), src.getConfig());
for(int x = 0; x < src.getWidth(); x++){
for(int y = 0; y < src.getHeight(); y++){
int pixelColor = src.getPixel(x, y);
int pixelAlpha = Color.alpha(pixelColor);
int pixelRed = Color.red(pixelColor);
int pixelGreen = Color.green(pixelColor);
int pixelBlue = Color.blue(pixelColor);
int pixelBW = (pixelRed + pixelGreen + pixelBlue)/3;
int newPixel = Color.argb(
pixelAlpha, pixelBW, pixelBW, pixelBW);
dest.setPixel(x, y, newPixel);
}
}
return dest;
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Original" />
<ImageView
android:id="@+id/source"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/testpicture"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Gray: 0.3R + 0.59G + 0.11B" />
<ImageView
android:id="@+id/imageGray"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="B and W: (R + G + B)/3" />
<ImageView
android:id="@+id/imageBW"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
</LinearLayout>
The code listed here convert color bitmap to gray scale by calculation. It's another approach using ColorMatrixColorFilter; refer to next article: Convert bitmap from color to GrayScale using ColorMatrixColorFilter
0 Response to "Convert bitmap from color to GrayScale"
Posting Komentar