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

 更新时间:2012年11月22日 16:18:41   作者:  
本文将详细介绍Android-如何将RGB彩色图转换为灰度图方法,需要了解更多的朋友可以参考下

实例: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 手机SD卡读写操作(以txt文本为例)实现步骤

    android 手机SD卡读写操作(以txt文本为例)实现步骤

    要完成SD卡读写操作首先对manifest注册SD卡读写权限其次是创建一个对SD卡中文件读写的类写一个用于检测读写功能的的布局然后就是UI的类了,感兴趣的朋友可以参考下,希望可以帮助到你
    2013-02-02
  • Android ViewPager实现每隔两秒自动切换图片功能

    Android ViewPager实现每隔两秒自动切换图片功能

    图片来回自动切换,设计非常人性化,那么图片自动切换功能基于代码如何实现的呢?下面小编给大家带来了Android ViewPager实现每隔两秒自动切换图片功能,感兴趣的朋友一起看看吧
    2021-10-10
  • Android实现客户端语音动弹界面实例代码

    Android实现客户端语音动弹界面实例代码

    这篇文章主要介绍了Android实现客户端语音动弹界面实例代码,文章只给大家介绍了控件布局的方法,需要的朋友可以参考下
    2017-11-11
  • Android平台基于Pull方式对XML文件解析与写入方法详解

    Android平台基于Pull方式对XML文件解析与写入方法详解

    这篇文章主要介绍了Android平台基于Pull方式对XML文件解析与写入方法,结合实例形式分析了Android基于Pull方式对xml文件解析及写入操作的步骤、原理与操作技巧,需要的朋友可以参考下
    2016-10-10
  • 详解Flutter中网络框架dio的二次封装

    详解Flutter中网络框架dio的二次封装

    其实dio框架已经封装的很好了,但是在实战项目中,为了项目可以统一管理,还是需要对dio框架进行二次封装。本文将详细讲解一下dio如何二次封装,需要的可以参考一下
    2022-04-04
  • 使用Android原生WebView+Highcharts实现可左右滑动的折线图

    使用Android原生WebView+Highcharts实现可左右滑动的折线图

    折线图是Android开发中经常会碰到的效果,但由于涉及自定义View的知识,对许多刚入门的小白来说会觉得很高深,下面这篇文章主要给大家介绍了关于如何使用Android原生WebView+Highcharts实现可左右滑动的折线图的相关资料,需要的朋友可以参考下
    2022-05-05
  • Android实现画板、写字板功能(附源码下载)

    Android实现画板、写字板功能(附源码下载)

    这篇文章主要介绍了Android实现画板、写字板功能的方法,文中给出了简单的介绍和示例代码,想要了解更多的朋友可以下载源码进行学习,感兴趣的朋友们下面来一起看看吧。
    2017-01-01
  • Android自定义view实现侧滑栏详解

    Android自定义view实现侧滑栏详解

    之前一直没有写侧滑菜单的实现方法,今天计划补上。手机开发中,往往存在很多功能没处放的问题。我们可能会把功能放入一个菜单列表,但现在一种流行的做法是侧滑菜单
    2022-11-11
  • Android自定义带拼音音调Textview

    Android自定义带拼音音调Textview

    这篇文章主要介绍了Android自定义带拼音音调的Textview,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-01-01
  • Android使用WindowManager构造悬浮view

    Android使用WindowManager构造悬浮view

    这篇文章主要为大家详细介绍了Android使用WindowManager构造悬浮view的具体方法,感兴趣的小伙伴们可以参考一下
    2016-05-05

最新评论