Android-将RGB彩×××转换为灰度

  实例:RGB2Grey 项目运行效果图: Android-将RGB彩×××转换为灰度图_第1张图片           Android-将RGB彩×××转换为灰度图_第2张图片      源代码:
                                                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 p_w_picpathView1 = (ImageView) findViewById(R.id.p_w_picpathView1);
                 final ImageView p_w_picpathView2 = (ImageView) findViewById(R.id.p_w_picpathView2);    
                 //通过位图工厂,创建一个位图
                 final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_android);
                p_w_picpathView1.setImageBitmap(bitmap);
                 //为“转换为灰度图”按钮添加监听事件
                rgb2greyBtn.setOnClickListener( new OnClickListener() {
        
      @Override
       public void onClick(View v) {
         // TODO Auto-generated method stub
         //将转换过后的灰度图显示出来
        p_w_picpathView2.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;
        }
}


 
布局文件:   < 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/p_w_picpathView1"
         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/p_w_picpathView2"
         android:layout_width ="wrap_content"
         android:layout_height ="wrap_content"
         android:layout_gravity ="center_horizontal"
         />"
LinearLayout >
 
       

 

 

更多相关文章

  1. Android异步加载图片并缓存到内存和SD卡上
  2. Android中ImageSwitcher结合Gallery展示SD卡中的资源图片
  3. ImageView 设置图片
  4. 阅读《Android 从入门到精通》(24)——切换图片
  5. android 删除SD卡或者手机的缓存图片和目录
  6. android 视频图片混合轮播实现
  7. android用ImageView显示网络图片
  8. android通过BitmapFactory.decodeFile获取图片bitmap报内存溢出

随机推荐

  1. android支付宝首页、蚂蚁森林效果、视频
  2. Android(安卓)xml资源文件中@、@android:
  3. LinearLayout和RelativeLayout 比较
  4. android Setting中隐藏项
  5. android支持的media文件格式--MediaFile
  6. Android(安卓)给TextView添加点击事件
  7. Android(安卓)修改 以太网 IP地址
  8. Android(安卓)Studio 的原生输入框控件 E
  9. android:inputType常用取值
  10. android:gravity与android:layout_gravit