android图片二值化并显示在ImageView,该怎么处理

android图片二值化并显示在ImageView请各位大侠帮忙。是个按钮的监听实现。下面得到图片的方法一和二 结果是

android图片二值化并显示在ImageView

  请各位大侠帮忙。是个按钮的监听实现。下面得到图片的方法一和二 结果是一样的吗??
 ImageView上只显示Bitmap格式的图片吗???
 怎么把像素数组转化成Bitmap并显示在ImageView上??
 万分感谢!!  
 

  /*得到图片的大小 一*/
  BitmapFactory.Options bmOptions = new BitmapFactory.Options();
  bmOptions.inJustDecodeBounds = true;//dont save pic to memory ,only get width and height
  Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
  //the resulting width and height of the bitmap
  int photoW = bmOptions.outWidth;
  int photoH = bmOptions.outHeight;
   
  /*得到图片的大小 二*/
   
  BufferedImage image = null;
  File f = new File(mCurrentPhotoPath);
   
  try {
  image=ImageIO.read(f);
  } catch (IOException e) {
   
  e.printStackTrace();
  }
  int iw = image.getWidth();
  int ih = image.getHeight();
  // 上面两种方法得到的图片的大小是一样的吗??
 
  /*存储图片*/  
   
  int pixels [] = new int [iw*ih]; //如果想存储到二位数组中,怎么实现呢??
 
  /*图片做黑白处理*/
  int grey = 100;
  // 对图像进行二值化处理,Alpha值保持不变
  ColorModel cm = ColorModel.getRGBdefault();
  for (int i = 0; i < iw * ih; i++) {
  int red, green, blue;
  int alpha = cm.getAlpha(pixels);
  if (cm.getRed(pixels) > grey) {
  red = 255;
  } else {
  red = 0;
  }
  if (cm.getGreen(pixels) > grey) {
  green = 255;
  } else {
  green = 0;
   
  }
  if (cm.getBlue(pixels) > grey) {
  blue = 255; } else { blue = 0; }
  pixels = alpha << 24 | red << 16 | green << 8 | blue;
 
  /*图片显示在ImageView上*/
bmOptions.inJustDecodeBounds = false;
  bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
  bitmap.setPixels(pixels, 0, photoW, 0, 0, photoW, photoH);
 
  mImageView.setImageBitmap(bitmap);
  mImageView.setVisibility(View.VISIBLE);




一点按钮就异常退出了
 






 
























































高级模式 
B Color Image Link Quote Code Smilies |添加附件
 












发表回复 回帖后跳转到最后一页 









. .
 




[解决办法]
方法1:没有真正载入图片数据,只是得到宽和高
bmOptions.inJustDecodeBounds = true;//dont save pic to memory ,only get width and height

方法2:BufferedImage是java awt的方法,不是android的。

看下这个例子吧:
http://download.csdn.net/detail/sdsunqian/4128949