Android拍照、相册选取、裁剪图片
具体需求是:拍照或相册选取完毕后,先进入系统裁剪界面,裁剪完毕后再显示或保存图片,下面看下截图部分所要调用的代码:
//截取图片 public void cropImage(Uri uri, int outputX, int outputY, int requestCode){ //裁剪图片意图 Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); intent.putExtra("crop", "true"); //裁剪框的比例,1:1 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); //裁剪后输出图片的尺寸大小 intent.putExtra("outputX", outputX); intent.putExtra("outputY", outputY); //图片格式 intent.putExtra("outputFormat", "JPEG"); intent.putExtra("noFaceDetection", true); intent.putExtra("return-data", true); startActivityForResult(intent, requestCode); }
例如,从相册选择图片后先调用裁剪功能代码如下:
Intent openAlbumIntent = new Intent(Intent.ACTION_GET_CONTENT); openAlbumIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); startActivityForResult(openAlbumIntent, REQUEST_CODE);
接下来在onActivityResult里面对裁剪后的图片进行处理,调用上面的cropImage()方法,将URI传入,500是指定裁剪后图片的尺寸,最后一个参数为标识码:
Uri uri = data.getData(); cropImage(uri, 500, 500, CROP_PICTURE);
处理裁剪后的图片就和之前一样,这里只贴出代码片段仅供参考:
case CROP_PICTURE: Bitmap photo = null; Uri photoUri = data.getData(); if (photoUri != null) { photo = BitmapFactory.decodeFile(photoUri.getPath()); } if (photo == null) { Bundle extra = data.getExtras(); if (extra != null) { photo = (Bitmap)extra.get("data"); ByteArrayOutputStream stream = new ByteArrayOutputStream(); photo.compress(Bitmap.CompressFormat.JPEG, 100, stream); } } iv_image.setImageBitmap(photo); break;
相关推荐
houjinkai 2020-03-01
xiaouncle 2020-01-23
gaoyubotaili 2014-06-18
心丨悦 2019-12-24
luofuIT成长记录 2019-12-15
Lincain 2019-11-16
killmice 2016-07-14
GoAheadY 2011-07-18
RickyHuo成长之路 2012-06-20
xiyf0 2018-05-30
yongzhang 2019-05-24
AbitGo 2019-04-12
haoxun0 2019-04-12
loveyy 2018-04-16
finnaxu 2016-04-29
87921432 2016-03-15
GimmeS 2016-03-01
LiTOPPPP 2015-06-09
QAnyang 2013-10-08