欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android-如何將RGB彩色圖轉(zhuǎn)換為灰度圖方法介紹

 更新時間:2012年11月22日 16:18:41   作者:  
本文將詳細(xì)介紹Android-如何將RGB彩色圖轉(zhuǎn)換為灰度圖方法,需要了解更多的朋友可以參考下

實例:RGB2Grey

項目運行效果圖:       

         \

 

 

源代碼

[java] 
public class MainActivity extends Activity { 

    /* (non-Javadoc)
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        //通過Id來獲取界面中組件的引用  
        Button rgb2greyBtn  = (Button) findViewById(R.id.rgb2greybtn); 
        ImageView imageView1 = (ImageView) findViewById(R.id.imageView1); 
        final ImageView imageView2 = (ImageView) findViewById(R.id.imageView2);  
        //通過位圖工廠,創(chuàng)建一個位圖  
        final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_android); 
        imageView1.setImageBitmap(bitmap); 
        //為“轉(zhuǎn)換為灰度圖”按鈕添加監(jiān)聽事件  
        rgb2greyBtn.setOnClickListener(new OnClickListener() { 

            @Override 
            public void onClick(View v) { 
                // TODO Auto-generated method stub  
                //將轉(zhuǎn)換過后的灰度圖顯示出來  
                imageView2.setImageBitmap(convertGreyImg(bitmap)); 
            } 
        }); 

    } 

    /**
     * 將彩色圖轉(zhuǎn)換為灰度圖
     * @param img 位圖
     * @return  返回轉(zhuǎn)換好的位圖
     */ 
    public Bitmap convertGreyImg(Bitmap img) { 
        int width = img.getWidth();         //獲取位圖的寬  
        int height = img.getHeight();       //獲取位圖的高  

        int []pixels = new int[width * height]; //通過位圖的大小創(chuàng)建像素點數(shù)組  

        img.getPixels(pixels, 0, width, 0, 0, width, height); 
        int alpha = 0xFF << 24;  
        for(int i = 0; i < height; i++)  { 
            for(int j = 0; j < width; j++) { 
                int grey = pixels[width * i + j]; 

                int red = ((grey  & 0x00FF0000 ) >> 16); 
                int green = ((grey & 0x0000FF00) >> 8); 
                int blue = (grey & 0x000000FF); 

                grey = (int)((float) red * 0.3 + (float)green * 0.59 + (float)blue * 0.11); 
                grey = alpha | (grey << 16) | (grey << 8) | grey; 
                pixels[width * i + j] = grey; 
            } 
        } 
        Bitmap result = Bitmap.createBitmap(width, height, Config.RGB_565); 
        result.setPixels(pixels, 0, width, 0, 0, width, height); 
        return result; 
    } 

public class MainActivity extends Activity {

    /* (non-Javadoc)
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //通過Id來獲取界面中組件的引用
        Button rgb2greyBtn  = (Button) findViewById(R.id.rgb2greybtn);
        ImageView imageView1 = (ImageView) findViewById(R.id.imageView1);
        final ImageView imageView2 = (ImageView) findViewById(R.id.imageView2);
        //通過位圖工廠,創(chuàng)建一個位圖
        final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_android);
        imageView1.setImageBitmap(bitmap);
        //為“轉(zhuǎn)換為灰度圖”按鈕添加監(jiān)聽事件
        rgb2greyBtn.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    //將轉(zhuǎn)換過后的灰度圖顯示出來
    imageView2.setImageBitmap(convertGreyImg(bitmap));
   }
  });

    }

    /**
     * 將彩色圖轉(zhuǎn)換為灰度圖
     * @param img 位圖
     * @return 返回轉(zhuǎn)換好的位圖
     */
    public Bitmap convertGreyImg(Bitmap img) {
     int width = img.getWidth();   //獲取位圖的寬
     int height = img.getHeight();  //獲取位圖的高

     int []pixels = new int[width * height]; //通過位圖的大小創(chuàng)建像素點數(shù)組

     img.getPixels(pixels, 0, width, 0, 0, width, height);
     int alpha = 0xFF << 24;
     for(int i = 0; i < height; i++) {
      for(int j = 0; j < width; j++) {
       int grey = pixels[width * i + j];

       int red = ((grey  & 0x00FF0000 ) >> 16);
       int green = ((grey & 0x0000FF00) >> 8);
       int blue = (grey & 0x000000FF);

       grey = (int)((float) red * 0.3 + (float)green * 0.59 + (float)blue * 0.11);
       grey = alpha | (grey << 16) | (grey << 8) | grey;
       pixels[width * i + j] = grey;
      }
     }
     Bitmap result = Bitmap.createBitmap(width, height, Config.RGB_565);
     result.setPixels(pixels, 0, width, 0, 0, width, height);
     return result;
    }
}
 

布局文件:

[html] 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/LinearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
    <ImageView  
        android:id="@+id/imageView1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center_horizontal" 
        /> 
    <Button  
        android:id="@+id/rgb2greybtn" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/rgb2greybtn" 
        android:layout_gravity="center_horizontal"/> 
    <ImageView  
        android:id="@+id/imageView2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center_horizontal" 
        />" 
</LinearLayout> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 <ImageView
     android:id="@+id/imageView1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_gravity="center_horizontal"
     />
 <Button
     android:id="@+id/rgb2greybtn"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="@string/rgb2greybtn"
     android:layout_gravity="center_horizontal"/>
 <ImageView
     android:id="@+id/imageView2"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_gravity="center_horizontal"
     />"
</LinearLayout>

相關(guān)文章

  • android開發(fā)之Json文件的讀寫的示例代碼

    android開發(fā)之Json文件的讀寫的示例代碼

    這篇文章主要介紹了android開發(fā)之Json文件的讀寫的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • Android線程中設(shè)置控件的值提示報錯的解決方法

    Android線程中設(shè)置控件的值提示報錯的解決方法

    這篇文章主要介紹了Android線程中設(shè)置控件的值提示報錯的解決方法,實例分析了textview報錯的原因以及Handler設(shè)置來解決錯誤的實現(xiàn)技巧,需要的朋友可以參考下
    2016-06-06
  • PullToRefreshListView實現(xiàn)多條目加載上拉刷新和下拉加載

    PullToRefreshListView實現(xiàn)多條目加載上拉刷新和下拉加載

    這篇文章主要為大家詳細(xì)介紹了PullToRefreshListView實現(xiàn)多條目加載上拉刷新和下拉加載,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • Flutter實現(xiàn)支付寶集五福手畫福字功能

    Flutter實現(xiàn)支付寶集五福手畫福字功能

    支付寶一年一度的集五?;顒佑珠_始了,其中包含了一個功能就是手寫福字,還包括撤銷一筆,清除重寫,保存相冊等。本文將介紹如何使用Flutter實現(xiàn)這些功能,感興趣的可以了解一下
    2022-01-01
  • ListView的View回收引起的checkbox狀態(tài)改變監(jiān)聽等問題解決方案

    ListView的View回收引起的checkbox狀態(tài)改變監(jiān)聽等問題解決方案

    之前講到了自定義Adapter傳遞給ListView時,因為ListView的View回收,需要注意當(dāng)ListView列表項中包含有帶有狀態(tài)標(biāo)識控件的問題,感興趣的朋友可以祥看本文,或許會有意外的收獲哦
    2013-01-01
  • Android Studio安裝配置方法圖文教程

    Android Studio安裝配置方法圖文教程

    這篇文章主要為大家詳細(xì)介紹了Android Studio安裝配置方法圖文教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • Kotlin中局部方法的深入探究

    Kotlin中局部方法的深入探究

    這篇文章主要給大家介紹了關(guān)于Kotlin中局部方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-10-10
  • Android下Activity全屏顯示實現(xiàn)方法

    Android下Activity全屏顯示實現(xiàn)方法

    這篇文章主要介紹了Android下Activity全屏顯示實現(xiàn)方法,以兩種不同的方法來實現(xiàn)這一技巧,非常具有實用性,需要的朋友可以參考下
    2014-10-10
  • RxJava取消訂閱的各種方式的實現(xiàn)

    RxJava取消訂閱的各種方式的實現(xiàn)

    這篇文章主要介紹了RxJava取消訂閱的各種方式的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Android開發(fā)解決popupWindow重疊報錯問題

    Android開發(fā)解決popupWindow重疊報錯問題

    今天小編就為大家分享一篇關(guān)于Android開發(fā)解決popupWindow重疊報錯問題的文章,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-10-10

最新評論