我是Android新手,我需要你的帮助。我正在尝试创建简单的应用程序,在其中一个中,我想通过使用算法方法将彩色图像转换为灰度。我可以使用Uri和ImageView选择一个图像在屏幕上显示,但我需要能够操作图像。我认为Bitmap类是可行的方法,但我需要一些使用正确方法的指导。
谢谢。
答案 0 :(得分:0)
从Uri获取Bitpmap:
Uri imageUri;//you say you already have this
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri);
//now call the method below to get the grayscale bitmap
Bitmap greyBmp = toGrayscale( bitmap );
//set the ImageView to the new greyscale
Imageview my_img_view = (Imageview ) findViewById (R.id.my_img_view);//your imageview
my_img_view.setImageBitmap( greyBmp );
这是一种将颜色位图转换为灰度位图的方法:
public Bitmap toGrayscale(Bitmap bmpOriginal)
{
int width, height;
height = bmpOriginal.getHeight();
width = bmpOriginal.getWidth();
Bitmap bmpGrayscale = Bitmap.createBitmap(width, height,
Bitmap.Config.RGB_565);
Canvas c = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(bmpOriginal, 0, 0, paint);
return bmpGrayscale;
}