2016-12-06 66 views
-1

我是Android新手,我需要您的幫助。我正在嘗試創建簡單的應用程序,並且在其中一個應用程序中,我想通過使用算法方法將彩色圖像轉換爲灰度圖像。我可以使用Uri和ImageView選擇一個圖像在屏幕上顯示它,但我需要讓它可以操縱圖像。我認爲Bitmap類是要走的路,但我需要一些使用正確方法的指導。需要幫助操縱Android中的圖像 - 轉換爲灰度

謝謝。

回答

0

要從烏里得到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; 
     }