Android-如何将RGB彩色图转换为灰度图方法介绍

所属分类: 软件编程 / Android 阅读数: 1081
收藏 0 赞 0 分享

实例: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);  
        //通过位图工厂,创建一个位图  
        final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_android); 
        imageView1.setImageBitmap(bitmap); 
        //为“转换为灰度图”按钮添加监听事件  
        rgb2greyBtn.setOnClickListener(new OnClickListener() { 

            @Override 
            public void onClick(View v) { 
                // TODO Auto-generated method stub  
                //将转换过后的灰度图显示出来  
                imageView2.setImageBitmap(convertGreyImg(bitmap)); 
            } 
        }); 

    } 

    /**
     * 将彩色图转换为灰度图
     * @param img 位图
     * @return  返回转换好的位图
     */ 
    public Bitmap convertGreyImg(Bitmap img) { 
        int width = img.getWidth();         //获取位图的宽  
        int height = img.getHeight();       //获取位图的高  

        int []pixels = new int[width * height]; //通过位图的大小创建像素点数组  

        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);
        //通过位图工厂,创建一个位图
        final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_android);
        imageView1.setImageBitmap(bitmap);
        //为“转换为灰度图”按钮添加监听事件
        rgb2greyBtn.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    //将转换过后的灰度图显示出来
    imageView2.setImageBitmap(convertGreyImg(bitmap));
   }
  });

    }

    /**
     * 将彩色图转换为灰度图
     * @param img 位图
     * @return 返回转换好的位图
     */
    public Bitmap convertGreyImg(Bitmap img) {
     int width = img.getWidth();   //获取位图的宽
     int height = img.getHeight();  //获取位图的高

     int []pixels = new int[width * height]; //通过位图的大小创建像素点数组

     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>

更多精彩内容其他人还在看

Android 动画之AlphaAnimation应用详解

本节讲解AlphaAnimation 动画,窗口的动画效果,淡入淡出什么的,有些游戏的欢迎动画,logo的淡入淡出效果就使用AlphaAnimation,具体的祥看本文,需要的朋友可以参考下
收藏 0 赞 0 分享

Android 动画之TranslateAnimation应用详解

本节讲解TranslateAnimation动画,TranslateAnimation比较常用,比如QQ,网易新闻菜单条的动画,就可以用TranslateAnimation实现,本文将详细介绍通过TranslateAnimation 来定义动画,需要的朋友可以参考下
收藏 0 赞 0 分享

Android 动画之ScaleAnimation应用详解

本节讲解ScaleAnimation 动画在应用中的实现,有需要的朋友可以参考下
收藏 0 赞 0 分享

Android 动画之RotateAnimation应用详解

本节讲解旋转动画效果RotateAnimation方法的应用,有需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之文件操作模式深入理解

本文将介绍Android开发之文件操作模式,需要了解的朋友可以参考下
收藏 0 赞 0 分享

Android应用程序窗口(Activity)窗口对象(Window)创建指南

本文将详细介绍Android应用程序窗口(Activity)的窗口对象(Window)的创建过程,需要了解的朋友可以参考下
收藏 0 赞 0 分享

android activity设置无标题实现全屏

本文将详细介绍Android如何设置Activity全屏和无标题的实现方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android启动模拟器报错解决方法

本文将详细介绍Android模拟器报"Failed To Allocate memory 8"错误的解决办法,需要了解的朋友可以参考下
收藏 0 赞 0 分享

Android如何实现非本地图片的点击态

Android如何实现非本地图片的点击态,本文提供了详细的实现代码,需要了解的朋友可以参考下
收藏 0 赞 0 分享

android viewpaper实例探讨

本文将提供一个android viewpaper实例实现过程,需要了解更多的朋友可以参考下
收藏 0 赞 0 分享
查看更多