服务器之家

服务器之家 > 正文

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

时间:2020-12-19 16:33     来源/作者:Android教程网

实例:rgb2grey

项目运行效果图:       

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

 

 

源代码

[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>

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址 2020-08-12
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
返回顶部