@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Photo.RESULT_IMAGE && data != null) {
Cursor c = this.getContentResolver().query(data.getData(), null,
null, null, null);
c.moveToFirst();
String imagePath = c.getString(c.getColumnIndex("_data"));
c.close();
File file = new File(imagePath);
Uri originalUri = Uri.fromFile(file);
ClipImage.startPhotoZoom(originalUri, REQUEST_CODE_CLIP,MainActivity.this);
} else if (requestCode == Photo.RESULT_CAMERA) {
File file = new File(Photo.TEMP_IMAGE_PATH);
Uri uri_tack = Uri.fromFile(file);
ClipImage.startPhotoZoom(uri_tack, REQUEST_CODE_CLIP,MainActivity.this);
}else if(requestCode==REQUEST_CODE_CLIP){
Bitmap photo = null;
if (photo == null) {
Bundle bundle = data.getExtras();
if (bundle != null) {
photo = (Bitmap) bundle.get("data");
}
}
if (photo != null) {
image.setImageBitmap(toRoundBitmap.toRoundBitmap(photo));
}
}
}
剪切的工具类
public static void startPhotoZoom(Uri uri, int witch,Context context) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
// 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪图片宽高
intent.putExtra("outputX", 300);
intent.putExtra("outputY", 300);
// 系统的裁剪图片默认对图片进行人脸识别,当识别到有人脸时,
// 会按aspectX和aspectY为1来处理,如果想设置成自定义的裁剪比例,
// 需要设置noFaceDetection为true。
intent.putExtra("noFaceDetection", true);
intent.putExtra("return-data", true);
Activity a=(Activity)context;
a.startActivityForResult(intent, witch);
}
显示圆形图片的工具类
public class ToRoundBitmap {
private static ToRoundBitmap toRoundBitmap;
private ToRoundBitmap(Context context) {
}
public static ToRoundBitmap getInstance(Context context) {
if (toRoundBitmap == null) {
toRoundBitmap = new ToRoundBitmap(context);
}
return toRoundBitmap;
}
public Bitmap toRoundBitmap(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float roundPx;
float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
if (width <= height) {
roundPx = width / 2;
left = 0;
top = 0;
right = width;
bottom = width;
height = width;
dst_left = 0;
dst_top = 0;
dst_right = width;
dst_bottom = width;
} else {
roundPx = height / 2;
float clip = (width - height) / 2;
left = clip;
right = width - clip;
top = 0;
bottom = height;
width = height;
dst_left = 0;
dst_top = 0;
dst_right = height;
dst_bottom = height;
}
Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final Paint paint = new Paint();
final Rect src = new Rect((int) left, (int) top, (int) right,
(int) bottom);
final Rect dst = new Rect((int) dst_left, (int) dst_top,
(int) dst_right, (int) dst_bottom);
final RectF rectF = new RectF(dst);
paint.setAntiAlias(true);// 设置画笔无锯齿
canvas.drawARGB(0, 0, 0, 0); // 填充整个Canvas
// 以下有两种方法画圆,drawRounRect和drawCircle
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);// 画圆角矩形,第一个参数为图形显示区域,第二个参数和第三个参数分别是水平圆角半径和垂直圆角半径。
// canvas.drawCircle(roundPx, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));// 设置两张图片相交时的模式,参考http://trylovecatch.iteye.com/blog/1189452
canvas.drawBitmap(bitmap, src, dst, paint);// 以Mode.SRC_IN模式合并bitmap和已经draw了的Circle
return output;
}
}