android图片圆角、图片去色处理示例

2019-12-10 20:04:48刘景俊

  */
 public static void saveBefore(String path) {
  BitmapFactory.Options options = new BitmapFactory.Options();
  options.inJustDecodeBounds = true;
  // 获取这个图片的宽和高
  Bitmap bitmap = BitmapFactory.decodeFile(path, options); // 此时返回bm为空
  options.inJustDecodeBounds = false;
  // 计算缩放比
  int be = (int) (options.outHeight / (float) 200);
  if (be <= 0)
   be = 1;
  options.inSampleSize = 2; // 图片长宽各缩小二分之一
  // 重新读入图片,注意这次要把options.inJustDecodeBounds 设为 false哦
  bitmap = BitmapFactory.decodeFile(path, options);
  int w = bitmap.getWidth();
  int h = bitmap.getHeight();
  System.out.println(w + "   " + h);
  // savePNG_After(bitmap,path);
  saveJPGE_After(bitmap, path);
 }

 /**
  * 保存图片为PNG
  * 
  * @param bitmap
  * @param name
  */
 public static void savePNG_After(Bitmap bitmap, String name) {
  File file = new File(name);
  try {
   FileOutputStream out = new FileOutputStream(file);
   if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)) {
    out.flush();
    out.close();
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 /**
  * 保存图片为JPEG
  * 
  * @param bitmap
  * @param path
  */
 public static void saveJPGE_After(Bitmap bitmap, String path) {
  File file = new File(path);
  try {
   FileOutputStream out = new FileOutputStream(file);
   if (bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)) {
    out.flush();
    out.close();
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 /**
  * 图片合成
  * 
  * @param bitmap