实现的步骤:
1. 调用相册中的图片裁剪然后显示。
2. 调用拍照功能获得图片裁剪然后显示。
调用相册中的图片裁剪然后显示
第一步 跳转系统相册获取图片
public static final int VALUE_PICK_PICTURE = 2; private void selectPicFromLocal() { Intent intent = new Intent(Intent.ACTION_PICK, null); intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); startActivityForResult(intent, VALUE_PICK_PICTURE); }
- new Intent(Intent.ACTION_PICK, null); 使用这个方法可以调用,会让用户选择照片选取工具。系统会选取所有可用的程序来供用户选择
-
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*")
如果要限制上传到服务器的图片类型时可以直接写如:”image/jpeg 、 image/png等的类型”
第二步 onActivityResult接收所选图片信息
protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { switch (requestCode) { case VALUE_PICK_PICTURE: // 如果是直接从相册获取 startPhotoZoom(data.getData());//拿到所选图片的Uri break; } } }
通过data.getData()方法拿到所选图片的Uri,这里需要注意,如果跳转以后用户并没有选择任何图片,并按了返回键,那么data就是null,就会导致空指针异常。这也就是我们判断resultCode == RESULT_OK
的原因。如果用户按了返回键,这里的resultCode == RESULT_CANCELED
。
第三步 裁剪所选取的图片
private static final String IMAGE_FILE_LOCATION = "file:///" + Environment.getExternalStorageDirectory().getPath() + "/temp.jpg"; private Uri imageUri = Uri.parse(IMAGE_FILE_LOCATION); public void startPhotoZoom(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); // 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪 intent.putExtra("crop", "true"); //该参数可以不设定用来规定裁剪区的宽高比 intent.putExtra("aspectX", 2); intent.putExtra("aspectY", 1); //该参数设定为你的imageView的大小 intent.putExtra("outputX", 600); intent.putExtra("outputY", 300); intent.putExtra("scale", true); //是否返回bitmap对象 intent.putExtra("return-data", false); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());//输出图片的格式 intent.putExtra("noFaceDetection", true); // 头像识别 startActivityForResult(intent, 3); }
其中aspectX/aspectY 是宽高比 可以设定为16:9 或者 1:1 如果不设定那么裁剪取得大小将会是由用户决定,也就是裁剪区的宽高比不定。
outputX/outputY则是该intent裁剪完成输出的图片的宽高,这个属性当我们需要得到一个与imageView相同大小的图片的时候可以使用,如果我们裁剪的图片比我们设定的值小,那么裁剪完成将得到一个与我们所选区域一样大小的图片。如果我们要裁剪的图片比较大,那么裁剪完成输出的将会是我们所设置的宽高值。所以综上我们只需要将该值设置成我们希望得到的像素值就可以了。
还有一个属性也非常重要,return-data 这是属性决定我们在onActivityResult中接收到是什么数据,如果设置为true那么data将会返回一个bitmap如果我们设置为false,则会将图片保存到本地并将对应的uri返回,当然这个uri得有我们自己设定。系统裁剪完成后将会将裁剪完成的图片保存在我们所这设定这个uri地址上。我们只需要在裁剪完成后直接调用该uri来设置图片,就可以了。
下面是调用系统相册裁剪图片的完整代码:
public class PictureActivity extends AppCompatActivity { private static final String TAG = PictureActivity.class.getSimpleName(); private static final int CHOOSE_PICTURE = 1; private static final int CROP_PICTURE = 2; @BindView(R.id.btn_select_from_local) Button mBtnSelectFromLocal; @BindView(R.id.iv_local) ImageView mIvLocal; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_picture); ButterKnife.bind(this); } @OnClick(R.id.btn_select_from_local) public void onClick() { selectFromLocal(); } private void selectFromLocal() { Intent intent = new Intent(Intent.ACTION_PICK, null); intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); startActivityForResult(intent, CHOOSE_PICTURE); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Log.d(TAG, resultCode + ""); if (resultCode == RESULT_OK) { switch (requestCode) { case CHOOSE_PICTURE: startPhotoZoom(data.getData()); break; case CROP_PICTURE: // 取得裁剪后的图片 try { mIvLocal.setImageBitmap(BitmapFactory.decodeStream( getContentResolver().openInputStream(imageUri) )); } catch (FileNotFoundException e) { e.printStackTrace(); } break; default: break; } } } private static final String IMAGE_FILE_LOCATION = "file:///" + Environment.getExternalStorageDirectory().getPath() + "/temp.jpg"; private Uri imageUri = Uri.parse(IMAGE_FILE_LOCATION); public void startPhotoZoom(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); // 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪 intent.putExtra("crop", "true"); intent.putExtra("scale", true); intent.putExtra("aspectX", 2); intent.putExtra("aspectY", 1); intent.putExtra("outputX", 600); intent.putExtra("outputY", 300); intent.putExtra("return-data", false); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); // no face detection startActivityForResult(intent, CROP_PICTURE); } }调用系统相机拍照,然后裁剪图片
与从相册选取不同之处在于获取图片的途径不同,之后的裁剪操作基本相似。
第一步 调用系统相机拍照
这里为了记录不同的Uri生成方式,就使用了File的方式,本质上与上述使用相册选取的时候拼接的String没有区别。
private File mFile = new File(Environment.getExternalStorageDirectory().getAbsoluteFile(), "temp.jpg"); private void selectPicFromCamera() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // 下面这句指定调用相机拍照后的照片存储的路径 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mFile)); startActivityForResult(intent, PICK_CAMERA); }
第二步 调用裁剪方法
onActivityResult中增加的case
case PICK_CAMERA: startPhotoZoom(Uri.fromFile(mFile)); break;
public void startPhotoZoom(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); // 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪 intent.putExtra("crop", "true"); intent.putExtra("scale", true); intent.putExtra("aspectX", 2); intent.putExtra("aspectY", 1); intent.putExtra("outputX", 600); intent.putExtra("outputY", 300); intent.putExtra("return-data", false); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); // no face detection startActivityForResult(intent, CROP_PICTURE); }这里需要注意我们设置的相机拍照完成保存的图片位置如果和
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
裁剪完成保存地址相同的话,那么裁剪完成后,将会替换原来所拍的图片。