故作坚强 2014-04-03
具体需求是:拍照或相册选取完毕后,先进入系统裁剪界面,裁剪完毕后再显示或保存图片,下面看下截图部分所要调用的代码:
//截取图片 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;