需要帮助在android中操作图像-转换为灰度

需要帮助在android中操作图像-转换为灰度,android,image,bitmap,uri,Android,Image,Bitmap,Uri,我是一个Android新手,我需要你的帮助。我正在尝试创建简单的应用程序,在其中一个应用程序中,我想通过使用算法方法将彩色图像转换为灰度。我可以使用Uri和ImageView来选择一个图像在屏幕上显示,但是我需要使操作图像成为可能。我认为位图类是一个不错的选择,但是我需要一些使用正确方法的指导 多谢各位 要从Uri获取Bitpmap: Uri imageUri;//you say you already have this Bitmap bitmap = MediaStore.Images.Me

我是一个Android新手,我需要你的帮助。我正在尝试创建简单的应用程序,在其中一个应用程序中,我想通过使用算法方法将彩色图像转换为灰度。我可以使用Uri和ImageView来选择一个图像在屏幕上显示,但是我需要使操作图像成为可能。我认为位图类是一个不错的选择,但是我需要一些使用正确方法的指导


多谢各位

要从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;
        }