团队博客

换种剪裁方法。

Intent intentCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, imagePhotoUri);
startActivityForResult(intentCamera, PHOTO_REQUEST_CAREMA);
public static void cropImageUri(Activity activity, Uri orgUri, Uri desUri, int width, int height, int requestCode) {
  Intent intent = new Intent("com.android.camera.action.CROP");
  intent.setDataAndType(orgUri, "image/*");
  intent.putExtra("crop", "true");
  intent.putExtra("aspectX", 1);
  intent.putExtra("aspectY", 1);
  intent.putExtra("outputX", width);
  intent.putExtra("outputY", height);
  intent.putExtra("scale", true);
  //将剪切的图片保存到目标Uri中
  intent.putExtra(MediaStore.EXTRA_OUTPUT, desUri);
  intent.putExtra("return-data", false);
  intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
  intent.putExtra("noFaceDetection", true);
  activity.startActivityForResult(intent, requestCode);
}
private static Bitmap decodeUriAsBitmap(Context context, Uri uri) {
  Bitmap bitmap;
  try {
    bitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri));
  } catch (FileNotFoundException e) {
    e.printStackTrace();
    return null;
  }
  return bitmap;
}
//获取圆形图片
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
  if (bitmap == null) {
  return null;
  }
  Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
  Canvas canvas = new Canvas(output);
  final Paint paint = new Paint();
  /* 去锯齿 */
  paint.setAntiAlias(true);
  paint.setFilterBitmap(true);
  paint.setDither(true);
  // 保证是方形,并且从中心画
  int width = bitmap.getWidth();
  int height = bitmap.getHeight();
  int w;
  int deltaX = 0;
  int deltaY = 0;
  if (width <= height) {
    w = width;
    deltaY = height - w;
  } else {
    w = height;
    deltaX = width - w;
  }
  final Rect rect = new Rect(deltaX, deltaY, w, w);
  final RectF rectF = new RectF(rect);
 
  paint.setAntiAlias(true);
  canvas.drawARGB(0, 0, 0, 0);
  // 圆形,所有只用一个
  int radius = (int) (Math.sqrt(w * w * 2.0d) / 2);
  canvas.drawRoundRect(rectF, radius, radius, paint);
  paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
  canvas.drawBitmap(bitmap, rect, rect, paint);
  return output;
}

 

posted @ 2021-05-11 20:37  谦寻  阅读(47)  评论(0编辑  收藏  举报