基本信息
源码名称:Android 实现图像的灰度化和二值化
源码大小:1.51M
文件格式:.rar
开发语言:Java
更新时间:2015-07-02
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍
Android 实现图像的灰度化和二值化

 /*******************************灰度化**************************/
            Button buttonGray = (Button) findViewById(R.id.buttonGray);
            buttonGray.setOnClickListener(new View.OnClickListener() {            
                @Override
                public void onClick(View arg0) {
               
                     //bmp=(Bitmap) data.getExtras().get("data");
                     bmp=BitmapFactory.decodeFile(picturePath);
                     //bmp=BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage),null,bmpFactoryOptions);
                     imageCreate = (ImageView) findViewById(R.id.imgView1);
                      
                     createBmp=Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
             
                     canvas = new Canvas(createBmp); //画布 传入位图用于绘制
                paint = new Paint(); //画刷 改变颜色 对比度等属性
                canvas.drawBitmap(bmp, 0, 0, paint);    //错误:没有图片 因为参数bmp写成createBmp
                //imageCreate.setImageBitmap(createBmp);
                imageCreate.setImageBitmap(grayScaleImage(BitmapFactory.decodeFile(picturePath)));
                imageCreate.setVisibility(View.VISIBLE);
                //imageView1.setImageBitmap(grayScaleImage(BitmapFactory.decodeFile(picturePath)));
                 
                }
            });
            
            
            /*******************************二值化**************************/
            Button buttonBinar = (Button) findViewById(R.id.buttonBinar);
            buttonBinar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                                   
                     bmp=BitmapFactory.decodeFile(picturePath);                     
                     imageCreate = (ImageView) findViewById(R.id.imgView1);                    
                     createBmp=Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
             
                    canvas = new Canvas(createBmp); //画布 传入位图用于绘制
                paint = new Paint(); //画刷 改变颜色 对比度等属性
                canvas.drawBitmap(bmp, 0, 0, paint);    //错误:没有图片 因为参数bmp写成createBmp
                imageCreate.setImageBitmap(binaryzationImage(BitmapFactory.decodeFile(picturePath)));
                imageCreate.setVisibility(View.VISIBLE);
               
                }
            });
             
        }
                   
    }
    
 
    
    public  static Bitmap grayScaleImage(Bitmap src) {
   // constant factors
   final double GS_RED = 0.299;
   final double GS_GREEN = 0.587;
   final double GS_BLUE = 0.114;
   
   // create output bitmap
   Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
   // pixel information
   int A, R, G, B;
   int pixel;
   
   // get image size
   int width = src.getWidth();
   int height = src.getHeight();
   
   // scan through every single pixel
   for(int x = 0; x < width; x) {
    for(int y = 0; y < height; y) {
     // get one pixel color
     pixel = src.getPixel(x, y);
     // retrieve color of all channels
     A = Color.alpha(pixel);
     R = Color.red(pixel);
     G = Color.green(pixel);
     B = Color.blue(pixel);
     // take conversion up to one single value
     R = G = B = (int)(GS_RED * R GS_GREEN * G GS_BLUE * B);
     // set new pixel color to output bitmap
     bmOut.setPixel(x, y, Color.argb(A, R, G, B));
    }
   }
   
   // return final image
   return bmOut;
  }
 
    public  static Bitmap binaryzationImage(Bitmap src) {
     
     final double GS_RED = 0.299;
     final double GS_GREEN = 0.587;
     final double GS_BLUE = 0.114;
     // create output bitmap
     Bitmap bmOut1 = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
     // pixel information
     int A, R, G, B;
     int pixel;
     
     // get image size
     int width = src.getWidth();
     int height = src.getHeight();
     
     // scan through every single pixel
     for(int x = 0; x < width; x) {
      for(int y = 0; y < height; y) {
       // get one pixel color
       pixel = src.getPixel(x, y);
       // retrieve color of all channels
       A = Color.alpha(pixel);
       R = Color.red(pixel);
       G = Color.green(pixel);
       B = Color.blue(pixel);
       R = G = B = (int)(GS_RED * R GS_GREEN * G GS_BLUE * B);
       if( R<100)
         R=0;
         else 
          R=255;
       if( G<100)
           G=0;
           else 
            G=255;
      if( B<100)
           B=0;
           else 
            B=255;
       // set new pixel color to output bitmap
       bmOut1.setPixel(x, y, Color.argb(A, R, G, B));
      }
     }
     
     // return final image
     return bmOut1;
    }